Документация
HTML CSS PHP PERL другое

Section 6.2.  Postfix Selectors

 
Previous
Table of Contents
Next

6.2. Postfix Selectors

Reserve postfix if for flow-of-control statements.

The only exception to the previous guideline comes about because of another of the principles enumerated at the start of this chapter: "make it very easy to detect variations in the flow of control".

Such variations come about when a next, last, redo, return, goto, die, croak, or throw [*] occurs in the middle of other code. These commands break up the orderly downward flow of execution, so it is critical that they are easy to detect. And, although they are usually associated with some conditional test, the fact that they may potentially interrupt the control flow is more important than the conditions under which they are doing so.

[*] See Chapter 13 for an explanation of the throw( ) method.

Hence it's better to place the next, last, redo, return, goto, die, croak, and throw keywords in the most prominent position on their code line. In other words, they should appear as far to the left as possible (as discussed in the "Keep Left" sidebar in Chapter 2).

If an if is being used solely to determine whether to invoke a flow-control statement, use the postfix form. Don't hide the action over on the right:

    sub find_anomolous_sample_in {
        my ($samples_ref) = @_;

        MEASUREMENT:
        for my $measurement (@{$samples_ref}) {
            if ($measurement < 0) { last MEASUREMENT; }

            my $floor = int($measurement);
            if ($floor == $measurement) { next MEASUREMENT; }

            my $allowed_inaccuracy = scale($EPSILON, $floor);
            if ($measurement-$floor > $allowed_inaccuracy) {
                return $measurement;
            }
        }
        return;
    }

Be "up front" about it:


    sub find_anomolous_sample_in {
        my ($samples_ref) = @_;

        MEASUREMENT:
        for my $measurement (@{$samples_ref}) {
            last MEASUREMENT if $measurement < 0;

            my $floor = int($measurement);
            next MEASUREMENT if $floor == $measurement;

            my $allowed_inaccuracy = scale($EPSILON, $floor);
            return $measurement
                if $measurement-$floor > $allowed_inaccuracy;
        }
        return;
    }

    Previous
    Table of Contents
    Next
    © 2000- NIV