The 12.12 sale flyer for Raka's T-Shirt Shop is about to be printed a thousand times. Its writer has read it ten times, but their eyes read what they meant, not what's written. A second person reading with a checklist in hand finds the mistyped price in two minutes.
Code review is being that second person: reading a teammate's code change before it goes into the main branch. The goal isn't finding who's to blame, it's making sure the code that goes in is easy to understand, safe to change, and in line with the team's standards.
PR #14: Add the SAVE20 voucher
The PR description: "Adds the SAVE20 voucher (20% off) for the 12.12 sale. Vouchers are now read from a VOUCHERS table. Tried with SAVE10." Its diff, - for removed lines and + for added ones:
-const VOUCHER_DISCOUNT = 0.1;
+const VOUCHERS = { SAVE10: 0.1, SAVE20: 0.2 };
const FREE_SHIPPING_MIN = 200;
const SHIPPING_FEE = 15;
function applyVoucher(amount, voucherCode) {
- if (voucherCode === 'SAVE10') return amount - amount * VOUCHER_DISCOUNT;
- return amount;
+ try {
+ // subtract the discount from amount
+ return amount - amount * VOUCHERS[voucherCode];
+ } catch (e) {
+ return amount;
+ }
}
function calculateTotal(cart, voucherCode) {
let total = calculateSubtotal(cart);
+ const freeShipping = total >= FREE_SHIPPING_MIN;
total = applyVoucher(total, voucherCode);
- total = addShipping(total);
+ if (!freeShipping) total += SHIPPING_FEE;
return 'Total: ' + formatPrice(total);
}
It looks tidy. The author says it was tried with SAVE10. Before you read it with a checklist, guess: of the four test carts, how many are still OK?
The checklist, most important first
The order goes from what's most expensive if it slips through:
1. Correct? Run the tests. Don't trust "tried it". The four carts from this track:
OK Total: $220.50
DIFF Total: $NaN, expected Total: $245.00
DIFF Total: $NaN, expected Total: $90.00
DIFF Total: $189.00, expected Total: $204.00
Only one is still OK, exactly the cart the author tried. With no voucher, VOUCHERS[''] is undefined, and amount * undefined gives NaN (Not a Number).
2. Does it match the description? The description says "adds a voucher", but the diff also changes calculateTotal: shipping is now checked before the voucher. That's why cart D becomes $189. A business-rule change the description doesn't mention easily slips through without anyone deciding on it.
3. Are errors handled properly? The try/catch in applyVoucher catches nothing: amount * undefined doesn't throw, it just gives NaN. It only gives a false sense of safety, and if a real error ever happens, it swallows it.
4. Is there a test for the change? Not one cart uses SAVE20. The suggestion: cart A with SAVE20, $245 minus 20% is $196, below $200, so shipping is added: Total: $211.00.
5. Easy to read? The comment // subtract the discount from amount only repeats its line. In other PRs you often meet: a 200-line function, a variable named x, magic numbers.
6. Safe? Data from outside (form fields, the URL, a voucher code the buyer types) is checked first. No password, token or API key is in the diff. Ownership is checked: a buyer can open only their own orders. No SQL or HTML is built from the user's text. In PR #14, voucherCode is typed by the buyer, so the code toString is also read from VOUCHERS. Number 6 does not mean least expensive: in a PR that touches login, payments or user data, check it together with number 1.
What's not on the list: spaces, semicolons, quote marks. That's the job of tools like Prettier and ESLint running automatically, so the reviewer can focus on the logic.
Writing review comments
A helpful comment is specific, says why, and offers a way out. Mark which ones must be fixed (blocking) and which are only suggestions:
- [blocking] With no voucher,
VOUCHERS['']isundefined, so carts A and C in the tests come out as$NaN. How aboutconst rate = Object.hasOwn(VOUCHERS, voucherCode) ? VOUCHERS[voucherCode] : 0;? (?? 0alone isn't enough: the codetoStringstill gives$NaN.) - [blocking]
freeShippingis now computed before the voucher, so cart D becomes $189. That isn't in the description. If it's on purpose, could it go in its own PR once Raka agrees? - [blocking] There's no SAVE20 test yet. Suggestion:
check(calculateTotal(cartA, 'SAVE20'), 'Total: $211.00'); - [suggestion] This
try/catchnever catches anything, it can go. - [nit] The comment
// subtract the discount from amountrepeats the code.
For a 200-line function, "Why is this so long?" doesn't help. Write something you can act on: "This function is too long (200 lines). How about splitting it into validation, pricing and saving, like the OrderService in the Single Responsibility lesson?"
On GitHub, a review ends with one of three: Comment (just commenting), Approve (fine to merge), or Request changes (fix things first). PR #14 has three blocking findings, so Request changes. Once the author fixes them, all four carts are OK plus SAVE20 at $211.00, and then Approve.
Small PRs are also easier to review carefully. SmartBear, from a code-review study at Cisco, advises no more than 400 lines per review.
⚠️ Reviews that help nobody
- Approving without running anything. "LGTM" (looks good to me) on a diff that looks tidy lets three
DIFFs through. - Reading only the part the description mentions. The shipping change in PR #14 sits right outside what it mentions.
- Getting busy with style. Spaces and quote marks are the formatter's job; the reviewer's time is for logic, tests and security.
- Comments that attack the person. "Why did you write it like this?" makes the author defensive. Talk about the code, say why, offer a way out.
- Not telling blocking from suggestions. If everything sounds mandatory, the author can't tell what must be fixed before merging.
TL;DR: Code review makes sure the code that goes in is easy to understand, safe to change and in line with the team's standards, not who's to blame. Read with a checklist, from what's most expensive if it slips through: correct (run the tests, don't trust "tried it"), matches the description, errors handled, a test for the change, easy to read, safe (input from outside is checked, no secret in the diff); style is left to the formatter and linter. PR #14, which "only adds SAVE20", turns carts with no voucher into $NaN and cart D into $189.00, with a try/catch that catches nothing and no SAVE20 test ($211.00). Review comments are specific, say why, offer a fix, and are marked blocking or suggestion; for a 200-line function, say it's too long and propose how to split it. Three blocking findings mean Request changes; once everything is OK, Approve.
⚡ A second reader with a checklist
Meaning: Code review is reading a teammate's code change before it goes into the main branch. The goal isn't finding who's to blame, it's making sure the code that goes in is easy to understand, safe to change, and in line with the team's standards.
⚡ Correct? Run the tests
Write:
return amount - amount * VOUCHERS[voucherCode];
Guess first: the author of PR #14 says "tried it with SAVE10". Of the four test carts, how many are still OK?
Only one, exactly the cart the author tried. With no voucher, VOUCHERS[''] is undefined, and amount * undefined gives NaN.
Meaning: Don't trust "tried it". The checklist goes from what's most expensive if it slips through: correct, matches the description, errors handled, has a test, easy to read. Spaces and quote marks are the formatter's job.
⚡ Comments you can act on
Write:
const rate = Object.hasOwn(VOUCHERS, voucherCode) ? VOUCHERS[voucherCode] : 0;
Meaning: A helpful review comment is specific, says why, and offers a way out like the line above, marked blocking or suggestion. Any blocking finding means Request changes. Once everything is OK, Approve.