in reply to Re^2: Conditional Elimination
in thread Conditional Elimination

No, I think using a sub makes it easier to read. Making those tests that will return early will not slow it down as much as putting them in random order. It's just something to keep in mind when writing the sub. But the most important thing is to put the condition in a sub so that it's easier to understand.

Replies are listed 'Best First'.
Re^4: Conditional Elimination
by JavaFan (Canon) on Aug 31, 2011 at 00:44 UTC
    I'd say that correctness is far more important than speed when it comes to assigning order.
    But the most important thing is to put the condition in a sub so that it's easier to understand.
    How does that magic work? You're still have the same tests. Personally, I find:
    if (bunch of clauses) { code }
    easier to understand then
    if (pointer-where-to-find-clauses) { code }
    specially when it comes to better understanding of the clauses.
      The magic works by assigning a name to the operation - names are very powerful magic.

      Personally, I find:

      if ($zipcode == 12345 || $zipcode == 55344 || $zipcode == ...) { code }
      much harder to understand than
      if (is_valid_zipcode($zipcode)) { code }
      specially when it comes to better understanding of what the code is meant to accomplish.
        Hmmm, the former clearer says to me "ah, he's checking whether the zip code is one of a given list", while the latter tells me he's validating a zipcode. I'd would find it surprising if is_valid_zipcode(10012) returns false, while it doesn't surprise me in your first snippet.

        Names are so much easier to get wrong, or be ambiguous.

      The magic works for the same reason you would break a long sequence of statements in subroutines.