From "Refactoring"
🎧 Listen to Summary
Free 10-min PreviewCode Smells Related to Code Structure and Readability
Key Insight
One significant code smell involves 'Mysterious Names,' where unclear naming for functions, modules, variables, and classes obscures their purpose and usage, hindering code comprehension. Renaming is a frequent refactoring (e.g., Change Function Declaration 124, Rename Variable 137, Rename Field 244), and difficulty in finding a good name often points to deeper design flaws requiring simplification. Similarly, 'Duplicated Code,' where identical structures appear in multiple places, increases reading effort and the risk of inconsistent changes; unifying these instances with refactorings like Extract Function (106), Slide Statements (223), or Pull Up Method (350) improves maintainability.
The 'Long Function' smell arises from functions that are excessively long, making them difficult to understand. Programs benefit from short functions that support explanation, sharing, and choice, with modern languages largely removing the overhead of calls. The key to small function clarity is good naming; often, the function's body needs no inspection if its name is clear. Developers should aggressively decompose functions, creating new ones whenever a comment would otherwise be needed, even for a single line, prioritizing semantic intent over mere length. Extract Function (106) is the primary solution, with Replace Temp with Query (178), Introduce Parameter Object (140), and Preserve Whole Object (319) used to reduce parameters, and Replace Function with Command (337) as a 'heavy artillery' option. Comments, conditionals (Decompose Conditional 260, Replace Conditional with Polymorphism 272), and loops (Split Loop 227) are common indicators for extraction.
Other readability and structure concerns include 'Long Parameter List,' 'Lazy Element,' and 'Speculative Generality.' Long parameter lists confuse; they can be reduced by querying existing parameters (Replace Parameter with Query 324), passing whole objects (Preserve Whole Object 319), combining related parameters into objects (Introduce Parameter Object 140), removing flag arguments (Remove Flag Argument 314), or consolidating with classes (Combine Functions into Class 144). 'Lazy Elements' are program components that provide little value despite their structure; they should be removed via Inline Function (115), Inline Class (186), or Collapse Hierarchy (380). 'Speculative Generality' involves adding unnecessary hooks for anticipated future features; this unused machinery complicates code and should be eliminated by collapsing hierarchies (Collapse Hierarchy 380), inlining delegation (Inline Function 115, Inline Class 186), or removing unused parameters (Change Function Declaration 124). Code solely used by test cases also signals speculative generality and warrants removal of both test case and code (Remove Dead Code 237).
📚 Continue Your Learning Journey — No Payment Required
Access the complete Refactoring summary with audio narration, key takeaways, and actionable insights from Martin Fowler, Kent Beck.