Commutativity
Many operations shouldn’t care which way round they’re given their inputs: adding two totals, merging two sets of changes, combining the counts from two servers. When one quietly does care, the result depends on which input happened to arrive first, and that’s a bug that shows up only sometimes. Commutativity checks that both orders give the same answer.
The example
Section titled “The example”A greengrocer’s app keeps each basket as fruit and how many of each. Combining two baskets should give everything in both:
/** Everything in both baskets. */@SafeVarargsstatic Map<String, Integer> combine(Map<String, Integer>... baskets) { var all = new HashMap<String, Integer>(); for (var basket : baskets) { all.putAll(basket); } return all;}Whichever basket comes first, the combined basket should be the same:
@Testvoid combiningBasketsInEitherOrderAgrees() { forAll(baskets(), baskets()) .check( (mine, yours) -> { var oneWay = Baskets.combine(mine, yours); var otherWay = Baskets.combine(yours, mine); assertEquals(oneWay, otherWay); });}
static Domain<Map<String, Integer>> baskets() { return maps(elements("apple", "pear"), integers().atLeast(1).atMost(9));}With only two kinds of fruit, two baskets often hold the same kind, which is where the order matters.
When it fails
Section titled “When it fails”expected: <{apple=2}> but was: <{apple=1}>
combiningBasketsInEitherOrderAgrees failed after 1 try (seed 0x73cb82e6abd602f4) shrunk counterexample: mine = {"apple"=1}, yours = {"apple"=2} pin it: .example(/* mine */ Map.of("apple", 1), /* yours */ Map.of("apple", 2)) rerun: PROPTASTIC_SEED=0x73cb82e6abd602f4 ./gradlew test --tests 'CommutativityTest.combiningBasketsInEitherOrderAgrees'My basket has one apple and yours has two. putAll doesn’t add the counts: the second basket’s
count replaces the first’s. So combining mine with yours gives two apples, yours with mine gives
one, and it should be three either way. Baskets with different fruit, the case a hand-written test
would likely try, combine correctly.
The assertion’s own message prints the maps as Java does, {apple=2}. The counterexample prints
them as source, {"apple"=1}, and the pin it: line as code that builds them.
Reach for it when…
Section titled “Reach for it when…”- the code combines two things of the same kind: sums, unions, merges, the counts from two workers, two sets of permissions;
- work is split up and gathered again in whatever order it finishes, as in parallel streams and distributed systems;
- two operations should be independent: applying a discount then a tax, or the tax then the discount.
It won’t catch…
Section titled “It won’t catch…”- Combining that’s wrong in both orders. A
combinethat drops every count but the largest is commutative, and still loses fruit. Check what the result holds too: here, that every fruit’s count is the sum of the two. That’s conservation. - Operations that are meant to care about order. Subtraction, string concatenation, applying changes to a document: check those with another pattern.
In the literature
Section titled “In the literature”An operation is commutative when a ∘ b = b ∘ a. Hughes, 2020 in further reading is a guide to choosing properties like this one.