Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: if variants

by DrHyde (Prior)
on Oct 20, 2004 at 09:21 UTC ( #400788=note: print w/replies, xml ) Need Help??

in reply to if variants

One thing that's worth mentioning is that while this:
if($foo == 7) { print "foo is 7\n"; } else { print "foo is not 7\n"; }
is acceptable, this:
unless($foo != 7) { print "foo is 7\n"; } else { print "foo is not 7\n"; }
is not. Sure, perl will accept it (I consider this to be a bug in perl), and it'll run, but anyone writing it should be beaten savagely. It's VERY hard to read.

Replies are listed 'Best First'.
Re^2: if variants
by apotheon (Deacon) on Oct 20, 2004 at 18:57 UTC
    I edited to include some brief mention of the undesirability of the != operator there. Thanks for the suggestion.

    - apotheon
    CopyWrite Chad Perrin

      It's worth noting, by the way, that using $foo != 7 as the control statement in an if statement is generally a Bad Idea. The unless statement is designed to cover that need, and using the != operator there just makes the code harder to read later when it must be maintained.

      I second DrHyde (Re^3: if variants). It's definately not bad and neither is it a maintenance trouble. That whole sentence above should be removed from your text. It's just plain wrong.

      The unless keyword is made to make you write code more like a natural language. It's not there to take !='s place in if expressions. They can both coexist happily and be mixed however you please.

      Why the combination of unless and else is shunned by many is because it's a sort of double negation.

      if (X) { ... } else { ... }
      can be read
      if (X) { ... } if (not X) { ... }
      "unless X" can be read "if not X". Combining this gives you
      unless (X) { ... } else { ... } if (not X) { ... } if (not not X) { ... }
      which just doesn't read well. Logically it's not strange but it's a funny and inappreciated way of expressing oneself.

      My own (rather inconsistent) style is to use unless for exceptional behaviour, and if for expected/wanted behaviour. When using unless I usually want something, but something might stop me from doing that. With if I express that I perhaps want something, perhaps not.

      print ... unless $quiet; # I want to print. exit if $done; # I might want to exit here. exit unless $stay; # I want to exit here, but apparently # something is holding me back.
      The examples can perhaps be better, and I'm not consistant myself in my use, but that's give you an idea of how I tend to use them.


      See perltoc if you don't know which perldoc to read!
      Read argumentation in its context!

        Thanks for the clarification. Mea culpa.

        - apotheon
        CopyWrite Chad Perrin
      Noooooo! != is fine. It's unless ... else that is hideously broken!
        I can understand avoiding the != in an if statement, since using unless covers that functionality. I don't know why unless . . . else would be "bad", however. Explain?

        - apotheon
        CopyWrite Chad Perrin

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://400788]
[choroba]: The problem was bigger, as the test tried to call a method that didn't exist anymore
[marinersk]: :: ducking ::
[choroba]: because, someone renamed the method, but didn't notice it was used in the test, as the test was skipped
[marinersk]: Well, if the method doesn't exist, it would be hard to pass the test.
[choroba]: later, someone removed the new method, as all its usage places were safe, but didn't notice the test still used the old name
[choroba]: fortunately, it wasn't that hard to replace the method and fix a few remaining failures due to the changes we did to the codebase over the years
[marinersk]: choroba Sounds like a process improvement opportunity; tests may not all need to be run, but they should all be compiled with perl -c before check-in/promotion happens.
[choroba]: so, now I have the test, so I can start making changes in the code. Back to the original ticket, yay!
[marinersk]: I definitely love it when my $current_taask=& taskPop(); if ($current_task eq $original_task) { } is true.
[marinersk]: s/taask/task/g;

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2017-05-25 15:14 GMT
Find Nodes?
    Voting Booth?