in reply to Re^2: 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. The statement was hard to understand without some major transformations. For example, the following is clearer:
if ( $bar !~ /bar/ and ( $baz =~ /qux/ or not defined $undef ) )

But let's not stop there. Reduce the number of things the reader has to keep in mind by breaking the statement into two.

if( $bar !~ /bar/ ) if ( $baz =~ /qux/ or not defined $undef )

Replies are listed 'Best First'.
Re^4: Is this the most elegant way to code directory lookup?
by BrowserUk (Patriarch) on Sep 29, 2006 at 16:46 UTC
    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.
      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

      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.