Check the Change Against Its Intent

In the last exercise, you sorted the effects of a few made-up changes by what a revert would leave behind. Reach and reversibility tell you how closely to read a change. Whether the change is right depends on what it was asked to do.

In this lesson, you’ll turn a change’s intent into a checklist and map each item to the lines that handle it. You’ll also find what a change does that nobody asked for and leave style for last. Start with a pull request that does what it was asked, so you know what a match looks like.

Turn the Intent Into a Checklist

A change can be clean, tested, and well named and still solve the wrong problem. So follow this principle for this part of the course: check a change against its intent before anything else. You wrote intents as sentences in the last course. For a review, split the intent into a checklist, one item per thing the request asks for, to tick off against the diff.

Pull request #22 limited the size of a basket. Open docs/pulls/22.md and read only Grace’s request:

A mistyped import can contain thousands of lines. Decline it before [the shop starts] reserving books. A hundred distinct lines is enough for this shop.

Before you read Ada’s reply or the diff, write the checklist under a ### #22 heading in the ## Reviews section of your map.md:

Language: Text
### #22
Intent (Grace)
- [ ] a basket of more than 100 lines is declined
- [ ] it's declined before any book is reserved
- [ ] (inferred) the limit doesn't decline a valid basket of 100 lines

Grace doesn’t say the third item outright, but “a hundred is enough” implies it. It covers the new limit only, so a basket can still be declined for an unknown book or a bad quantity. Mark an item you inferred, so that if it’s wrong, you ask Grace and not the author.

Now read the change in order. The file list is short:

Language: Shell
$ git show --stat --oneline cd817bb
cd817bb Decline oversized baskets before reserving stock (review #22)
 docs/pulls/22.md        | 7 +++++++
 src/orders/catalogue.js | 1 +
 2 files changed, 8 insertions(+)

Apart from the exported discussion, one line was added:

Language: Shell
$ git show --oneline cd817bb -- src/orders/catalogue.js
cd817bb Decline oversized baskets before reserving stock (review #22)
diff --git a/src/orders/catalogue.js b/src/orders/catalogue.js
index cc630d3..d8ad605 100644
--- a/src/orders/catalogue.js
+++ b/src/orders/catalogue.js
@@ -7,6 +7,7 @@ export function findBook(state, id) {

 export function orderLines(state, items) {
     if (!Array.isArray(items) || items.length === 0) throw new Error('Choose a book');
+    if (items.length > 100) throw new Error('A basket can have at most 100 book lines');
     let lines = [];
     let seen = [];
     for (let item of items) {

Map each checklist item to the line that handles it, with src/orders/catalogue.js open:

  • More than 100 lines declined: Line 10 throws when items.length > 100, so 101 lines are declined and 100 aren’t.
  • Before any book is reserved: This needs code outside the hunk. In src/orders/create.js, createOrder() calls orderLines() on line 15 and reserve() on line 23, so the throw comes first.
  • The limit doesn’t decline 100 valid lines: Line 10 uses >, not >=, so exactly 100 lines pass. Line 17, which declines the same book on two lines, is an existing rule that this change doesn’t touch.

That’s still an interpretation, so check the boundary with a run. The seed data has only four books, so this check makes up 101 books in memory. Save it as basket-limit.mjs in your checks folder:

Language: JavaScript
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';

const file = resolve('src/orders/catalogue.js');
const catalogue = await import(pathToFileURL(file));

const state = { books: [] };
let items = [];
for (let n = 1; n <= 101; n++) {
    state.books.push({ id: 'test-' + n, title: 'T', price: 500 });
    items.push({ bookId: 'test-' + n, quantity: 1 });
}

let lines = catalogue.orderLines(state, items.slice(0, 100));
console.log('100 lines:', lines.length, 'accepted');
try {
    catalogue.orderLines(state, items);
} catch (error) {
    console.log('101 lines:', error.message);
}

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.