mobile menu icon

"Split or condition in if" refactoring

Published by Manuel Rivero on 08/10/2022

Refactoring, Code Smells


We’d like to document a refactoring that we usually teach to teams we work with: the Split or condition in if refactoring.

Split or condition in if.

Motivation.

Sometimes we have nested conditionals that are branching depending on the same information. When an outer if condition contains an or which combines boolean expressions that check that information, splitting the or in the outer if may lead to opportunities to remove the rechecks in inner branching. This happens because having that or in the outer if forces us to check again the same information in an inner if condition.

Let’s see an example: