07480 257117 Talk to us

One money rule, asserted three times

TipBop promises that the worker receives exactly the amount the customer chose. The server, the iPhone app and the Android app each compute the money, so we made them prove it against one shared file, to the penny.

£10.00 set large on a pale pink background beside a receipt for a £10 tip: a fee of 40p plus 4% is £0.80, the customer pays £10.80 and the worker gets £10.00, above the words: What the worker receives from a £10 tip, in all three languages

One of the tests that guards TipBop’s fee is called “has NO ceiling”. It tries every tip from £2.00 to £250.00, one penny at a time. That is 24,801 amounts, and it checks the fee on each one.

It sounds excessive for a sum that fits on a receipt. It is not, because the sentence it protects is the only promise TipBop makes about money: the worker receives exactly the amount the customer chose.

We told the story of why the customer pays the fee in the customer pays the fee. This post is about the other half: how a promise like that stays true after a third person touches the code. The plain-terms version for customers is in tipbop.com’s guide to where a digital tip goes.

Here is the promise on one tip. A customer chooses £10.00. The fee is 40p plus 4%, which is £0.80, so the customer pays £10.80 and the worker receives £10.00. Every test in this post exists to keep that last figure true.

Whole pence, everywhere

The first rule is in the comment at the top of the server’s money module. Every value is an integer number of pence. There is no floating-point currency value anywhere in the file, and there must never be one.

The reason is old and boring. A computer’s ordinary decimal number cannot hold a value like 10.8 exactly. Do a little arithmetic on it and you can end up a penny out. A penny out on one tip is invisible. A penny out on every tip is a worker shorted by a rounding habit.

So the fee is worked out in whole numbers: a fixed 40p plus a share measured in basis points, rounded half up. The two phone apps follow the same rule in their own languages.

Where Language How it keeps the pence whole
Server TypeScript Integer pence only. Rounding is applied to integer expressions, never to a decimal.
iPhone app Swift A money type built on pence, with no Double in it. The formatter is handed an exact decimal.
Android app Kotlin Whole pence, with Long intermediates so a multiplication cannot overflow, and an integer floor division for rounding.

One file all three must agree with

Writing the same sum three times is three chances to short someone by a penny. Code review does not catch a penny. So we stopped relying on review and made a file do the checking.

A script runs the server’s own arithmetic, which is also the code that creates the real payment, and writes a shared fixture. The iPhone and Android test suites read that file and must reproduce every number in it. The fixture holds 54 quotes. They include both ends of the tip range and odd amounts such as £3.12, and the file also lists typed amounts the apps must parse.

The server is the authority by construction. If its sum changes, the fixture changes, and the phone tests fail until the apps catch up. That is the point.

TypeScript, serverthe authority, makes thereal paymentwritesOne sharedfixturequotes, amountsmust matchmust matchSwift, iPhoneno Double for moneyKotlin, AndroidLong intermediatesParity test: regenerate, compare, fail the build if they differ

The fixture that agreed with itself

Here is where it went wrong, and it went wrong quietly.

The generator’s own header promised a check that would regenerate the fixture and compare it. That check was never written. Nothing regenerated the file and nothing compared it. It sat out of date for weeks, still stating a tip range of £3 to £100, long after the product had moved to £2 to £250.

The iPhone and Android suites both passed against it and reported agreement. That agreement was worthless.

A second problem hid behind the first. The fixture predated the day we corrected which side of the payment Stripe bills. The Android code was still working out the old, wrong application fee, and it was held to a fixture that agreed with it.

Two stale copies agreeing is what a fixture nobody regenerates buys you.

The same hunt turned up older cases of the same bug. A fee cap left behind in a database constraint had failed every tip over £40 at the moment of payment. The tip range had once been stuck in a second place in the same way. Both were found in production.

What now fails the build

The fix was not a flag somebody has to remember. A parity test now regenerates the fixture from the live server code and compares it with the committed file on every gate run. If the two differ, the build stops.

The same test reads the source text of the iPhone app, the Android app and the web app, and checks that each holds the right tip limits. It reads text because the server’s test suite has no Swift or Kotlin toolchain. A rule that only one of three apps enforces is not a rule.

It also says what it cannot do. It cannot prove that an app uses its constant, or uses it correctly. It only makes sure the next person who reaches for one finds the right number.

The “has NO ceiling” test comes from the same habit, and from a mistake of ours in August. We had capped the fee, and the cap quietly stopped covering card costs on large tips. We removed it. Now the test walks the whole range and fails if the fee ever falls behind as the tip grows.