Learning Tests & Boundaries — Clean Code

A boundary is the line between your code and code you didn't write (libraries, Intl, Date, payment SDKs); a learning test (Clean Code chapter 8, a term from…

By the end of this lesson you'll have a few small tests that make sure other people's code (the browser's number format, the date reader, a payment library) still works the way you think it does. If it ever changes, those tests speak up first, not your buyers.

Raka did the same when he bought a new receipt printer. Before using it at the till, he printed one test receipt with $220.50 on it to make sure both decimals come out and the "$" fits. That receipt went in the drawer. Next year the printer is replaced by another brand, the same receipt is printed again and compared. That test receipt is called a learning test: a small test you write to pin down how other people's code behaves, before you depend on it. The line where your code meets code you didn't write (a library, a browser's built-in API, a payment gateway SDK) is called a boundary. The term comes from chapter 8, "Boundaries", of Clean Code (Robert C. Martin, 2008); that chapter was written by James Grenning, who took the name learning tests from Jim Newkirk.

The boundary: code that isn't yours

formatPrice in Raka's checkout is one line, '$' + amount.toFixed(2). But what turns 220.5 into 220.50 isn't your code: it's toFixed, part of the browser's JavaScript engine. The same goes for Intl, which formats numbers and dates with language data from CLDR (the Common Locale Data Repository), for new Date(text) reading a date, and the payment gateway SDK charging the buyer. Your code holds assumptions about other people's code, and those assumptions have never been checked.

Learning test: write your assumption down

Write each assumption as one small check that calls the real library:

function show(value) {
  return JSON.stringify(value).replace(/\u00a0/g, '\\u00a0');
}

function check(actual, expected, label) {
  if (actual === expected) console.log('OK   ' + label);
  else console.log('DIFF ' + label + ': ' + show(actual) + ', expected ' + show(expected));
}

check((220.5).toFixed(2), '220.50', 'toFixed two decimals');
check((220.5).toLocaleString('en-US'), '220.50', 'en-US two decimals');
check(new Date('12/03/2026').getMonth(), 2, '12/03/2026 month March');

Before you look at the result, guess the second check: is (220.5).toLocaleString('en-US') === '220.50' true or false? Here is the result in Chrome and Node when this lesson was written:

OK   toFixed two decimals
DIFF en-US two decimals: "220.5", expected "220.50"
DIFF 12/03/2026 month March: 11, expected 2

The answer is false. toLocaleString drops the zeros at the end of the decimals, so the receipt would say $220.5. It looks like the better choice, because it adds a thousands comma (1,250) and toFixed doesn't, until you test it. One small test teaches the decision: the checkout keeps using toFixed(2), which always writes two decimals, and the first test above pins that down.

The date 12/03/2026

Raka's shop writes the day first, like most of the world, so 12/03/2026 is 12 March. new Date('12/03/2026').getMonth() returns 11 (months count from 0, so 11 = December): the browser reads it American style, month/day, 3 December. The JavaScript specification only guarantees the ISO format, like 2026-03-12; other formats such as 12/03/2026 are up to the engine. So the checkout reads dates itself:

function parseDate(text) {
  const [day, month, year] = text.split('/').map(Number);
  return new Date(year, month - 1, day);
}

check(parseDate('12/03/2026').getMonth(), 2, 'parseDate: March');   // OK

The learning test that failed becomes a record of reality, check(new Date('12/03/2026').getMonth(), 11, '12/03/2026 read as month/day'), so anyone who asks "why not just use new Date?" finds the answer right there.

When the library changes

Once the assumptions are corrected, the learning tests hold the real behaviour, and they run in CI every time the browser, Node or a library is upgraded. If the behaviour changes, these tests go red first, before a buyer finds a strange receipt. It has really happened: CLDR 42 data (late 2022) replaced the space before AM/PM in English time formats with a narrow U+202F space, and many tests that compared time text failed after the upgrade.

In the Lab, a made-up "version 2" of stripe drops toFixed(2) and Math.round. The learning tests that used to say OK speak up at once:

DIFF format 50 padding: "$0.5", expected "$0.50"
DIFF parse $1.15: 114.99999999999999, expected 115

parseFloat('1.15') * 100 really is 114.99999999999999 in JavaScript (decimal numbers aren't stored exactly); version 1's Math.round was covering that up.

Wrap it in one place

Learning tests are most useful when the library is called in only one place of yours, an adapter. formatPrice and parseDate are already adapters: receipts, emails and reports call them, not toFixed or Date. Payments work the same way. If the vendor SDK is called directly in 50 files, moving from Stripe to another payment provider means editing 50 files. With an adapter that speaks the shop's language, not the vendor's:

// paymentGateway.js: the only file that knows the vendor SDK
const paymentGateway = {
  async charge(order) {
    const result = await vendorSdk.createCharge({ amount: order.total, currency: 'USD' });
    return { id: result.id, paid: result.status === 'succeeded' };
  },
  async refund(paymentId) {
    await vendorSdk.createRefund({ charge: paymentId });
  },
};

The SDK method names above are only examples; every vendor has its own, and that is exactly what gets hidden. Checkout just calls paymentGateway.charge(order). Change vendors and only this file is rewritten, while its learning tests show what differs in the new vendor.

When to wrap, when to use directly

Exercise: learningTests for the stripe stub

stripe in the exercise is a stub: a small fake payment library that counts in cents (the smallest unit) and writes dollars. stripe.format(cents) turns 1234 into "$12.34" and 50 into "$0.50"; stripe.parse(str) does the reverse, "$12.34" into 1234. The helper assert(actual, expected, label) is already there and returns { label, pass, actual, expected }. In learningTests(), push four assert results into results, in exactly this order and with exactly these labels:

results.push(assert(stripe.format(1234), '$12.34', 'format 1234'));
results.push(assert(stripe.format(50), '$0.50', 'format 50 padding'));
results.push(assert(stripe.parse('$12.34'), 1234, 'parse $12.34'));
results.push(assert(stripe.format(stripe.parse('$7.89')), '$7.89', 'round-trip $7.89'));

The six console.log calls below print 4, true, format 1234, $0.50, 1234, round-trip $7.89. The round-trip check, from text to number and back to text, is there because that is where rounding problems usually show. Don't change the stub: the checker also calls stripe.format(305), stripe.parse("$0.07") and assert(...) with other numbers.

⚠️ What goes wrong at the boundary

TL;DR: A boundary is the line between your code and code you didn't write (libraries, Intl, Date, payment SDKs); a learning test (Clean Code chapter 8, a term from Jim Newkirk) is a small test that calls the real library to check your assumption. (220.5).toLocaleString('en-US') isn't '220.50': it drops the zero at the end and gives '220.5'; toFixed(2) always writes two decimals, so the checkout keeps it. new Date('12/03/2026') is read as 3 December (getMonth() 11), so dates are read with parseDate. Once corrected, learning tests pin the real behaviour and run on every upgrade, so a library change (like the U+202F space in CLDR 42) shows up before production. Wrap a library that can be replaced or that changes in one adapter that speaks the domain's language (formatPrice, paymentGateway.charge, refund); a foundation framework like React and standard APIs are used directly. In the exercise: four assert results pushed into results with the labels format 1234, format 50 padding, parse $12.34, round-trip $7.89, and the output 4, true, format 1234, $0.50, 1234, round-trip $7.89.

⚡ The boundary: code that isn't yours

Meaning: A boundary is the line where your code meets code you didn't write: Intl, Date, a payment SDK. A learning test is a small test that calls the real library to check your assumptions about how it behaves.

⚡ Write your assumption down

Write:

check((220.5).toLocaleString('en-US'), '220.50', 'en-US two decimals');
Guess first: is the result of (220.5).toLocaleString('en-US') equal to '220.50'?

No. It drops the zero at the end and gives '220.5', so the receipt would say $220.5.

Meaning: One small test makes the decision clear: the checkout keeps using toFixed(2), which always writes two decimals.

⚡ The date 12/03/2026

Write:

function parseDate(text) {
  const [day, month, year] = text.split('/').map(Number);
  return new Date(year, month - 1, day);
}

Meaning: new Date('12/03/2026') is read American style, 3 December. The JavaScript specification only guarantees ISO formats like 2026-03-12, so the checkout reads the date itself.

⚡ An alarm for upgrades

Meaning: Learning tests run in CI on every upgrade, so a change in a library shows up before a buyer finds it. Wrap a replaceable library in one adapter that speaks the shop's language, like paymentGateway.charge(order).

What you will learn