- Guides
- Refactoring
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
Smells: bloat
Code smells in code that has grown so much it can no longer be understood at a glance.
About this group0 of 5 readSmells: rigidity
Code smells where one change forces many others, far away: divergent change, shotgun surgery, and the refactorings that cure them.
About this group0 of 3 readSmells: 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 group0 of 5 readSmells: dead weight
Code smells in code that adds nothing: duplicated, unused or speculative.
About this group0 of 6 read- 1Duplicate Code: When the Same Logic Lives in Several Places5 min
- 2Dead Code: Unused Functions, Branches and Flags7 min
- 3Speculative Generality: Abstractions Built for Needs That Never Came7 min
- 4Lazy Class: When a Class Doesn't Earn Its Place6 min
- 5Data Class: Fields and Getters With the Logic Living Elsewhere7 min
- 6Explaining Comments: When a Comment Covers for Unclear Code7 min
Smells: misused abstractions
Code smells where inheritance, types or conditionals are used against the grain.
About this group0 of 4 readTechniques: composing functions
Refactorings that split long functions into named pieces, or undo pieces that add nothing.
About this group0 of 7 read- 1Extract function: give a fragment a name4 min
- 2Inline Function: Remove Indirection That Adds Nothing6 min
- 3Extract Variable: Give a Complex Expression a Name7 min
- 4Split Loop: One Loop Doing Two Jobs Becomes Two Loops7 min
- 5Slide Statements: Group Related Code Before You Extract It7 min
- 6Replace Loop with Pipeline: Turn Loops into map, filter and reduce7 min
- 7Replace Function with Command: Turn a Long Function into an Object9 min
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 group0 of 5 read- 1Introduce Parameter Object: Bundle Parameters That Travel Together7 min
- 2Preserve Whole Object: Pass the Object, Not Its Fields7 min
- 3Replace Temp with Query: Turn Local Variables into Methods7 min
- 4Split Variable: One Name per Purpose6 min
- 5Replace Primitive with Object: Give Rule-Bearing Values a Type7 min
Techniques: simplifying calls
Refactorings that make a function's signature say what it does and make it easy to use correctly.
About this group0 of 4 readTechniques: simplifying conditionals
Refactorings that make conditional logic readable at a glance: guard clauses, decomposed conditions and polymorphism instead of switches.
About this group0 of 5 read- 1Decompose Conditional: Name the Condition and Each Branch8 min
- 2Consolidate Conditional: Merge Checks That Share a Result6 min
- 3Replace Nested Conditional with Guard Clauses6 min
- 4Replace Conditional with Polymorphism: One Class per Case7 min
- 5Introduce Special Case: Replace Repeated Null Checks With an Object9 min
Techniques: moving features
Refactorings that move each responsibility to the class or module where it belongs.
About this group0 of 5 read- 1Move Function: Put Behaviour Next to the Data It Uses6 min
- 2Extract Class: Split a Class That Does Two Jobs7 min
- 3Inline Class: Fold a Class That No Longer Earns Its Keep6 min
- 4Hide Delegate: Stop Callers Reaching Through an Object's Internals6 min
- 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.