Your intent for the student discount sits in map.md, the map you started in Finding Your Way Around a Codebase You Didn’t Write, with its “Ask Grace” marks. Grace has answered the biggest one. The 15 percent comes off the books a student pays for, the way READ20’s saving works today. So the “Afterward” line can now read “a student’s order costs 15% less on its paid books.”
That line looks finished, but you still couldn’t hand it to a teammate and expect the same verdict back. In this lesson, you’ll rewrite the intent as criteria that someone else can check without asking you, whoever wrote the change, a teammate or an AI agent, and you’ll test those criteria by inventing a wrong change that still meets them. Start with that line, and ask what a reviewer would compare.
Replace Adjectives With Examples
A sentence like “costs 15% less” feels checkable because it names a number. Hand it to two reviewers, though, and give them one Small Maps at 750p. Fifteen percent of 750p is 112.5p, so one reviewer accepts a total of 637p and the other expects 638p. The line can’t settle it.
An acceptance criterion is one statement of what a change must do, written so that someone else can check it without asking you. You make a criterion checkable with a concrete example, which gives an input and the exact result you expect. One common way to write it is given/when/then: given a starting situation, when something happens, then this is the result.
Here’s the rounding question written as a criterion:
Given a student's order of one Small Maps (750p)
with no code, when it's priced,
then the saving is 113p and the total is 637p.
The 113p comes from the shop’s existing rule. discountAmount() in src/discounts/codes.js rounds every saving with Math.round(), which takes 112.5 up to 113.
If you can’t write an example like this for a line of your intent, the decision behind it hasn’t been made yet. Go back to the intent and add a question for Grace, rather than writing a criterion vague enough to agree with any answer.
The criterion also leaves things out. It doesn’t say how staff mark an order as a student’s or which function applies the discount. A criterion such as “Add a student flag to the order” describes how the code is built, so a change could meet it and still charge full price. For each criterion, ask what a customer or a caller of priceOrder() would observe, and leave design choices open.
When you check a result by hand, you already pick a normal case, a boundary, an empty case, and an invalid one. The rounding example is the student discount’s boundary. The normal case is a two-book order, such as Small Maps at 750p and Night Trains at 1250p, which comes to 1700p. Write that one in map.md too, in the same form.
Cover Every Part of the Intent
Criteria written only from the “Afterward” line would pass a change that gives students 15 percent off and quietly breaks READ20. Your intent has four parts, and three of them turn into different kinds of criteria:
“Afterward” becomes examples of the new behavior, covering the normal case, the boundary, and the empty cases. “Must not change” becomes must-not criteria: examples of what the shop does today, with today’s totals, which the change has to leave alone.
The invariants hold for every example, but each needs an example that could break it before anyone can check it. “Out of scope” becomes nothing to check, and writing it down stops a reviewer from failing the change for something it never promised.
The empty cases need care, because the shop already handles some of them. An order with no books never reaches pricing: orderLines() in src/orders/catalogue.js rejects it with “Choose a book”. An order whose only book is complimentary does reach pricing, with a paidItemsSubtotal of 0p, so 15 percent of it is 0p.
The invalid case comes from Grace’s own words in docs/REQUESTS.md: two codes are rejected with “Use one discount code”. The shop does this for a list of codes. If you send the codes as one string, like Mei’s READ20, WELCOME10 in the first course, you get “Unknown or inactive discount code” instead. That’s a known gap this change doesn’t fix, so criterion 8 sends the codes as a list.
You can’t write one case yet. When a student also brings READ20, the intent keeps one code per order, but nobody has said which discount wins. Add it to your questions for Grace instead.
Add the must-not criteria to map.md in the same given/when/then form, and list the invariants under “Every example keeps”. Take today’s totals from a run of student-discount.mjs. Change the const lines = ... line to end in .slice(0, 1), which keeps Small Maps alone, and change the '' in the priceOrder() call to 'READ20'. Run it, then run it again with 'WELCOME10'.
Read discount and total in the printed object. READ20 gives a 150p saving and a 600p total, and WELCOME10 gives 75p and 675p. Ignore the two reading lines below the object, because they don’t apply a code. Afterward, put the const lines line and the priceOrder() call back as they were. Then compare your criteria with this set:
## Criteria: student discount
Afterward
1. Given a student's order of Small Maps (750p) and
Night Trains (1250p) with no code, when it's
priced, then the total is 1700p.
2. Given a student's order of one Small Maps (750p)
with no code, when it's priced, then the saving is
113p and the total is 637p.
3. Given a student's order of only a complimentary
A Bookshop Window with no code, when it's priced,
then the total is 0p.
4. Given a student's order with no books, when it's
placed, then it's rejected with "Choose a book".
Must not change
5. Given an order of one Small Maps with READ20 for a
customer who isn't a student, when it's priced,
then the total is 600p, as today.
6. Given an order of one Small Maps with WELCOME10 for
a customer who isn't a student, when it's priced,
then the saving is 75p and the total is 675p.
7. Given an order of Small Maps and Night Trains with
no code for a customer who isn't a student, when
it's priced, then the total is 2000p.
8. Given a student's order with READ20 and WELCOME10
sent as a list, when it's placed, then it's
rejected with "Use one discount code".
Every example keeps
- The total is never below 0p: criterion 3 tests it.
- At most one discount applies to the order: its
student example waits on Grace's READ20 answer.
- All refunds on an order together never exceed what
was paid for it: no example here refunds, so it
needs its own, such as refunds on criterion 2's
order adding up to at most 637p.
Out of scope
- Checking who is a student: nothing to check.
Ask Grace
- A student brings READ20. Which discount applies?
Criteria 5 and 6 come from runs of priceOrder(), and criterion 7 uses the sum of the two catalogue prices with no code. Edsger’s first test in test/pricing.test.js checks the same 600p. Today’s code has no student discount, so the student totals come from Grace’s answer, rounded by the rule in discountAmount().
To get a second opinion before you compare your criteria, use the button below to open Code Mentor with a question ready to send. Paste your criteria after the question:
Expect it to point out any criterion without a precise result, whether a total or an error message. Check a suggestion about today’s behavior with a run, and a student total against Grace’s answer and the paid-books calculation from the first lesson.
Hunt for a Wrong Version That Passes
A set of criteria can mention every part of the request and still let a wrong change through. To find the gaps, invent a plausible wrong implementation and run it through your criteria in your head.
Take the reading Grace turned down. Picture a change that takes the student saving from grossSubtotal, the catalogue value, instead of paidItemsSubtotal. It’s one word different from the right change, on the line that calls discountAmount(). The legacy floor on line 19 remains in place.
Before you read on, go through criteria 1 to 8: which of them does this wrong version fail?
None of them. In criteria 1, 2, 5, 6, and 7, no book is complimentary, so grossSubtotal and paidItemsSubtotal are the same number, as you saw when you dropped the free book from order-1 in the last lesson. The wrong version gives 1700p and 637p, the same totals as the right one.
Criterion 3 is the surprise. The wrong version works out 15 percent of A Bookshop Window’s 1000p, a saving of 150p on a basket that costs 0p. Line 19 of src/orders/pricing.js caps the saving at paidItemsSubtotal, so the total is 0p, as the criterion expects. The orders in criteria 4 and 8 are rejected before any price is worked out.
The criteria tell the two readings apart only on a basket where they disagree, so add one. You need paid books and a complimentary one together:
9. Given a student's order of Small Maps (750p) and a
complimentary A Bookshop Window (1000p) with no
code, when it's priced, then the saving is 113p and
the total is 637p.
The wrong version saves 15 percent of 1750p, or 263p, and charges 487p, so criterion 9 fails it. Edsger’s second test in test/pricing.test.js prices the same basket with READ20 because its two subtotals differ. To check the arithmetic, set lines in student-discount.mjs to [state.orders[0].lines[0], state.orders[0].lines[2]] and run it. The two readings print 637 and 487.
Repeat the hunt for every other reading the intent left open and for changes that deliberately break each “must not change” line. A change that gives every customer 15 percent off passes every criterion about students, and criteria 5 to 7 rule it out. When a wrong version passes, add the example where it and the right version give different totals. For more candidates of both kinds, press the button below:
Name the criterion that rules out Code Mentor’s suggested version, or write a new criterion that would.
Check Your Understanding
Answer these with your criteria open:
- A teammate’s criterion reads: “Student discounts are calculated correctly.” Rewrite it as one given/when/then criterion with amounts in pence for an order of one Night Trains at 1250p with no code.
- Another criterion reads: “Add an
applyStudentDiscount()function and store the rate in settings.” Could a change meet it and still charge students full price? What should it say instead? - Suppose your only criteria were 1 and 2 from “Criteria: student discount”. Describe a wrong change that meets both, and the criterion that rules it out.
- Why doesn’t criterion 3 rule out the version that discounts the catalogue value?
- Given a student’s order of one Night Trains (1250p) with no code, when it’s priced, then the saving is 188p and the total is 1062p. Fifteen percent of 1250p is 187.5p, which
Math.round()takes up to 188p. - Yes. The function could exist and never be called, or the rate could be stored as 0. Write what a customer sees instead, such as criterion 1’s 1700p total.
- A change that gives every customer 15 percent off meets both, because they concern only students. Criterion 7 rules it out: a customer who isn’t a student still pays 2000p.
- Its basket has no paid books, so the right saving is 0p, and line 19’s floor caps the wrong 150p saving at 0p too. Both versions charge 0p. Only a basket with paid and complimentary books together, such as criterion 9’s, separates them.
In Exercise: Rule Out the Wrong Version, you’ll write criteria for a pricing request as rows containing a basket, a code, and an expected total. Your rows should hold for the right version of the code, and each wrong version should fail at least one of them.