Refactor: Simplify Branching
Reduce conditional complexity for better readability.
Techniques
- Guard Clauses (Early Return)
Replace deep nesting with flat guards.
- Replace Conditionals with Polymorphism
Use protocol dispatch instead of type checking.
- Replace Nested Conditionals with Table
Use lookup tables for multi-dimensional conditions.
- Decompose Compound Conditionals
Extract complex conditions to named variables or methods.
- Replace Flag Arguments
Create separate methods instead of boolean flags.
Warning Signs
-
Nesting > 3 levels deep
-
More than 3 else-if branches
-
Switch with > 5 cases
-
Condition spans multiple lines