Refactoring
Last updated
Lehman's second law says code grows more tangled with every change unless someone untangles it. Refactoring is that untangling: changing how code is built without changing what it does. In this lesson you'll see what makes a change a refactoring, why it's done in small steps with the tests green after each, and you'll refactor one of Pila's most tangled functions yourself, with its tests watching.
By the end of this lesson you can
- Say what refactoring is, and what it isn’t
- Refactor in small steps, running the tests after each
- Keep refactoring and new behaviour in separate commits
What refactoring is
Martin Fowler, whose book named the practice, defines refactoring as a change to the internal structure of software that makes it easier to understand and cheaper to change, without changing its observable behaviour.
The last part is the point. If patients would notice the difference, it isn't a refactoring:
| Change | Refactoring? |
|---|---|
Rename n to numbers_given | Yes: nothing outside changes |
| Split a 60-line function into three short ones | Yes |
| Remove code that can never run | Yes, if it truly can’t |
| Make the reply say the waiting time | No: that’s a new feature |
| Fix the reply for 1 person ahead | No: that’s a bug fix. Behaviour changes, on purpose |
It's preventive maintenance (lesson 8.1): the untangling Lehman's law says is needed.
Small steps
Fowler's other rule: refactor in small steps, each one keeping the program working, and run the tests after every step. If a test fails, the mistake is in the last small step, and you undo it. Without tests (Stage 6), you can't know the behaviour stayed the same, so refactoring safely starts with tests.
Each common step has a name in his catalogue: Rename Variable, Extract Function, Replace Nested Conditional with Guard Clauses. Named steps are easier to do carefully, and easier to explain in a commit message.
Refactor reply_for
Here's reply_for from Pila's second year: three languages were planned, two were added, and each fix added another if. It works. The six tests all pass. Its if statements nest three deep, and the next language would make it worse.
Refactor it until no if is nested more than two deep, keeping all six tests green. Some ideas, in small steps:
- A guard clause: handle
number is Nonefirst, andreturn. The rest no longer needs to sit inside anif. - Handle
ahead == 0the same way. - Put the wording for each language in a dictionary, such as
TURN = {"en": "...", "fil": "..."}, and look it up with.get(lang, TURN["en"]).
Press Run after each step.
Two hats
Kent Beck describes two hats: when adding a feature, you don't restructure; when refactoring, you don't add features. You can swap hats often, but you wear one at a time, and each commit is one or the other.
- Mon Marco refactors reply_for and adds the waiting time to replies, in one commit.
- Tue Review: “Too big to follow. Looks fine?” Approved.
- Wed Filipino replies say “Numero None”. Which of the 400 changed lines did it?
- Wed The whole commit is reverted. The refactor, which was fine, is lost too.
Without refactoring and the new feature in separate commits: A two-hour hunt for a bug that hid inside a 400-line change, and the whole change rolled back, refactor included.
Switch to the other side to compare.
Spot the trap: a refactor and a feature together
Marco’s pull request says “Clean up sms.py, and add the CANCEL ALL command.” 412 lines changed, and several tests were rewritten.
What happens if…
Check what you learned
Sources
Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.
- Martin Fowler: Definition of Refactoring: the definition used in this lesson
- Refactoring.com: Fowler’s site for the book Refactoring, 2nd edition (2018)
- Refactoring catalog: named refactorings, including Replace Nested Conditional with Guard Clauses
- Lehman: Programs, life cycles, and laws of software evolution (Proceedings of the IEEE, 1980): the law of increasing complexity