Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: Useless <c>unless</c> (subtle)

by tye (Sage)
on Aug 07, 2005 at 06:56 UTC ( [id://481628]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Useless <c>unless</c>
in thread Useless unless

I didn't believe this until I personally hit it and now I've hit it personally more than once (even after I've been aware of the potential) and I've seen others hit it. And it doesn't even take two variables. The "unless" can make the "not" blend into the background and you can go over the code 5 times trying to figure out what is wrong and not realize that you've got one too few or too many implicit "not"s involved.

There are lots of possible implicit "not"s. You can have an implicit "not" because your variable is called $disabled. You can have an implicit "not" because you are using < instead of >= or such. You can have an implicit "not" because the object lets you ask "are you empty" when you want to ask "do you have any members" or vice versa. Then you can have explicit "not"s (or "!"s).

But the "not" implicit in "unless" is just too subtle and is too easy for your mind (if you are over 40) to drop. And you should try to never have more than one implicit "not" per conditional. But you are likely to not notice the other implicit "not"s when you are writing the code. Or the implicit "not" may not be there when you write the code because you are thinking at it from one direction but the bug forces you to think at it from another direction (which may partially explain why you've got a bug there).

I also dislike swapping out keywords when maintaining code. So I dislike changing "if" to "unless" or vice versa when a conditional is adjusted. Phrase the test so it works with "if" and is as clear as possible, and if that requires that you add a not, then add an explicit one. "if not" will be less likely to confuse you than "unless" when you are agitated trying to debug something and it flows just as well even if you are trying to make your code read like lovely prose.

So avoid the one sure source of an implicit "not": unless. Remove it from the language? Well, I wouldn't miss it, but having it in the language doesn't bother me. It can be useful in obfu and poetry, anyway.

- tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://481628]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-26 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found