Section 2.18. Breaking by Precedence


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".



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