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

Replace Temp with Query

Key Insight

Temporary variables are often used to store calculation results for later reference within a function, providing clarity and avoiding redundant calculations. However, converting these temporary variables into dedicated functions, or 'queries', offers substantial benefits, particularly when refactoring large functions. By transforming a variable into a function, the need to pass that variable as a parameter to extracted sub-functions is eliminated, simplifying the extraction process. This also creates clearer boundaries between different logical parts of the code, aiding in the identification and prevention of awkward dependencies and side effects.

Beyond improving function decomposition, using queries instead of temporary variables helps to avoid duplicating calculation logic across multiple functions. If the same calculation is performed in different places, converting it into a single query function centralizes that logic, making it easier to maintain and ensuring consistency. This refactoring is most effective when applied within a class context, as the class provides a shared scope for the extracted methods, mitigating the risk of introducing too many parameters in top-level functions. Crucially, 'Replace Temp with Query' is suitable only for temporary variables that are calculated once and subsequently only read, and whose calculation consistently yields the same result without side effects; variables used as snapshots, like 'oldAddress', are not good candidates.

The mechanics involve verifying the variable's value is determined completely before its use and that its calculation is consistent across invocations. Making the variable `const` is a good initial step to confirm it's read-only. The next step is to extract the right-hand side of the variable's assignment into a new function. After ensuring this new function is free of side effects, the temporary variable itself is inlined. For example, in an `Order.price` method, `basePrice` and `discountFactor` are initially temporary variables. `basePrice`'s calculation (`this._quantity * this._item.price`) is extracted into a `getBasePrice()` method. Similarly, the entire calculation for `discountFactor`, including its conditional logic (`if (this.basePrice > 1000) discountFactor -= 0.03;`), is extracted into a `getDiscountFactor()` method. Once extracted, the original temporary variables are inlined, simplifying the `price` method to `return this.basePrice * this.discountFactor;`.

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