Skip to content

Refactoring

Learn to spot problem code and fix it with small, safe changes. There are two kinds of guides: code smells and refactoring techniques.

49 guides10 groups320 min in total

  1. Smells: bloat

    Code smells in code that has grown so much it can no longer be understood at a glance.

    About this group
    1. 1Long method: when one function does the whole job5 min
    2. 2Large Class7 min
    3. 3Long Parameter List6 min
    4. 4Primitive Obsession: when strings and numbers fake domain concepts6 min
    5. 5Data Clumps6 min
  2. Smells: rigidity

    Code smells where one change forces many others, far away: divergent change, shotgun surgery, and the refactorings that cure them.

    About this group
    1. 1Divergent Change: One Module, Many Unrelated Reasons to Change6 min
    2. 2Shotgun Surgery: When One Change Means Edits in Many Files7 min
    3. 3Parallel Inheritance Hierarchies6 min
  3. Smells: coupling

    Code smells in pieces that know too much about each other: feature envy, inappropriate intimacy, message chains, and how to decouple them.

    About this group
    1. 1Feature Envy: when a method lives in the wrong class6 min
    2. 2Inappropriate Intimacy: Classes That Know Too Much About Each Other7 min
    3. 3Message Chains6 min
    4. 4Middle Man7 min
    5. 5Global Mutable State6 min
  4. Smells: dead weight

    Code smells in code that adds nothing: duplicated, unused or speculative.

    About this group
    1. 1Duplicate Code: When the Same Logic Lives in Several Places5 min
    2. 2Dead Code: Unused Functions, Branches and Flags7 min
    3. 3Speculative Generality: Abstractions Built for Needs That Never Came7 min
    4. 4Lazy Class: When a Class Doesn't Earn Its Place6 min
    5. 5Data Class: Fields and Getters With the Logic Living Elsewhere7 min
    6. 6Explaining Comments: When a Comment Covers for Unclear Code7 min
  5. Smells: misused abstractions

    Code smells where inheritance, types or conditionals are used against the grain.

    About this group
    1. 1Repeated Switches: the same type-code switch scattered across a codebase6 min
    2. 2Refused Bequest: Subclasses That Reject What They Inherit6 min
    3. 3Temporary Field: State That Only Lives During One Method Call6 min
    4. 4Flag Arguments6 min
  6. Techniques: composing functions

    Refactorings that split long functions into named pieces, or undo pieces that add nothing.

    About this group
    1. 1Extract function: give a fragment a name4 min
    2. 2Inline Function: Remove Indirection That Adds Nothing6 min
    3. 3Extract Variable: Give a Complex Expression a Name7 min
    4. 4Split Loop: One Loop Doing Two Jobs Becomes Two Loops7 min
    5. 5Slide Statements: Group Related Code Before You Extract It7 min
    6. 6Replace Loop with Pipeline: Turn Loops into map, filter and reduce7 min
    7. 7Replace Function with Command: Turn a Long Function into an Object9 min
  7. Techniques: organizing data

    Refactorings that give data a name, a type and a single purpose: replace primitives and magic numbers, encapsulate fields and collections.

    About this group
    1. 1Introduce Parameter Object: Bundle Parameters That Travel Together7 min
    2. 2Preserve Whole Object: Pass the Object, Not Its Fields7 min
    3. 3Replace Temp with Query: Turn Local Variables into Methods7 min
    4. 4Split Variable: One Name per Purpose6 min
    5. 5Replace Primitive with Object: Give Rule-Bearing Values a Type7 min
  8. Techniques: simplifying calls

    Refactorings that make a function's signature say what it does and make it easy to use correctly.

    About this group
    1. 1Rename: Make Variables, Functions and Classes Say What They Are7 min
    2. 2Change Function Declaration: Fix a Signature Without Breaking Callers6 min
    3. 3Remove Flag Argument: Replace a Boolean with Explicit Functions7 min
    4. 4Separate Query from Modifier: Split Reads from State Changes7 min
  9. Techniques: simplifying conditionals

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

    About this group
    1. 1Decompose Conditional: Name the Condition and Each Branch8 min
    2. 2Consolidate Conditional: Merge Checks That Share a Result6 min
    3. 3Replace Nested Conditional with Guard Clauses6 min
    4. 4Replace Conditional with Polymorphism: One Class per Case7 min
    5. 5Introduce Special Case: Replace Repeated Null Checks With an Object9 min
  10. Techniques: moving features

    Refactorings that move each responsibility to the class or module where it belongs.

    About this group
    1. 1Move Function: Put Behaviour Next to the Data It Uses6 min
    2. 2Extract Class: Split a Class That Does Two Jobs7 min
    3. 3Inline Class: Fold a Class That No Longer Earns Its Keep6 min
    4. 4Hide Delegate: Stop Callers Reaching Through an Object's Internals6 min
    5. 5Remove Middle Man: Let Callers Talk to the Real Object7 min

About this section

What these guides are
A code smell is a sign that something in the code is going to cause trouble; a refactoring is a change that improves its structure without changing what it does. In interviews they come up in code review exercises, refactoring tests and when you talk about how you maintain a project. Naming both the problem and the technique that fixes it shows good judgement.
How they are organised
Smells are grouped by the kind of damage they do, such as bloat or coupling, and techniques by what they change, such as functions, data or conditionals. Each smell links to the refactorings that cure it, and each refactoring to the smells it removes.
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.
Where to start
Start with the long method smell and the extract function technique: they are the most common pair. From there, follow the links between smells and techniques.