Refactoring under properties
A refactor should change how the code is written and nothing it does. Example tests check that for the inputs they name, and a refactor can go wrong just past them: a case the old shape handled by accident and the new one doesn’t. Properties check the new shape on inputs nobody picked, and keep the cases the code once got wrong as pinned examples.
This page refactors the code from Test first: a worked example, catches a refactor that breaks it, and checks the new shape against the old. Then it covers following a model through a refactor, and checking with mutastic that the properties are strong enough to lean on.
Before you start
Section titled “Before you start”Start where the worked example ends: Pages.count, with its two properties and the five examples
they pinned, every test green and committed.
/** How many pages a list of items needs, a number of items to a page. */final class Pages { static int count(int items, int perPage) { return items / perPage + (items % perPage == 0 ? 0 : 1); }}Are the properties strong enough?
Section titled “Are the properties strong enough?”A refactor leans on the tests, so check them first. A property can pass and still say too little,
as one page an item showed in the worked example. mutastic checks: it
makes small changes to the code, a - for a +, a != for a ==, and runs your tests against
each. A change no test catches is a rule your properties don’t say, and a refactor could break it
unnoticed. From the project’s root:
mutastic runEach change it lists that no test caught is a card, with the test it suggests. Write the property, or the example, before the refactor, not after. With proptastic, mutastic gives every property the same seed for every change, so a property’s verdict on one doesn’t come and go between runs (With mutastic).
Refactor
Section titled “Refactor”The code divides, then adds a page for what’s left over: a division and a remainder. One division does it: take one item off, divide, and add a page.
/** How many pages a list of items needs, a number of items to a page. */final class Pages { static int count(int items, int perPage) { return (items - 1) / perPage + 1; }}The properties run against the new shape:
Expecting actual: 0Lto be less than: 0L
theLastPageIsNeverEmpty failed after 13 tries (seed 0x5eed) shrunk counterexample: items = 0, perPage = 2 fails for any: perPage pin it: .example(/* items */ 0, /* perPage */ 2) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'PagesTest.theLastPageIsNeverEmpty'No items, 2 to a page, now needs a page, where it needed none. None of the five pinned examples has
no items, so they couldn’t see it; the property drew a case that could, 13 tries in. With 1 to a
page the refactor gets it right by accident, -1 / 1 + 1 is 0, and that’s why the shrunk
counterexample has 2.
Pin it, and handle no items first:
/** How many pages a list of items needs, a number of items to a page. */final class Pages { static int count(int items, int perPage) { return items == 0 ? 0 : (items - 1) / perPage + 1; }}@Testvoid theLastPageIsNeverEmpty() { forAll(integers().atLeast(0), integers().atLeast(1)) .example(/* items */ 2, /* perPage */ 2) .example(/* items */ 0, /* perPage */ 2) .check( (items, perPage) -> { int pages = Pages.count(items, perPage); long beforeTheLast = (long) (pages - 1) * perPage; assertThat(beforeTheLast).isLessThan(items); });}Both properties pass, with all six examples.
Check the new shape against the old
Section titled “Check the new shape against the old”The properties say what must hold, and not everything the code does. For a refactor, you have something better than a rule: the code you’re replacing, which you trust because it’s been running. Keep a copy of it beside the new code while you refactor:
/** The pages a list needs, as the code said it before the refactor. */final class PagesBefore { static int count(int items, int perPage) { return items / perPage + (items % perPage == 0 ? 0 : 1); }}and check that they agree on every input, compared with a reference:
@Testvoid agreesWithTheCodeItReplaces() { forAll(integers().atLeast(0), integers().atLeast(1)) .check( (items, perPage) -> { int before = PagesBefore.count(items, perPage); assertThat(Pages.count(items, perPage)).isEqualTo(before); });}It needs no expected values and no rule, and it catches any change at all. On the broken refactor:
expected: 0 but was: 1
agreesWithTheCodeItReplaces failed after 3 tries (seed 0x5eed) shrunk counterexample: items = 0, perPage = 2 fails for any: perPage pin it: .example(/* items */ 0, /* perPage */ 2) rerun: PROPTASTIC_SEED=0x5eed ./gradlew test --tests 'ReferenceTest.agreesWithTheCodeItReplaces'Once the refactor’s merged, delete the copy and its property. The rule properties stay: they say what the code must do, and the copy only said what it did.
Follow a model through a refactor
Section titled “Follow a model through a refactor”A copy of the old code is a reference for one refactor. A model is one for every refactor: a simpler version of what the code does, written to be obviously right, that a property checks the code against (Follow a model). It might be a function in Java, a TLA+ spec of the steps the code may take, or a Lean model with theorems the compiler has checked.
A refactor changes the code and leaves the model alone. So the property that follows it checks the new shape against the same model, step by step, and a refactor that changes what the code does fails it with the shortest run of steps that shows it. That’s the safety net for the refactors example tests are weakest at: changing a data structure, batching work, moving a lock.
- The counter in TLA+ and the counter in Lean follow a counter that batches its work against each kind of model.
- From design to code follows a trading desk against both, and catches a planted race and an off-by-one.
If the refactor needs the model to change, it isn’t a refactor: it changes what the code does.
Change the model first, check it (TLC checks a spec, lake build checks Lean’s proofs), and then
the code, test first, against it.
After the refactor
Section titled “After the refactor”Run mutastic again on what changed:
mutastic run --since=HEADThe new shape has new places to go wrong, here an items == 0 that wasn’t there before. A change
to it that no test catches is a rule the properties don’t say about the new code. Then commit.
What it can’t tell you
Section titled “What it can’t tell you”Properties, a reference and a model all check what they say. A model that’s wrong the same way as the code agrees with it, and a reference that had the bug keeps it. mutastic says whether the tests would notice a change, not whether the code is right. Each says more than a list of examples, and none says everything.