- Refactoring
- Techniques: simplifying conditionals
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.
- Decompose Conditional: Name the Condition and Each BranchTurn a dense if/else into a readable decision by extracting the condition, the then-branch and the else-branch into well-named functions.8 min readStart here
- Consolidate Conditional: Merge Checks That Share a ResultLearn how to merge separate conditional checks that return the same result into one well-named condition, with TypeScript, Python and PHP examples.6 min read
- Replace Nested Conditional with Guard ClausesFlatten deeply nested if/else logic by handling special cases first and returning early, so the main path of a function reads straight down.6 min read
- Replace Conditional with Polymorphism: One Class per CaseLearn how to replace a switch on a type code with one subclass per case, in small steps that keep your tests green, with TypeScript, Python and PHP examples.7 min read
- Introduce Special Case: Replace Repeated Null Checks With an ObjectLearn how to replace scattered null checks with a special-case object that carries sensible defaults, with step-by-step code in TypeScript, Python and PHP.9 min read
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.
More groups in Refactoring
- Smells: bloatCode smells in code that has grown so much it can no longer be understood at a glance.See the 5 guides
- Smells: rigidityCode smells where one change forces many others, far away: divergent change, shotgun surgery, and the refactorings that cure them.See the 3 guides
- Smells: couplingCode smells in pieces that know too much about each other: feature envy, inappropriate intimacy, message chains, and how to decouple them.See the 5 guides
- Smells: dead weightCode smells in code that adds nothing: duplicated, unused or speculative.See the 6 guides
- Smells: misused abstractionsCode smells where inheritance, types or conditionals are used against the grain.See the 4 guides
- Techniques: composing functionsRefactorings that split long functions into named pieces, or undo pieces that add nothing.See the 7 guides
- Techniques: organizing dataRefactorings that give data a name, a type and a single purpose: replace primitives and magic numbers, encapsulate fields and collections.See the 5 guides
- Techniques: simplifying callsRefactorings that make a function's signature say what it does and make it easy to use correctly.See the 4 guides
- Techniques: moving featuresRefactorings that move each responsibility to the class or module where it belongs.See the 5 guides