Section 6.1. If Blocks


6.1. If Blocks

Use block if, not postfix if.

One of the most effective ways to make decisions and their consequences stand out is to avoid using the postfix form of if. For example, it's easier to detect the decision and consequences in:

      if (defined $measurement) {         $sum += $measurement;     }

than in:

     $sum += $measurement if defined $measurement;

Moreover, postfix tests don't scale well as the consequences increase. For example:

     $sum += $measurement     and $count++     and next SAMPLE         if defined $measurement;

and:

     do {         $sum += $measurement;         $count++;         next SAMPLE;     } if defined $measurement;

are both much harder to comprehend than:

      if (defined $measurement) {         $sum += $measurement;         $count++;         next SAMPLE;     }

So always use the block form of if.



Perl Best Practices
Perl Best Practices
ISBN: 0596001738
EAN: 2147483647
Year: 2004
Pages: 350
Authors: Damian Conway

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net