Loading video player…

Locate the Fault at the Seams

Key points (each is said and shown in the video):

  • Grace’s report, reproduced: on a freshly seeded shop, book-1 has 18 on hand. Saving an adjustment of -20 from the staff desk’s Shelf count form leaves book-1: -2 on hand, 0 reserved.
  • Your map places the cause in adjustStock(), at the line that adds the adjustment. Your check script, node ~/checks/shelf-count.mjs, calls adjustStock() directly with -20 and prints Actual: book-1 onHand -2, but it doesn’t show what the staff desk’s real path hands adjustStock(). So treat the placement as a claim, and check it on that path.
  • A short method for debugging code you didn’t write:
  • State the expected and actual results exactly. Expected: the adjustment is refused, and book-1 stays at 18. Actual: book-1: -2 on hand.
  • Form a hypothesis that a check could prove wrong: the count goes wrong inside adjustStock(), not before it. Then the form and the route should pass on exactly what staff typed, -20, and any other value arriving at adjustStock() would prove the hypothesis wrong.
  • Check it with a real value, such as a logged number. Rereading the code isn’t a check.
  • This path has three short parts. A real bug can cross eight, and reading each closely first can take hours. Let a few logged values choose the one part worth reading.
  • The adjustment travels through three parts: the staff desk’s form in public/admin/app.js, the /api/stock/adjust route in server.js, and adjustStock() in src/stock/adjustments.js, which changes the stock row. Each place where one part hands the value to the next is a seam.
  • In handleForm(), the form’s transform function turns what staff typed into what gets sent, so the form’s log line prints transform(values). adjustStock() gets two log lines. Before the adding line, one logs what comes in: the adjustment, and the count it starts from, which only adjustStock() can see. After it, the other logs what goes out to the stock row. That pushes the adding line from line 7 down to line 8.
  • The form’s log line runs in the browser, so it appears in the browser’s Console: open the developer tools with F12, or Cmd+Option+I on macOS, and pick the Console tab before you save. The lines of the route and of adjustStock() run in Node, so they print in the terminal that runs the shop.
  • The run: the form sends -20, what staff typed, and the route gets the same number. Neither one changes the value, so both are ruled out as the place it goes wrong. Whether either should refuse -20 waits for the next lesson. adjustStock() gets 18 and -20 and hands back -2, a count below zero, so that’s where the count goes wrong.
  • Only then read that one function closely: one line adds the adjustment to the count, and nothing compares the result with zero. The map’s placement is now a finding, with logged values behind it.
  • The rule: check the value at each seam first, then read closely only the part where a good value turns bad.

Final code: the log lines, one at each seam.

In handleForm(), public/admin/app.js, new line 58:

Language: JavaScript
const values = Object.fromEntries(new FormData(form));
console.log('form sends', transform(values));

In the /api/stock/adjust branch of server.js, new line 62:

Language: JavaScript
} else if (url.pathname === '/api/stock/adjust') {
    console.log('route gets', input);
    result = await runCommand(state => adjustStock(state, input.bookId, input.delta));

In src/stock/adjustments.js, new lines 7 and 9:

Language: JavaScript
export function adjustStock(state, bookId, delta) {
    if (!Number.isSafeInteger(delta)) throw new Error('Adjustment must be an integer');
    let row = findStock(state, bookId);
    console.log('adjustStock gets', delta, 'on hand', row.onHand);
    row.onHand += delta;
    console.log('adjustStock leaves', row.onHand);
    return row;
}

What the run printed, in the browser’s Console:

Language: Program Output
form sends {bookId: 'book-1', delta: -20}

And in the terminal that runs the shop:

Language: Program Output
route gets { bookId: 'book-1', delta: -20 }
adjustStock gets -20 on hand 18
adjustStock leaves -2

Keep these log lines for now. The next lesson takes them out.

Next: Test a Hypothesis at a Seam.

00:00 In Exercise: Pin Down the Shelf Count, you reproduced Grace’s report. On a freshly seeded shop, book-1 has 18 copies on hand. Save an adjustment of -20, and the shelf count reads -2.

00:14 Your map already places the cause at the line in adjustStock() that adds the adjustment. Your check script calls adjustStock() directly with -20, and gets -2.

00:24 It doesn’t show what the staff desk’s real path hands it. So treat that placement as a claim, and check it on that path.

00:33 Start with a short method. First, state the expected and actual results exactly. The adjustment should be refused, and book-1 should stay at 18. It shows -2.

00:45 Second, form a hypothesis that a check could prove wrong. Yours is that the count goes wrong inside adjustStock(), not before it. The form and the route should pass on exactly what staff typed, -20.

00:58 Any other value arriving at adjustStock() would prove you wrong. Third, check it with a real value, such as a logged number. Rereading the code isn’t a check.

01:10 This path has three short parts. A real bug can cross eight, and reading each closely first can take hours. Let a few logged values choose the one part worth reading.

01:23 The adjustment travels through three parts. The staff desk’s form sends it. A route in server.js receives it. Then adjustStock() changes the count. Each place where one part hands the value to the next is a seam.

01:38 Add a log line at each seam. In app.js, handleForm() sends every staff desk form. Its transform function turns what staff typed into what gets sent. The new line prints that value.

01:51 The lines around it gather the form and send the request, and you don’t need them yet. In server.js, the route logs the input it gets, before adjustStock() sees it.

02:03 adjustStock() gets two log lines. Before the adding line, log what comes in: the adjustment, and the count it starts from, which only adjustStock() can see.

02:13 That pushes the adding line down to line 8. After it, log what goes out to the stock row.

02:21 Now reproduce the bug. Run npm run seed and start the shop. Open the staff desk again so it loads your new line. Open the browser’s developer tools with F12, or Command Option I on a Mac, and pick Console.

02:36 Then save -20 for book-1. app.js runs in the browser, so its line appears in the browser’s Console. The form sends delta -20.

02:49 The other two files run in Node, so theirs print in the terminal that runs the shop. The route gets -20. adjustStock() gets -20 with 18 on hand. It leaves -2.

03:04 Compare each line with what you expected. The form sends -20, what staff typed, and the route gets the same number. Neither one changes the value, so both are ruled out as the place it goes wrong.

03:16 Whether either should refuse it waits for the next lesson.

03:21 adjustStock() gets the values you expected, 18 and -20. It hands back -2, a count below zero. So adjustStock() is where the count goes wrong.

03:34 Only now, read that one function closely. Between your two log lines, one line adds the adjustment to the count, and nothing compares the result with zero.

03:44 Your map’s placement is now a finding, with logged values behind it.

03:50 So when a bug crosses several parts, check the value at each seam first. Then read closely only the part where a good value turns bad. Keep your log lines for now.

04:02 The next lesson takes them out.

04:05 Next, in Test a Hypothesis at a Seam, you’ll ask what each side of this seam means by that number, and start a record of every check.

Become a Member to join the conversation.