header

Replace Temp With Query

You are using temporary variable to hold the result of an expression. Extract the expression into a method. Replace all the references to the temp with the new method. The new method can then be used then in other methods.

The problem with temps is that they are temporary and local. Because they can be seen only in the context of the method in wich they are used, temps tend to encourage longer methods, because that’s the only way you can reach the temp. By replacing the temp with query method, any method in the class can get at the information. That helps a lot in coming up with cleaner code for the class.

Consider the following class:

package com.josdem.refactoring;

public class DiscountCalculator {

   public Double getTotal(Integer quantity, Integer itemPrice) {
    Integer basePrice = quantity * itemPrice;

    Double = discountFactor;
    if (basePrice > 1000) {
      discountFactor * 0.95;
    } else {
      discountFactor * 0.98;
    }
    return basePrice * discountFactor;

}

First I replace the right-hand side of the assignment:

Integer basePrice = getBasePrice();

Then I replace all references to that temp with the expression.

public Double getTotal() {
  Double = discountFactor;
  if (getBasePrice() > 1000) {
    discountFactor * 0.95;
  } else {
   discountFactor * 0.98;
  }
  return getBasePrice() * discountFactor;
}

private Integer getBasePrice() {
  return quantity * itemPrice;
}

With that done I can extract discountFactor in a similar way.

private Integer getDiscountFactor() {
  return (getBasePrice() > 1000) ? 0.95 : 0.98;
}

See how it would have been difficult to extract discountFactor if I had not replaced basePrice with a query. The getPrice method ends up as follow.

private Double getTotal() {
  return getBasePrice() * getDiscountFactor();
}

When I’m working on a method, I like to replace temp with query to get rid of any temporary variables that I can remove. If the temp is used for many things, I use split temporary variable first to make the temp easier to replace. ~ Martin Fowler.

To download the project:

git clone https://github.com/josdem/refactoring.git
git fetch
git checkout feature/replace-temp-with-query-setup
git checkout feature/replace-temp-with-query-complete

To run the project:

gradle test

Read more, returning to the main article. Refactoring

comments powered by Disqus