Find the Rules That Must Hold

Your map now follows Small Maps’ 750 pence from book-1’s record to the three places that show it. To answer Grace’s discount code question, the map also needs the rules the shop’s data is supposed to follow, such as “a total is never below zero.” You can’t explain why a code didn’t work without them.

In this lesson, you’ll list the rules The Second Draft relies on and cite where each one comes from. Then you’ll take one rule, find every place its data changes, and mark where the rule is enforced and where it’s only assumed.

Start From the Rules Someone Wrote Down

A rule the code relies on being true at all times, whoever places an order and however it reaches the shop, is called an invariant. Some come from the business: Grace never wants the shop to owe a customer money. Others come from the data or the code’s assumptions, such as every order having a customer.

Ada left a list of them in docs/ARCHITECTURE.md, the overview she wrote at v1.0. Here are its lines 25 to 33, with the long line 30 broken in two:

Language: Markdown Text
## Invariants

- Stock never goes negative, and each reservation is released exactly once.
- Order totals never go below zero; only one discount code applies.
- Total refunds never exceed the order's payment.
- Every order belongs to a customer; personal data stays out of logs and
  reports.

These are rules the shop relies on, not proof that every write path keeps them.
Check a second writer before trusting a rule you've found in the first.

Treat this list as claims, the way you treated Code Mentor’s reply. Ada’s list, a comment, or an issue is a source: it says a rule exists, and sometimes why. You can find evidence that the code keeps the rule in these forms:

Start with the rule behind Grace’s question: an order’s total is never below zero. You’ve already read its guard, the legacy floor on line 19 of priceOrder() in src/orders/pricing.js. Here it is with its comment, the long line 17 broken in two:

Language: JavaScript
    // Keep the legacy floor: old imported discount records
    // could exceed the basket.
    // See docs/issues/17.md before removing it.
    if (saving > paidItemsSubtotal) saving = paidItemsSubtotal;

In your OLD120 run, the floor never ran, because findDiscount() refused the inactive code first. So why keep a line today’s data never reaches? The comment points to docs/issues/17.md, where the team saved the discussion:

Language: Markdown Text
# #17 · A migrated offer produced a negative basket

**Grace:** An old imported offer had a percentage above 100. It is disabled now,
but can the same thing make the shop owe a customer money?

**Ada:** Keep the floor in pricing even though new offers are checked elsewhere.
An imported record goes through a different path. The floor belongs at the point
where a discount becomes a total.

**Edsger:** Added a check with an oversized migrated offer. Removing a strange
line without finding its history would undo that guarantee.

Ada’s reason is a past import, and that’s a claim too. To see where discount records come from today, search the repository for them:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.