Skip to content

Techniques: simplifying conditionals

Refactorings that make conditional logic readable at a glance: guard clauses, decomposed conditions and polymorphism instead of switches.

5 guides36 min in totalRefactoring

The guides, in order

Read them top to bottom: each one builds on the ones before. The ones you've read carry a check.

About this group

What you'll find
Decompose conditional, consolidate conditional, replace nested conditional with guard clauses, replace conditional with polymorphism and introduce special case.
It's for you if…
your code has nested ifs, switches on types or repeated null checks.
How each guide works
Smell guides cover the signs, an example, why it happens, the treatment with its refactorings, what you gain and when it's not worth fixing. Technique guides show the problem, the code before and after, the steps, which smells it removes, its benefits and its cautions.