Fix the Cause and Nothing More

In the last exercise, you wrote checks that decide good or bad on one bug. Grace’s shelf count already has one: ~/checks/shelf-count.mjs, which you wrote while reproducing her note. You’ve also located the fault: line 7 of adjustStock() in src/stock/adjustments.js adds whatever the staff desk sends to the count. Now Grace wants it fixed.

Fixing code you didn’t write carries a risk that finding the bug didn’t, because your change can break something you never read. In this lesson, you’ll decide what the fix must enforce, confirm your check fails before you change anything, and list every line your change could affect. You’ll also learn to tell a fix from a patch that only hides the number. Start with the cause, in one sentence.

Name the Cause and the Rule Before You Fix

A fix that only makes the reported number look right can leave the real problem in place, so write the cause down first. For the shelf count, it reads like this:

adjustStock() adds any whole-number delta to onHand without asking whether that many copies are there to remove, so an adjustment of -20 on 18 copies goes through.

The sentence says the fix is the missing question, but it doesn’t say what the question should be. You could invent one, but the shop already asks it on another path. Here’s reserve(), lines 3 to 5 of src/stock/reservations.js, with line 5 broken in two:

Language: JavaScript
export function reserve(state, order) {
    for (let line of order.lines) {
        if (available(state, line.bookId) < line.quantity)
            throw new Error('Not enough stock');

Before an order takes any copies, reserve() asks available() how many are free and throws an error if there aren’t enough. The answer comes from lines 8 to 11 of src/stock/inventory.js:

Language: JavaScript
export function available(state, bookId) {
    let row = findStock(state, bookId);
    return row.onHand - row.reserved;
}

So the order path’s rule is about copies on hand minus copies reserved, not about the count alone. The fix gives the manual path the same check. That’s the rule the fix enforces: refuse an adjustment that would leave available() below zero, which means onHand below reserved. Throw an error, and leave the stock row unchanged.

Why compare with reserved and not with zero? A sale lands on line 19 of reservations.js, where release() subtracts each sold reservation from onHand. If onHand were already below reserved, that sale would take the count below zero. In today’s shop, createOrder() holds reservations only while it pays for an order, so stored rows show 0 reserved and the two rules agree.

Still, adjustStock() can’t know when it’s called, so its rule has to hold for any row it’s given, including one with copies held. Matching reserve() keeps both paths on one rule.

Since reserved is never below zero, the same rule also keeps onHand at zero or above. That’s Ada’s first invariant, “Stock never goes negative,” now enforced on the path that only assumed it.

Watch the Check Fail on a Branch

Your check is evidence that the fix works only if you’ve seen it fail first. A check that already passes, or that you’ve never run, can’t show that your change made the difference.

Run it on a branch, a separate line of commits in your copy of the repository. The rest of the course uses main, and commits made on your branch leave it unchanged.

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.