Refactoring is a disciplined technique for restructuring existing code by applying small, behavior-preserving transformations to improve its internal structure without changing external behavior. Popularized by Martin Fowler's catalog, refactoring addresses code smells β symptoms of deeper problems like duplicated logic, unclear naming, or tangled dependencies β with systematic, named techniques that each tackle a specific design weakness. The practice relies on comprehensive automated tests as a safety net, enabling developers to continuously improve code readability, maintainability, and adaptability. Understanding when and how to apply each refactoring, along with recognizing the smells that trigger them, transforms chaotic codebases into clean, flexible systems without risking functionality.
What This Cheat Sheet Covers
This topic spans 14 focused tables and 84 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Foundational Code Extraction
| Technique | Example | Description |
|---|---|---|
total = calcTotal();instead of inline calculation | β’ Moves a code fragment into a new method with a descriptive name β’ most fundamental refactoring that reduces duplication, improves readability, and enables reuse. | |
const isEligible = age > 18 && hasID;if (isEligible) ... | β’ Replaces a complex expression with a named variable β’ clarifies what the expression represents, making conditionals and calculations self-documenting. | |
Move phoneNumber, areaCode fieldsβ new PhoneNumber class | β’ Splits a class doing too much work into two β’ key move when a class has distinct responsibilities or when field groups are always used together. |