Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^3: Multiple Conditional Statements

by BillKSmith (Vicar)
on Sep 11, 2013 at 19:59 UTC ( #1053558=note: print w/replies, xml ) Need Help??

in reply to Re^2: Multiple Conditional Statements
in thread Multiple Conditional Statements

Of course I agree with all your arguments. All I meant to imply was that it directly compares the variables in question (not copies) and with very little source code. I find it very easy to understand. In general, that should trump issues of speed, memory, or namespace.

Replies are listed 'Best First'.
Re^4: Multiple Conditional Statements
by BrowserUk (Pope) on Sep 11, 2013 at 21:23 UTC
    I find it very easy to understand. In general, that should trump issues of speed, memory, or namespace.

    Interesting justifiction. Personally, I prefer solutions that are:

    1. Clean.

      Don't create persistent, named temporaries to clutter, conflict and confuse the purpose of the code.

    2. Clear.

      Don't introduce unnecessary (and counterproductive) levels of indirection in the name of premature optimisation (avoiding copies).

      It is more expensive to takes references then dereference those references; than to make copies of simple, small scalar values:

      ($a,$b,$c,$d,$e) = 1 .. 5; cmpthese -1,{ a=>q[ my @a = ($a,$b,$c,$d,$e); ++$_ for @a;], b=>q[my @a = \($a,$b,$c,$d,$e); ++$$_ for @a; ] };; Rate b a b 525790/s -- -23% a 685872/s 30% --
    3. Concise.

      The goal of this snippet if to cross-compare a few variables with the only interest being a single boolean truth.

      Spreading that across half a dozen lines, adding named temporaries to the mix; adding a module, a callback and nested loops; and obscure indirections to derive a simple boolean does the very opposite of making things easy to understand.

    4. Simple.

      Simple means when the algorithm calls for:

      if( <some condition> ) { ## do somthing }

      the implementation (code) should reflect that. Not detract from the algorithm by introducing unneeded complexity.

    5. Efficient.

      looping to perform all 15 comparisons when the first or the second can resolve the boolean condition is just flagrant waste.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      There is a maximum of only ten comparisons. By your criteria, the OP had it right in the first place. The ternary operator, with the format from the book "Perl Best Practices", cleans it slightly.
      my $match = $RB1 == $RB2 ? 1 : $RB1 == $WR1 ? 1 : $RB1 == $WR2 ? 1 : $RB1 == $TE1 ? 1 : $RB2 == $WR1 ? 1 : $RB2 == $WR2 ? 1 : $RB2 == $TE1 ? 1 : $WR1 == $WR2 ? 1 : $WR1 == $TE1 ? 1 : $WR2 == $TE1 ? 1 : 0 ;
      UPDATE: Or even
      my $match = $RB1 == $RB2 || $RB1 == $WR1 || $RB1 == $WR2 || $RB +1 == $TE1 || $RB2 == $WR1 || $RB2 == $WR2 || $RB2 == $TE1 || $WR1 == $WR2 || $WR1 == $TE1 || $WR2 == $TE1 ;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1053558]
[Lotus1]: I was here

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2018-06-18 17:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.