Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: A new idiom -or- I Hate Unless

by grinder (Bishop)
on Aug 22, 2004 at 22:36 UTC ( [id://384989]=note: print w/replies, xml ) Need Help??


in reply to A new idiom -or- I Hate Unless

Hear, hear. As a statement modifier I have no problem with unless, but I cringe when I see it used as the block form. I find it interesting to note that perlsyn doesn't even bother mentioning it, so I'm surprised that it pops up so often.

The main reason for my dislike is the puzzlement factor that a newcomer to Perl will have when encountering the statement. It's obvious to me, because I'm familiar with Perl, but I can also see that someone coming from another language could have considerable difficulty realising that unless is the opposite of if. I can imagine such a person wondering if there isn't some subtle distinction if using it instead of simply negating the if. So for me it falls into the Too Clever For Its Own Good category. Doubly so when the code becomes unless( $cond ) {...} else {...}. That's just perverse.

My own approach to avoiding block unless statements is to simply use if( not $cond ) {...}. Works for me.

My main rule is to arrange to have the if part be the shortest, and the else part the longest, so that at the beginning of the else it's easy to see the conditional that led you here. Locality is more important.

- another intruder with the mooring of the heat of the Perl

Replies are listed 'Best First'.
Re^2: A new idiom -or- I Hate Unless
by chromatic (Archbishop) on Aug 23, 2004 at 00:36 UTC
    The main reason for my dislike is the puzzlement factor that a newcomer to Perl will have when encountering the statement.

    Why should that be a concern? Do you use sequential alphanumeric identifiers for variables and subroutines to avoid the puzzlement that newcomers to English will have when encountering idiomatic words?

    I agree with you about avoiding unless {} else {}, but that's because it's confusing to native English speakers as well. I don't believe that avoiding idioms -- where they'd otherwise be appropraite -- in deference to hypothetical unexperienced maintenance programmers is a good idea.

    Update: I misread the parent post the first time, with which I agree. Sorry about that.

      I agree with you about avoiding unless {} else {}, but that's because it's confusing to native English speakers as well.
      Is it? I'm not native (Russian) and the whole concept of unless is alien to me. But I got over it, and even got used to it and even became fond of it (due to Perl) :) Never thought that unless {} else {} does not look like real English. Thanks for education!

        I think the 'else' following an 'unless' in english is often spelled 'otherwise'. As in,

        unless its raining i will go to the swimming pool, otherwise ill watch + a movie.

        'else' in the context wouldn't be wrong, but it wouldnt be particularly idiomatic.

        All IMO of course.


        ---
        demerphq

          First they ignore you, then they laugh at you, then they fight you, then you win.
          -- Gandhi


Re^2: A new idiom -or- I Hate Unless
by ambrus (Abbot) on Aug 23, 2004 at 09:11 UTC

    Anyway, I still think unless is still much nicer than its slang equivalent, !if .

    I have no problem with unless, but I cringe when I see it used as the block form. I find it interesting to note that perlsyn doesn't even bother mentioning it, so I'm surprised that it pops up so often.

    It is mentioned in perlsyn (5.8.2), it's just easy to miss it:

    from perlsyn(1): The following compound statements may be used to control flow: if (EXPR) BLOCK if (EXPR) BLOCK else BLOCK if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK ... The "if" statement is straightforward. Because BLOCKs are alwa +ys bounded by curly brackets, there is never any ambiguity about which "if" an "els +e" goes with. If you use "unless" in place of "if", the sense of the test is rev +ersed.

    Btw, why isn't there an elsunless statement like if : unless = while : until = elsif : elsunless?

    (Update: fixed /code tag)

      Btw, why isn't there an elsunless statement like if : unless = while : until = elsif : elsunless?

      LW had something to say on that in a recent post on p6lang:

      .... And there's a new C<elsunless> in Perl 6--except that it's spelled C<elsif not>. C<:-)>

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-19 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found