Decompose Conditional


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


Decompose the complicated parts of the conditional into separate methods: the condition, then and else.

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.


  • 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.