Maintenance and the end of life · lesson 3 of 7
Ideas in this lesson
ARefactoringchange the structure, keep the behaviour
BGuard clausehandle the special case first, and return
CSmall steps, tests greenrun the tests after every change

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:

Refactoring, or not?
ChangeRefactoring?
Rename n to numbers_givenYes: nothing outside changes
Split a 60-line function into three short onesYes
Remove code that can never runYes, if it truly can’t
Make the reply say the waiting timeNo: that’s a new feature
Fix the reply for 1 person aheadNo: 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:

  1. A guard clause: handle number is None first, and return. The rest no longer needs to sit inside an if.
  2. Handle ahead == 0 the same way.
  3. 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.

Step 3 code
Edit the code, then run it again
Halfway through, a test fails. What should you do?

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.

  1. Mon Marco refactors reply_for and adds the waiting time to replies, in one commit.
  2. Tue Review: “Too big to follow. Looks fine?” Approved.
  3. Wed Filipino replies say “Numero None”. Which of the 400 changed lines did it?
  4. 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

Spot the trapMarco

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

Which of these is a refactoring?
What makes refactoring safe?
What is a guard clause?

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.

All sources for Simple Software Development