Idempotence
Some operations should finish the job in one go: tidy a name, normalize a path, deduplicate a list, apply a migration, retry a payment. If the second run still changes something, the first didn’t finish, and a value tidied once won’t match the same value tidied again elsewhere. Idempotence checks that doing it twice is the same as doing it once.
The example
Section titled “The example”A sign-up form tidies the names people type: no spaces at either end, and one space between words.
/** The name without spaces at either end, and one space between words. */static String tidy(String name) { return name.strip().replace(" ", " ");}Whatever the name, tidying the tidied name should change nothing:
@Testvoid tidyingTwiceIsTidyingOnce() { forAll(names()) .check( name -> { String once = Names.tidy(name); String twice = Names.tidy(once);
assertEquals(once, twice); });}The names are built from words and spaces, so runs of spaces come up often. strings() would draw
any text, but three spaces in a row between two words would be rare:
/** Names as people type them: words, with any number of spaces around them. */static Domain<String> names() { return lists(elements("Ada", " ", "Lovelace")) .ofSize(atMost(12)) .map(parts -> String.join("", parts));}When it fails
Section titled “When it fails”expected: <Ada Ada> but was: <Ada Ada>
tidyingTwiceIsTidyingOnce failed after 19 tries (seed 0xeeb3afada5ff4021) shrunk counterexample: name = "Ada Ada" pin it: .example("Ada Ada") rerun: PROPTASTIC_SEED=0xeeb3afada5ff4021 ./gradlew test --tests 'IdempotenceTest.tidyingTwiceIsTidyingOnce'tidy calls replace with two spaces, marked above, and one space. It turns each pair of
spaces into one space, in a single pass. Three spaces hold one pair and one left over, so they
become two. Tidying again makes them one. Two spaces, the case anyone would test, comes out
right the first time. A fix replaces every run of spaces at once: replaceAll(" +", " "), a
space and a plus.
The expected value comes first in assertEquals, so the message reads “expected” for the name
tidied once, and “was” for the name tidied twice.
Reach for it when…
Section titled “Reach for it when…”- the code cleans, normalizes, formats or sorts something: tidying text, canonical paths, sorted and deduplicated lists, a formatter run over its own output;
- an operation may run again by accident: a retried request, a migration applied twice, a message delivered twice;
- it’s a setter or an “ensure” step:
ensureDirectoryExists,setStatus(PAID).
It won’t catch…
Section titled “It won’t catch…”- An operation that’s wrong but stable. A
tidythat returns""for every name is perfectly idempotent. Pair it with a property that says what the result should be: no double spaces, and the same words in the same order. - Inputs the domain doesn’t draw. Tabs and line breaks aren’t spaces, and these names have none. Draw them too if people can type them.
In the literature
Section titled “In the literature”The name is from mathematics: an operation f is idempotent when f(f(x)) = f(x). Hughes, 2020 in further reading is a guide to choosing properties like this one.