Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

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


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

Quick, what does this do?
unless ($bar =~ /bar/ or $baz !~ /qux/ and defined $undef)

Exactly the same as

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

There. So much clearer now...not.


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.

Replies are listed 'Best First'.
Re^3: Is this the most elegant way to code directory lookup?
by wfsp (Abbot) on Sep 29, 2006 at 15:09 UTC
    BrowserUk++

    A not, a regex, an or and an and. Not only not clear but almost impossible for me to get my head round.

    I resort to extreme lengths to try and get it straight.

    if( not ( ( $bar =~ /bar/ or ( $baz !~ /qux/ and defined $undef ) ) ) )
    But I find that almost always these constructions are the result of a poor algorithm. As the old saying goes "get your algorithm right and the code writes itself". While we're here, that is closely followed by "don't try to code yourself out of a problem, start again".

    It works for me! :-)

Re^3: Is this the most elegant way to code directory lookup? (!unless)
by tye (Sage) on Sep 29, 2006 at 15:41 UTC
    There. So much clearer now...not.

    Your ability to make something less clear doesn't negate everyone else's ability to make something more clear.

    if( ! -d && $_ ne "." && $_ ne ".." ) {

    Yes, that prevents a noticeable mental pause that the "unless" version induced. [ Even better, it avoids the mental tension between how "unless" is usually used in English vs. how it is used in Perl and the fact that the unexplicit "not" implied by "unless" is too easy to mentally drop. ]

    - tye        

      So the condition to which I was referring:

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

      can be tranformed into

      if( ! -d && $_ ne "." && $_ ne ".." ) {

      I guess I'll have to review DeMorgan's law (Tye's version) plus some of this.


      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.

        No, of course not. I was going back to the original code (obviously). I'm sure you know how to transform the made-up code in a similar manner (especially since you used the term for it) even though you didn't do it originally. But I didn't find that made-up code much good in illustrating whether something is easy to understand (since the code has no real point so how can I say I've understood it).

        This is why I usually respond to you anonymously, because you seem to have a very hard time examining what I say instead of the fact that I am the one who said it. It certainly seems to work out better that way, in my experience, and is my best guess at explaining why you felt that "appeal to ridicule" was appropriate. I certainly had no intent to ridicule. But I'll not spend more time trying to convince you of this as previous such attempts have resulted in you feeling more ridiculed. I'll just continue to mostly avoid attaching my name to replies to you in order to avoid this particular problem. Cheers.

        - tye        

Re^3: Is this the most elegant way to code directory lookup?
by ikegami (Patriarch) on Sep 29, 2006 at 16:14 UTC
    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 )
      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.)

Log In?
Username:
Password:

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

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

    No recent polls found