04 / 05
Improving code in small steps while preserving its behaviour.
You want to add a new feature to a working component but the code does not suit it. Either you force the code as it is, or you tidy it up first. This guide covers the second: refactoring. The examples are frontend code, but the ideas hold in any language.
For Martin Fowler, refactoring is restructuring existing code's internals without changing its externally visible behaviour. It is made of small, low-risk steps, and the system stays working after every step. Chaining those steps can bring about a large restructuring too.1
Two consequences follow:
Refactoring is not rewriting. You do not tear the code down in one go; you change it step by step.
A step that changes the behaviour does not count as refactoring.1
The definition and securing it with a test are covered in practice in the Restructuring a project guide. We are not repeating it here.
Fowler's refactoring catalogue names and lists the steps one by one.2 The three you will use most often in the frontend:
// Before
let a = height * width
// After: Rename Variable
let area = height * width// Before
const charge = isBetween(date, plan.summerStart, plan.summerEnd)
? quantity * plan.summerRate
: quantity * plan.regularRate + plan.regularServiceCharge
// After: Decompose Conditional
const charge = isSummer(date, plan)
? summerCharge(quantity, plan)
: regularCharge(quantity, plan)In the second example the code no longer says how it calculates but what it checks and what it does. After every step, run the application or the tests; the behaviour has to stay the same.
When you see a smell. For Fowler, a code smell is a sign on the code's surface: it usually corresponds to a deeper problem. But a smell is not bad on its own. Some long methods are fine.3 So "this component is long" is not on its own a sufficient reason to refactor. Look at why it is a problem.
Before a new feature. In Kent Beck's words: make the change easy, then make the easy change. Fowler describes that as preparatory refactoring: if the existing structure makes adding the new feature hard, you prepare the structure first.4
// The goal: adding a discount badge to ProductCard.
// The price calculation is scattered inside the JSX.
// 1. Refactor first: move the calculation into a function; the tests still have to pass
function getPrice(product: Product) {
return product.price * (1 - product.discountPercent / 100)
}
// 2. Then the feature: the badge is now easy to add
{product.discountPercent > 0 && <Badge>{product.discountPercent}% off</Badge>}During a refactoring, the tests are how you know the behaviour stayed the same. For Kent C. Dodds, you should very rarely have to change a test when you refactor the code. Tests that test a component's internal details break during a refactoring even when the code is healthy, and make the job harder.5 Write tests that look at what the user sees. How that is done is in the Vitest guide.
Caution
Refactoring is not always the job to do.
Not every smell is a problem. A structure that is long but understandable can stay as it is.3
Do not mix a new feature with refactoring. A step that changes the behaviour is not refactoring.1 This guide's advice: do the two in separate steps, and in separate commits where you can.
Do not touch code you do not have to touch. Tidying up a working file that is not going to change, just because you do not like it, adds risk. That too is this guide's advice.
Sources
Fowler's site: the definition of refactoring (restructuring the internals while preserving the behaviour), the small steps and an introduction to the catalogue.
The definitions of the Rename Variable, Extract Function, Inline Function and Decompose Conditional steps.
A smell being a surface sign of a deeper problem, and not being bad on its own. February 9, 2006.
Preparing the structure before a new feature, and Kent Beck's "make the change easy" line. January 5, 2015.
Testing internal details making refactoring harder, and the principle of rarely changing a test while refactoring. July 13, 2019.
Check the lesson's main ideas with three questions.
Your progress is stored only in this browser.
Last updated: November 3, 2026