Previous Page
Next Page

2.18. Breaking by Precedence

Always break a long expression at the operator of the lowest possible precedence.

As the examples in the previous two guidelines show, when breaking an expression across several lines, each line should be broken before a low-precedence operator. Breaking at operators of higher precedence encourages the unwary reader to misunderstand the computation that the expression performs. For example, the following layout might surreptitiously suggest that the additions and subtractions happen before the multiplications:

    push @steps, $steps[-1] + $radial_velocity
                 * $elapsed_time + $orbital_velocity
                 * ($phase + $phase_shift) - $DRAG_COEFF
                 * $altitude

If you're forced to break on an operator of less-than-minimal precedence, indent the broken line one additional level relative to the start of the expression, like so:

    push @steps, $steps[-1]
                 + $radial_velocity * $elapsed_time
                 + $orbital_velocity
                     * ($phase + $phase_shift)
                 - $DRAG_COEFF * $altitude

This strategy has the effect of keeping the subexpressions of the higher precedence operation visually "together".

    Previous Page
    Next Page