Cover of Refactoring by Martin Fowler, Kent Beck - Business and Economics Book

From "Refactoring"

Author: Martin Fowler, Kent Beck
Publisher: Addison-Wesley Professional
Year: 1999
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 6: A First Set of Refactorings
Key Insight 5 from this chapter

Change Function Declaration

Key Insight

Change Function Declaration is a critical refactoring that manages the 'joints' of software systems, referring to how functions are named and how their parameters are defined. These declarations dictate how program parts fit together, and well-designed joints facilitate system expansion and understanding, while poorly designed ones lead to constant difficulties in comprehension and modification. A primary aspect is the function name, which should clearly convey its purpose without requiring inspection of its implementation; thus, it is imperative to promptly rename confusing functions, often by evolving a descriptive comment into a precise name.

Similarly, parameters are crucial as they define a function's context and applicability. Modifying parameters can significantly expand a function's utility, for example, by replacing a specific object parameter with a more general data item like a telephone number for formatting. This also helps reduce coupling between modules, lessening the cognitive burden during changes. Deciding on the 'right' parameters involves balancing concerns like coupling and encapsulation, as there is no single universally correct answer, especially over time. Therefore, familiarity with 'Change Function Declaration' is essential for evolving code with improving understanding of system needs. When only renaming, this refactoring is often referred to as 'Rename Function' for clarity.

The refactoring offers two sets of mechanics: simple and migration. Simple mechanics involve directly changing the declaration and updating all callers, suitable for cases with few callers or robust automated tools, ensuring to separate changes like renaming and parameter addition, testing after each. Migration mechanics are used for more complex scenarios, such as numerous callers, awkward access, polymorphic methods, or complicated changes. This approach involves extracting the function's body into a new, potentially temporarily named, function, adding new parameters via simple mechanics, inlining the old function, and then renaming the new function back. For published APIs, this strategy allows for deprecation of the old function, enabling gradual client migration before final removal, ensuring backwards compatibility during transition.

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