Refactoring Ruby: Replace Temp with Query

def total_price
# Temp variables holding expressions
tax = price * TAX_PERCENTAGE
discount = price * (discount_percentage / 100.0)
price + tax - discount
end
def total_price
# Temp variables replaced with query methods
price + tax - discount
end
  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.

--

--

Engineer @convertkit, programming enthusiast, father, and musician

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jon Lunsford

Jon Lunsford

Engineer @convertkit, programming enthusiast, father, and musician