Also known as

Refactoring Consolidate Conditional Expression

Problem

You have multiple conditionals that lead to the same result or action.

Solution

Consolidate all these conditionals in a single expression.
Before
double disabilityAmount() {
  if (seniority < 2) {
    return 0;
  }
  if (monthsDisabled > 12) {
    return 0;
  }
  if (isPartTime) {
    return 0;
  }
  // compute the disability amount
  //...
}
After
double disabilityAmount() {
  if (isNotEligableForDisability()) {
    return 0;
  }
  // compute the disability amount
  //...
}
Before
double DisabilityAmount() 
{
  if (seniority < 2) 
  {
    return 0;
  }
  if (monthsDisabled > 12) 
  {
    return 0;
  }
  if (isPartTime) 
  {
    return 0;
  }
  // compute the disability amount
  //...
}
After
double DisabilityAmount() 
{
  if (IsNotEligableForDisability()) 
  {
    return 0;
  }
  // compute the disability amount
  //...
}
Before
function disabilityAmount() {
  if ($this->seniority < 2) return 0;
  if ($this->monthsDisabled > 12) return 0;
  if ($this->isPartTime) return 0;
  // compute the disability amount
  ...
After
function disabilityAmount() {
  if ($this->isNotEligableForDisability()) return 0;
  // compute the disability amount
  ...
Before
def disabilityAmount():
    if seniority < 2:
        return 0
    if monthsDisabled > 12:
        return 0
    if isPartTime:
        return 0
    # compute the disability amount
    #...
After
def disabilityAmount():
    if isNotEligableForDisability():
        return 0
    # compute the disability amount
    #...

Why Refactor

Your code contains many alternating operators that perform identical actions. It is not clear why the operators are split up.

The main purpose of consolidation is to extract the conditional to a separate method for greater clarity.

Benefits

  • Eliminates duplicate control flow code. Combining multiple conditionals that have the same "destination" helps to show that you are doing only one complicated check leading to one action.

  • By consolidating all operators, you can now isolate this complex expression in a new method with a name that explains the conditional's purpose.

How to Refactor

Before refactoring, make sure that the conditionals do not have any "side effects" or otherwise modify something, instead of simply returning values. Side effects may be hiding in the code executed inside the operator itself, such as when something is added to a variable based on the results of a conditional.

  1. Consolidate the conditionals in a single expression by using and and or. As a general rule when consolidating:
  • Nested conditionals are joined using and.
  • Consecutive conditionals are joined with or.
  1. Perform Extract Method on the operator conditions and give the method a name that reflects the expression's purpose.

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