Replace Temp with Query.
You place the result of an expression in a local variable for later use in your code.
Move the entire expression to a separate method and return the result from it. Query the method instead of using a variable. Incorporate the new method in other methods, if necessary.
##Example
double calculateTotal() {
double basePrice = quantity * itemPrice;
if (basePrice > 1000) {
return basePrice * 0.95;
}
else {
return basePrice * 0.98;
}
}double calculateTotal() {
if (basePrice() > 1000) {
return basePrice() * 0.95;
}
else {
return basePrice() * 0.98;
}
}
double basePrice() {
return quantity * itemPrice;
}##Why Refactor
This refactoring can lay the groundwork for applying Extract Method for a portion of a very long method.
The same expression may sometimes be found in other methods as well, which is one reason to consider creating a common method.
##Benefits
-
Code readability. It’s much easier to understand the purpose of the method
getTax()than the lineorderPrice() * 0.2. -
Slimmer code via deduplication, if the line being replaced is used in multiple methods.
##How to Refactor
-
Make sure that a value is assigned to the variable once and only once within the method. If not, use Split Temporary Variable to ensure that the variable will be used only to store the result of your expression.
-
Use Extract Method to place the expression of interest in a new method. Make sure that this method only returns a value and doesn’t change the state of the object. If the method affects the visible state of the object, use Separate Query from Modifier.
-
Replace the variable with a query to your new method.