Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by BrowserUk (Patriarch)
on Sep 29, 2006 at 16:46 UTC ( [id://575570]=note: print w/replies, xml ) Need Help??


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

You made Hue-Bond's point.

No. You made my point.

An overly complex conditional is an overly complex conditional regardless of whether it appears as part of an if statement or an unless statement. Or as you more succinctly put it:

The statement was hard to understand without some major transformations.

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.
  • Comment on Re^4: Is this the most elegant way to code directory lookup?

Replies are listed 'Best First'.
Re^5: Is this the most elegant way to code directory lookup?
by Hue-Bond (Priest) on Sep 29, 2006 at 17:20 UTC
    An overly complex conditional is an overly complex conditional regardless of whether it appears as part of an if statement or an unless statement.

    I think I should've formulated the question in other words: "Quick, when will we enter into the block?".

    • if ($bar !~ /bar/ and ($baz =~ /qux/ or not defined $undef)): If this doesn't match that, and at least one of the other conditions.
    • unless ($bar =~ /bar/ or ($baz !~ /qux/ and defined $undef)): Err, well... applying DeMorgan... err... when there's no match... and...

    Or, put it yet another way: if we know the values of $bar, $baz, and $undef, is it easier to walk the if or the unless conditional?

    --
    David Serrano

Re^5: Is this the most elegant way to code directory lookup?
by ikegami (Patriarch) on Sep 29, 2006 at 17:31 UTC

    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.)

      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.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found