Refactoring Ruby: Replace Temp with Query

jonlunsford

Jon Lunsford

Posted on October 26, 2023

Refactoring Ruby: Replace Temp with Query

Let’s look at another simple yet powerful refactoring method, Replace Temp with Query. You can use this when you have a temporary variable holding the result of an expression. I will reach for this when temporary variables add too much to a method’s size/complexity, or when the result of an expression needs to be used in other methods. It's a fantastic next step after the last approach we look at: Extract Variable

Let’s use Replace Temp with Query in the Movie#total_price method.

Before:

class Movie
  TAX_PERCENTAGE = 0.5

  def initialize(price:, discount_percentage: 0)
    @price = price
    @discount_percentage = discount_percentage
  end

  def total_price
    # Temp variables holding expressions
    tax = price * TAX_PERCENTAGE
    discount = price * (discount_percentage / 100.0)

    price + tax - discount
  end

  private

  attr_reader :price, :discount_percentage
end
Enter fullscreen mode Exit fullscreen mode

After:

class Movie
  TAX_PERCENTAGE = 0.5

  def initialize(price:, discount_percentage: 0)
    @price = price
    @discount_percentage = discount_percentage
  end

  def total_price
    # Temp variables replaced with query methods
    price + tax - discount
  end

  private

  attr_reader :price, :discount_percentage

  def tax
    price * TAX_PERCENTAGE
  end

  def discount
    price * (discount_percentage / 100.0)
  end
Enter fullscreen mode Exit fullscreen mode

The method before refactoring looks like:

def total_price
  # Temp variables holding expressions
  tax = price * TAX_PERCENTAGE
  discount = price * (discount_percentage / 100.0)
  price + tax - discount
end
Enter fullscreen mode Exit fullscreen mode

The method after refactoring looks like:

def total_price
  # Temp variables replaced with query methods
  price + tax - discount
end
Enter fullscreen mode Exit fullscreen mode

With the temp variables extracted to private query methods, there’s even less to grok when understanding the role of total_price.

Let’s run through the exact steps to take when applying Replace Temp with Query:

  1. Extract the expression into a private method. Try to use a private method as we don’t want to change the public API of an object if we don’t have to.

  2. Replace all references to the temp with the method name. Additionally, use the new query methods in any other places that need them.

Reach for this when you need to share the result of an expression or to reduce the size/complexity of a method.

💖 💪 🙅 🚩
jonlunsford
Jon Lunsford

Posted on October 26, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Refactoring - Ruby
undefined Refactoring - Ruby

March 12, 2024

Refactoring Ruby: Replace Temp with Query
refactoring Refactoring Ruby: Replace Temp with Query

October 26, 2023

Refactoring Ruby: Extract Method
refactoring Refactoring Ruby: Extract Method

October 10, 2023

Refactoring Ruby: Extract Variable
refactoring Refactoring Ruby: Extract Variable

September 12, 2023