Computer Science Canada [Tip] Smarter Conditionals |
Author: | wtd [ Tue Feb 13, 2007 1:25 am ] | ||||
Post subject: | [Tip] Smarter Conditionals | ||||
Code following this pattern:
Is better written as:
Yes, this seems horribly obvious, but it's something a lot of people do a lot more than they'd like to. |
Author: | Hikaru79 [ Tue Feb 13, 2007 1:39 am ] | ||||
Post subject: | Re: [Tip] Smarter Conditionals | ||||
It probably happens because a lot of people aren't aware of exactly how "&&" type of statements get evaluated. For example, it is understandable for one to (mistakenly) think that
It's important to consciously be aware of the way the first if statement is evaluated. Once we have that, the rest follows |
Author: | BenLi [ Tue Feb 13, 2007 8:56 am ] | ||||
Post subject: | Re: [Tip] Smarter Conditionals | ||||
I disagree if you have this
wouldn't it be a better logic structure if it was more like:
or explain why the above is perceived to be better |
Author: | rdrake [ Tue Feb 13, 2007 11:44 am ] |
Post subject: | RE:[Tip] Smarter Conditionals |
In the case wtd provided, it's best to condense it. In your case BenLi, it's best to factor out that bit. It really depends on the situation. |
Author: | wtd [ Tue Feb 13, 2007 11:49 am ] | ||||
Post subject: | Re: [Tip] Smarter Conditionals | ||||
Hikaru79 @ Tue Feb 13, 2007 2:39 pm wrote: It probably happens because a lot of people aren't aware of exactly how "&&" type of statements get evaluated. For example, it is understandable for one to (mistakenly) think that
It's important to consciously be aware of the way the first if statement is evaluated. Once we have that, the rest follows It's important to note that not all language specifications include "and" and "or" operators that short-circuit. |