Cover of Code Complete by Steve McConnell - Business and Economics Book

From "Code Complete"

Author: Steve McConnell
Publisher: Pearson Education
Year: 2004
Category: Computers

🎧 Free Preview Complete

You've listened to your free 10-minute preview.
Sign up free to continue listening to the full summary.

🎧 Listen to Summary

Free 10-min Preview
0:00
Speed:
10:00 free remaining
Chapter 7: Software Craftsmanship
Key Insight 3 from this chapter

Common Code Layout Styles and Their Applications

Key Insight

Code layout primarily concerns the formatting of blocks of statements governed by control structures (e.g., 'if-then-endif' in Visual Basic, or '{}' in C++ and Java). In languages like Visual Basic, 'Pure Blocks' are inherent, as each control construct has a distinct beginning and an explicit terminator (e.g., 'If...End If', 'While...Wend', 'Select Case...End Select'). This structure naturally promotes straightforward indentation, where the closing keyword aligns with the opening one, creating clear visual boundaries and consistent logical representation.

For languages like C++ and Java that use general-purpose block delimiters (braces '{}') uncoupled from control structures, two main styles emerge. 'Emulating Pure Blocks' treats the opening brace as an extension of the control construct, typically placing it on the same line as the control statement (e.g., 'if (condition) { statement; }'). The closing brace then aligns with the control statement. This style, standard in Java and common in C++, effectively reveals logical structure, is consistently applicable, and maintainable. Alternatively, 'Using begin-end Pairs (Braces) to Designate Block Boundaries' views braces as independent markers that define the block. Here, the opening brace usually starts on a new line, aligned with the control statement, with the closing brace also aligned below the block, visually emphasizing the block's extent.

'Endline Layout' involves indenting code to align blocks or parameters under the keyword that initiated them. While sometimes aesthetically appealing in simple cases (e.g., aligning 'Then', 'Else', 'End If' in an 'If' block), this style quickly becomes problematic with complex conditional expressions, providing misleading logical clues. It is difficult to apply consistently and poses maintenance challenges, as changes to an initial line's length necessitate re-indentation of subsequent aligned statements. For language-specific practices, Visual Basic encourages pure-block indentation, Java standardizes on pure-block emulation, while C++ allows either pure-block emulation or begin-end block boundaries, as studies show no statistically significant difference in understandability between these two in C++. Regardless of the chosen style, consistent application across a project or team yields the most significant benefits for readability and maintainability.

📚 Continue Your Learning Journey — No Payment Required

Access the complete Code Complete summary with audio narration, key takeaways, and actionable insights from Steve McConnell.