Refactoring Replace Parameter with Method Call


Calling a query method and passing its results as the parameters of another method, while that method could call the query directly.


Instead of passing the value through a parameter, try placing a query call inside the method body.
int basePrice = quantity * itemPrice;
double seasonDiscount = this.getSeasonalDiscount();
double fees = this.getFees();
double finalPrice = discountedPrice(basePrice, seasonDiscount, fees);
int basePrice = quantity * itemPrice;
double finalPrice = discountedPrice(basePrice);
int basePrice = quantity * itemPrice;
double seasonDiscount = this.GetSeasonalDiscount();
double fees = this.GetFees();
double finalPrice = DiscountedPrice(basePrice, seasonDiscount, fees);
int basePrice = quantity * itemPrice;
double finalPrice = DiscountedPrice(basePrice);
$basePrice = $this->quantity * $this->itemPrice;
$seasonDiscount = $this->getSeasonalDiscount();
$fees = $this->getFees();
$finalPrice = $this->discountedPrice($basePrice, $seasonDiscount, $fees);
$basePrice = $this->quantity * $this->itemPrice;
$finalPrice = $this->discountedPrice($basePrice);
basePrice = quantity * itemPrice
seasonalDiscount = this.getSeasonalDiscount()
fees = this.getFees()
finalPrice = discountedPrice(basePrice, seasonalDiscount, fees)
basePrice = quantity * itemPrice
finalPrice = discountedPrice(basePrice)

Why Refactor

A long list of parameters is hard to understand. In addition, calls to such methods often resemble a series of cascades, with winding and exhilarating value calculations that are hard to navigate yet have to be passed to the method. So if a parameter value can be calculated with the help of a method, do this inside the method itself and get rid of the parameter.


  • We get rid of unneeded parameters and simplify method calls. Such parameters are often created not for the project as it is now, but with an eye for future needs that may never come.


  • You may need the parameter tomorrow for other needs... making you rewrite the method.

How to Refactor

  1. Make sure that the value-getting code does not use parameters from the current method, since they will be unavailable from inside another method. If so, moving the code is not possible.

  2. If the relevant code is more complicated than a single method or function call, use Extract Method to isolate this code in a new method and make the call simple.

  3. In the code of the main method, replace all references to the parameter being replaced with calls to the method that gets the value.

  4. Use Remove Parameter to eliminate the now-unused parameter.

Tired of reading?

No wonder, there are 7 hours worth of the text on this website.

Try out something different. We've just launched the interactive learning course on refactoring. It has more content and much more fun than a boring text.

Learn more...

Live Example

First time here? No worries!

Everything is quite simple. This example is like video (but much cooler):

  1. After pressing the Play button, you will see floating messages, which will guide you through the example. You can proceed by clicking on them.
  2. You can fast-forward or return to previous steps by pressing arrow buttons on the left.
  3. Also, you can take a look at the code diff window, which will show what has changed in code during the example ()
  4. To check the code for errors, you can press "Compile and test" button ()