Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Useless <c>unless</c>

by davidrw (Prior)
on Aug 05, 2005 at 22:07 UTC ( [id://481378]=note: print w/replies, xml ) Need Help??


in reply to Useless unless

a if b; c otherwise;
doesn't the ternary operator accomplish this already with b ? a : c; ? (And second example is just (my $o = b) ? a : b;)

Do you have any examples of uses of unless that have created traps?

For better or for worse, I personally actually use ! $x, !$x, and unless $x ... depends on how it reads in context, and how it fits in the coding style of whatever code i'm modifying..

Update: First one could also be: (b && a) || c; But it requires that a only ever return a true value.

Replies are listed 'Best First'.
Re^2: Useless <c>unless</c>
by GrandFather (Saint) on Aug 05, 2005 at 23:17 UTC

    Can't use the ternary operator where the if/unless/otherwise is being used as a modifier.

    Any negated statement requires one more step of mental processing and it seems to take an extra step with unless to realise that it is negating the condition. The trap is simply that it is unless rather than if.


    Perl is Huffman encoded by design.
      Can't use the ternary operator where the if/unless/otherwise is being used as a modifier.
      modifier? do you mean assignment like my $x = $A ? $B : $C; ?
      The trap is simply that it is unless rather than if.
      How is that different a statement being !$x rather than $x and just missing/misreading the 'bang'?
Re^2: Useless <c>unless</c>
by tilly (Archbishop) on Aug 07, 2005 at 06:05 UTC
    My issue with unless is that humans do not do De Morgan's Laws on the fly while debugging. We do it fine while developing and reading software, but when we're trying to figure out what happened, and we have several variables juggled around, we do very poorly at figuring out whether we hit an unless.

    I didn't believe this until I personally encountered it.

    Therefore as soon as I have 2 variables and in a non-trivial combination, I write an if.

      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://481378]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-03-28 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found