Skip to content
Home » Replace Temp with Query

Replace Temp with Query

Refactoring code involves altering the structure of code to make it more understandable and maintainable without changing its observable behavior. One such strategy of refactoring is known as “Replace Temp with Query”.

What is Replace Temp with Query?

Replace Temp with Query is a refactoring technique that suggests the replacement of a local variable (temporary variable or temp) that holds the result of an expression with a method (query) that returns the result of the same expression. The essence of this refactoring is to remove the local variable and extract its creation logic into a separate method.

When To Use Replace Temp With Query?

Consider using Replace Temp with Query in the following scenarios:

  1. Complex calculation in a method: If a method contains complex calculations and the results are stored in temporary variables, it might be helpful to use this refactoring technique.
  2. Multiple references to a temporary variable: When the value of a temporary variable is referenced multiple times in a method, but not changed after its initial assignment, Replace Temp with Query can make the code more readable.
  3. Reduce side effects: Temporary variables can be a source of bugs, especially if their values are modified in the course of a method. By using a query method, you can reduce the potential for these sorts of side effects.

Examples of Replace Temp with Query

Java

Let’s consider the following Java code:

public class Order {
    private int quantity;
    private double itemPrice;

    public double calculateTotal() {
        double basePrice = quantity * itemPrice;
        double discount;
        if (basePrice > 1000) {
            discount = basePrice * 0.05;
        } else {
            discount = basePrice * 0.03;
        }
        return basePrice - discount;
    }
}
Java

Here, the temporary variables basePrice and discount can be replaced with queries:

public class Order {
    private int quantity;
    private double itemPrice;

    public double calculateTotal() {
        return getBasePrice() - getDiscount();
    }

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

    private double getDiscount() {
        return getBasePrice() > 1000 ? getBasePrice() * 0.05 : getBasePrice() * 0.03;
    }
}
Java

Python

Let’s see a Python example:

class Order:
    def __init__(self, quantity, item_price):
        self.quantity = quantity
        self.item_price = item_price

    def calculate_total(self):
        base_price = self.quantity * self.item_price
        if base_price > 1000:
            discount = base_price * 0.05
        else:
            discount = base_price * 0.03
        return base_price - discount
Python

We can refactor this Python code by replacing temp variables with query methods:

class Order:
    def __init__(self, quantity, item_price):
        self.quantity = quantity
        self.item_price = item_price

    def calculate_total(self):
        return self.get_base_price() - self.get_discount()

    def get_base_price(self):
        return self.quantity * self.item_price

    def get_discount(self):
        return self.get_base_price() * 0.05 if self.get_base_price() > 1000 else self.get_base_price() * 0.03
Python

Conclusion

Replace Temp with Query refactoring allows you to make your code clearer and easier to understand, as well as more robust against potential bugs. It replaces temporary variables with query methods,

1 thought on “Replace Temp with Query”

  1. Pingback: Inline Temp - ctrl+shift

Comments are closed.