Previous Page
Next Page

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.

    Previous Page
    Next Page