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 4: Statements
Key Insight 1 from this chapter

Organizing Straight-Line Code and Managing Dependencies

Key Insight

Organizing straight-line code primarily involves managing statement dependencies, which dictates execution order and impacts code quality, correctness, readability, and maintainability. Statements with explicit dependencies, like reading data before calculating results (e.g., 'data = ReadData(); results = CalculateResultsFromData(data); PrintResults(results);'), must be executed sequentially. However, dependencies can be less obvious, such as quarterly revenue relying on monthly calculations, or even hidden when a routine like 'ComputeMarketingExpense()' implicitly initializes shared class member variables without clear parameters, making it difficult to discern order from code alone.

To clarify dependencies, developers should organize code so they are obvious, for instance, by creating an 'InitializeExpenseData()' routine instead of embedding initialization within another function. Routine names should accurately reflect all actions performed; if 'ComputeMarketingExpense()' also initializes data, a name like 'ComputeMarketingExpenseAndInitializeMemberData()' is more descriptive. Explicitly passing data via routine parameters, such as 'InitializeExpenseData(expenseData)' and subsequent 'ComputeMarketingExpense(expenseData)', or converting routines to functions that return updated data (e.g., 'expenseData = InitializeExpenseData(expenseData)'), clearly signals order importance. If dependencies remain unclear, documenting them with comments is crucial for maintainable code. For critical sections, status variables and error-handling, like 'isExpenseDataInitialized' flags checked by dependent functions, can verify sequential execution, though this adds complexity and potential for secondary errors.

When statement order does not matter due to a lack of execution-order dependencies, the Principle of Proximity should guide organization: related actions should be kept together. Code should read from top to bottom, minimizing mental jumps; for example, grouping all operations related to 'marketingData' sequentially before moving to 'salesData' improves readability and localization, and can hint at possible routine decomposition. Statements can be related by operating on the same data, performing similar tasks, or having logical ties. A visual test involves drawing non-overlapping boxes around related code sections; overlapping boxes indicate poor organization. Strongly related, independent groups of statements should be refactored into their own routines to enhance modularity and clarity.

📚 Continue Your Learning Journey — No Payment Required

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