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 2 from this chapter

Inline Function

Key Insight

Inline Function serves as the inverse of Extract Function, primarily used to reduce unnecessary indirection in code. This refactoring is beneficial when a function's body provides as much clarity as its name, or after its logic has been simplified to that extent, rendering the function call redundant. Eliminating such needless indirection can streamline code and improve flow. It is also employed as a preparatory step for refactoring badly organized groups of functions, where they are first inlined into a larger block before being re-extracted into a more desirable structure. This helps flush out useful abstractions from mere simple delegations.

The process of inlining requires first confirming that the function is not polymorphic, meaning it is not overridden by subclasses, which would prevent safe inlining. Subsequently, all call sites of the target function must be identified. At each call site, the function call is replaced directly with the body of the function. It is important to perform testing after each replacement to ensure that no regressions are introduced. The inlining process can be completed gradually, addressing tricky parts incrementally as opportunities arise. Once all calls have been replaced and tested, the original function definition can be safely removed from the codebase.

While straightforward for simple cases, caution is advised for complex scenarios involving recursion, multiple return points, or inlining methods into other objects without proper accessors. Such complexities typically indicate that 'Inline Function' is not the appropriate refactoring. Examples illustrate simple replacements where an expression from the called function directly substitutes the call, or slightly more involved adjustments when argument names differ. For more complex code blocks, the strategy involves inlining one line at a time, potentially using 'Move Statements to Callers', to carefully fit the code into its new context, with continuous testing and readiness to revert if issues arise.

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