Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: Is this the most elegant way to code directory lookup?

by ikegami (Patriarch)
on Sep 29, 2006 at 17:31 UTC ( [id://575579]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Is this the most elegant way to code directory lookup?
in thread Is this the most elegant way to code directory lookup?

Would you also change

unless (not $var) { ... }

into

if (not not $var) { ... }

The major transformation I did was simply eliminating unless's implicit not. The whole point of if being simpler is that it doesn't have unless's implicit not. Eliminating the not is what makes it simpler, not making the not explicit. (Although tye will point out there is value in making the not explicit over using unless.)

Replies are listed 'Best First'.
Re^6: Is this the most elegant way to code directory lookup?
by BrowserUk (Patriarch) on Sep 29, 2006 at 19:13 UTC

    No. But then I wouldn't code unless( not $var ){ in the first place, and I wouldn't transform a legitimate use of unless into if( not ... ).

    But I would happily code unless( $var ) { in preference to if( not $var ){.

    And if the logic of my code dictates that using a guard statement to quit early is preferable to an extended if statement, then I prefer the OP's:

    unless( -d or $_ eq '.' or $_ eq '.. ) { die ... }; which I read as

    Unless it's a directory and it's neither '.' nor '..' get the hell outta here.

    which I think reads easily, and far better than either

    if( not( -d or $_ eq '.' or $_ eq '.. ) ) { die ... }; which I'd have to read as

    If( not( it's a directory and it's neither '.' nor '..' ) ) get the hell outer here.

    or

    if( !-d or ( -d and ( $_ ne '.' or $_ ne '..' ) ) ) { die ... }; and read as

    If( it's not a directory or ( it is a directory but it's either '.' or '..' ) ) get the hell outta here.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Your last expression is not equivalent to the others.
      if( !-d or ( -d and ( $_ ne '.' or $_ ne '..' ) ) )
      means
      unless( -d and $_ eq '.' and $_ eq '..' )
      It should have been
      if ( !-d and $_ ne '.' and $_ ne '..' ) { ... }
      "If it's not a directory, not '.' and not '..', then ..."

      I can't understand
      "Unless it's a directory and it's neither '.' nor '..' get the hell outta here."
      I don't even know what it would be if it were grammatically correct English. Would it be
      "Unless it's a directory, and unless it's neither '.' nor '..', get the hell outta here."
      or
      "Unless it's a directory, and if it's neither '.' nor '..', get the hell outta here."?
      The answer is kinda moot since I don't understand either version.

      By far, the three easiest for me to understand are:

      • "If it's not a directory, not '.' and not '..', then ..."
        if ( !-d && $_ ne '.' && $_ ne '..' ) { ... }
        (Because the tight binding makes it easier to process mentally.)

      • "If it's neither a directory, '.', nor '..', then ..."
        if ( none -d, $_ eq '.', $_ eq '..' ) { ... }
        (Because of the resemblance to English and French.)

      • ... unless it's a directory, '.', or '..'
        ... unless ( -d || $_ eq '.' || $_ eq '..');
        (Because of the resemblance to English and French.)

        I admit, I made a boo boo on c&ping my third attempt--and that's the point. I would not make that mistake in my code, because I wouldn't be trying to avoid the obvious solution--unless as used by the OP.

        I started to respond when you only had two "easiest to understands". The addition of the last one rendered that arguement moot because you are now offering the OPs version--which is (IMO) the simplest and best.

        Your first option is bad because it tests all three sub-conditions every time, whereas the original short circuits.

        Your second best is the worst.

        I have no idea what none is or where it comes from, but I assume you are having to:

        1. Load a module.
        2. Invoke a function -- with callback.
        3. And still you are testing all sub-conditions instead of short circuiting.

        And all of that to avoid using a perfect serviceable built in keyword.

        ... unless it's a directory, '.', or '..' ... unless ( -d || $_ eq '.' || $_ eq '..'); (Because of the resemblance to English and French.)

        This is perfect because it exactly reflects the thought process that lies behind the code.

        "Unless they give me something that is a directory, or if they give me '.' or '..'; die."

        And that's the point I've been trying to make. Sometimes the emphasis is on the negative and unless exactly lends itself to this.

        This is why a mother will often choose to say:

        Stay here unless I call you.

        In preference to

        Come to me when I call you.

        Because she wishes to emphasis "Stay here...", not "come when I call". And so it is in programming. There are occasions when the logic requires a particular emphasis, and whilst DeMorgan may allow you to invert the logic of the condition successfully--if applied correctly--it will leave you with a statement with the wrong emphasis.

        And this is where all the arguements for avoiding unless fall down. They continually say that DeMorgan is hard (it is); and so you are likely to make mistakes when converting a if into an unless--but they seem to miss that they same is true going the other way. Converting something that is a natural unless to an if in order to avoid using unless is stupid because it is subject to the exact same set of potential errors and mistakes as going the other way.

        My position is that you use if, when it reflects the natural thought process, or the structural requirements of the code. And you use unless when that reflects the natural thought process or the structural needs of the code.

        Ie. Don't use one, when the other is better.

        And dogmatic "thou shalt nots" do everyone a disservice because they force the use of DeMorgan and all the mistakes that come with it in one set of the two.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (None)
    As of 2024-04-25 00:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found