Refactoring Add Parameter


A method does not have enough data to perform certain actions.


Create a new parameter to pass the necessary data.
Add Parameter - Before
Add Parameter - After

Why Refactor

You need to make changes to a method and these changes require adding information or data that was previously not available to the method.


  • The choice here is between adding a new parameter and adding a new private field that contains the data needed by the method. A field is preferable when you need some occasional or frequently changing data for which there is no point in holding it in an object all of the time. In this case, a new parameter will be a better fit than a private field and the refactoring will pay off. Otherwise, add a private field and fill it with the necessary data before calling the method.


  • Adding a new parameter is always easier than removing it, which is why parameter lists frequently balloon to grotesque sizes. This smell is known as the Long Parameter List.

  • If you need to add a new parameter, sometimes this means that your class does not contain the necessary data or the existing parameters do not contain the necessary related data. In both cases, the best solution is to consider moving data to the main class or to other classes whose objects are already accessible from inside the method.

How to Refactor

  1. See whether the method is defined in a superclass or subclass. If the method is present in them, you will need to repeat all the steps in these classes as well.

  2. The following step is critical for keeping your program functional during the refactoring process. Create a new method by copying the old one and add the necessary parameter to it. Replace the code for the old method with a call to the new method. You can plug in any value to the new parameter (such as null for objects or a zero for numbers).

  3. Find all references to the old method and replace them with references to the new method.

  4. Delete the old method. Deletion is not possible if the old method is part of the public interface. If that is the case, mark the old method as deprecated.

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 ()