Refactoring Replace Parameter with Explicit Methods

Problem

A method is split into parts, each of which is run depending on the value of a parameter.

Solution

Extract the individual parts of the method into their own methods and call them instead of the original method.
Before
void setValue(String name, int value) {
  if (name.equals("height")) {
    height = value;
    return;
  }
  if (name.equals("width")) {
    width = value;
    return;
  }
  Assert.shouldNeverReachHere();
}
After
void setHeight(int arg) {
  height = arg;
}
void setWidth(int arg) {
  width = arg;
}
Before
void SetValue(string name, int value) 
{
  if (name.Equals("height")) 
  {
    height = value;
    return;
  }
  if (name.Equals("width")) 
  {
    width = value;
    return;
  }
  Assert.Fail();
}
After
void SetHeight(int arg) 
{
  height = arg;
}
void SetWidth(int arg) 
{
  width = arg;
}
Before
function setValue($name, $value) {
  if ($name == "height") {
    $this->height = $value;
    return;
  }
  if ($name == "width")) {
    $this->width = $value;
    return;
  }
  assert("Should never reach here");
}
After
function setHeight($arg) {
  $this->height = $arg;
}
function setWidth($arg) {
  $this->width = $arg;
}
Before
def output(self, type):
    if name == "banner"
        # Print the banner.
        # ...
    if name == "info"
        # Print the info.
        # ...
After
def outputBanner(self):
    # Print the banner.
    # ...

def outputInfo(self):
    # Print the info.
    # ...

Why Refactor

A method containing parameter-dependent variants has grown massive. Non-trivial code is run in each branch and new variants are added very rarely.

Benefits

  • Improves code readability. It is much easier to understand the purpose of startEngine() than setValue("engineEnabled", true).

When Not to Use

  • Do not replace a parameter with explicit methods if a method is rarely changed and new variants are not added inside it.

How to Refactor

  1. For each variant of the method, create a separate method. Run these methods based on the value of a parameter in the main method.

  2. Find all places where the original method is called. In these places, place a call for one of the new parameter-dependent variants.

  3. When no calls to the original method remain, delete it.

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