Also known as

Refactoring Decompose Conditional

Problem

You have a complex conditional (`if-then`/`else` or `switch`).

Solution

Decompose the complicated parts of the conditional into separate methods: the condition, `then` and `else`.
Before
if (date.before(SUMMER_START) || date.after(SUMMER_END)) {
  charge = quantity * winterRate + winterServiceCharge;
}
else {
  charge = quantity * summerRate;
}
After
if (notSummer(date)) {
  charge = winterCharge(quantity);
}
else {
  charge = summerCharge(quantity);
}
Before
if (date < SUMMER_START || date > SUMMER_END) 
{
  charge = quantity * winterRate + winterServiceCharge;
}
else 
{
  charge = quantity * summerRate;
}
After
if (NotSummer(date)) 
{
  charge = WinterCharge(quantity);
}
else 
{
  charge = SummerCharge(quantity);
}
Before
if ($date->before(SUMMER_START) || $date->after(SUMMER_END)) {
  $charge = $quantity * $winterRate + $winterServiceCharge;
}
else {
  $charge = $quantity * $summerRate;
}
After
if (notSummer($date)) {
  $charge = winterCharge($quantity);
}
else {
  $charge = summerCharge($quantity);
}
Before
if date.before(SUMMER_START) or date.after(SUMMER_END):
    charge = quantity * winterRate + winterServiceCharge
else:
    charge = quantity * summerRate
After
if notSummer(date):
    charge = winterCharge(quantity)
else:
    charge = summerCharge(quantity)

Why Refactor

The longer a piece of code is, the harder it is to understand. Things become even more hard to understand when the code is filled with conditions:

  • While you are busy figuring out what the code in the then block does, you forget what the relevant condition was.
  • While you are busy parsing else, you forget what the code in then does.

Benefits

  • By extracting conditional code to clearly named methods, you make life easier for the person who will be maintaining the code later (such as you, two months from now!).

  • This refactoring technique is also applicable for short expressions in conditions. The string isSalaryDay() is much prettier and more descriptive than code for comparing dates.

How to Refactor

  1. Extract the conditional to a separate method via Extract Method.

  2. Repeat the process for the then and else blocks.

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