|Welcome to the Monastery|
Re^5: Burned by precedence rules (not)by tye (Cardinal)
|on Dec 27, 2008 at 06:53 UTC||Need Help??|
Actually, I've seen the confusion happen with just a simple "unless $bool". Making the negation more explicit tends to help. I've quite recently seen several cases where I and people I've worked with were persistently confused as to how code could be doing what it was doing and it turned out that the part that was confusing us was the use of "unless".
I've also tried to simply replace an "unless" with an "if" and thus produced a horribly complicated bunch of negations (which I didn't keep). Most of the time, I can find other subtle negations and cancel them out. Things like "unless $count != 0".
Of course, "unless" seems to be "obviously" the same as "if not". But if the human mind actually worked that way, then there would have been no confusion when I answered "Is this preview button regarding updates not wanted?" with "No". The human mind doesn't do "double negatives" like some claim English does (and thus also not like programming languages do).
So reduce the number of negations in your programming constructs. And make the negations that you do more explicit. "unless" may be appealing because it eliminates a negation, but it doesn't, it just makes the negation more subtle.
An example of a really bad subtle double negative is "last unless ...". "last" has a subtle negation in it, since it means "do not keep looping".
And when a conditional gets complicated, sometimes the best route is to just add one or more named items for different parts of the conditional.
or, usually better, especially if the short-circuiting is important: