View Single Post
Old 01-29-2022, 05:11 AM   #318
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,509
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Thanks Since both of them use in-progress I was able to reduce it a tiny bit more:
Code:
elif						
	(status == 'In-Progress' &&
	(!select(ids, 'ao3')  && $publisher == 'Archive of Our Own') 						
	||
	(!select(ids, 'ffnet') && $publisher == 'FanFiction.net')	
	)
then "metadata.png"
You should pay attention to operator precedence and the implied parentheses. The && operator has higher precedence than the || operator, so your statement will evaluate like this:
Code:
program:
if
		(	status == 'In-Progress' &&
			(!select(ids, 'ao3')  && $publisher == 'Archive of Our Own') 						
		) 
	||
		(
			(!select(ids, 'ffnet') && $publisher == 'FanFiction.net')	
		)
then "metadata.png"
fi
That isn't what you want because the status == isn't done for the 'ffnet' branch. Instead you want this:
Code:
program:
if	status == 'In-Progress' &&
	(
			(!select(ids, 'ao3')  && $publisher == 'Archive of Our Own')

		||
			(!select(ids, 'ffnet') && $publisher == 'FanFiction.net')	
	)
then "metadata.png"
fi
By putting the second two checks in parentheses you ensure that the status == check is done for both of them.

My advice: don't depend on operator precedence unless you really know what you are doing. Instead use parentheses to specify the order of evaluation.

EDIT: I forgot to mention: factoring conditions in ifs is a very good thing for performance and readability. It makes the flow clearer and takes maximum advantage of condition short circuiting. Good that you noticed it.

Last edited by chaley; 01-29-2022 at 10:45 AM.
chaley is offline   Reply With Quote