2.16. Breaking Long Lines
When an expression at the end of a statement gets too long, it's common practice to break that expression after an operator and then continue the expression on the following line, indenting it one level. Like so: push @steps, $steps[-1] + $radial_velocity * $elapsed_time + $orbital_velocity * ($phase + $phase_shift) - $DRAG_COEFF * $altitude; The rationale is that the operator that remains at the end of the line acts like a continuation marker, indicating that the expression continues on the following line. Using the operator as a continuation marker seems like an excellent idea, but there's a serious problem with it: people rarely look at the right edge of code. Most of the semantic hints in a programsuch as keywordsappear on the left side of that code. More importantly, the structural cues for understanding codefor example, indentingare predominantly on the left as well (see the upcoming "Keep Left" sidebar). This means that indenting the continued lines of the expression actually gives a false impression of the underlying structure, a misperception that the eye must travel all the way to the right margin to correct.
A cleaner solution is to break long lines before an operator. That approach ensures that each line of the continued expression will start with an operator, which is unusual in Perl code. That way, as the reader's eye scans down the left margin of the code, it's immediately obvious that an indented line is merely the continuation of the previous line, because it starts with an operator. The indenting of the second and subsequent lines of the expression is also critical. Continued lines should not simply be indented to the next indentation level. Instead, they should be indented to the starting column of the expression to which they belong. That is, instead of: push @steps, $steps[-1] + $radial_velocity * $elapsed_time + $orbital_velocity * ($phase + $phase_shift) - $DRAG_COEFF * $altitude ; you should write: push @steps, $steps[-1] + $radial_velocity * $elapsed_time + $orbital_velocity * ($phase + $phase_shift) - $DRAG_COEFF * $altitude ; This style of layout has the added advantage that it keeps the two arguments of the push visually separated in the horizontal, and thereby makes them easier to distinguish. When a broken expression is continued over multiple lines, it is good practice to place the terminating semicolon on a separate line, indented to the same column as the start of the continued expression. As the reader's eye scans down through the leading operators on each line, encountering a semicolon instead makes it very clear that the continued expression is now complete. |