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 (now in its 2nd edition, updated for JavaScript and modern practices), 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 15 focused tables and 108 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Foundational Code Extraction
The single most impactful group of refactorings: breaking large, unwieldy code into smaller, named units. Every other technique in this catalog depends on being able to extract cleanly — master these first.
| Technique | Example | Description |
|---|---|---|
function printOwing(invoice) { printBanner(); printDetails(invoice);} | • Moves a code fragment into a new function with a descriptive name • most fundamental refactoring — separates intent (name) from implementation (body). Also called Extract Method (1st-edition name). | |
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. |