You have a complex conditional (
Decompose the complicated parts of the conditional into separate methods: the condition,
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
thenblock does, you forget what the relevant condition was.
- While you are busy parsing
else, you forget what the code in
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
Extract the conditional to a separate method via Extract Method.
Repeat the process for the