Replace Parameter with Method Call

Before a method call, a second method is run and its result is sent back to the first method as an argument. But the parameter value could have been obtained inside the method being called.

Instead of passing the value through a parameter, place the value-getting code inside the method.

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.