Perl Best Practices [Electronic resources]

Damian Conway

نسخه متنی -صفحه : 317/ 82
نمايش فراداده

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.