Code review checklist
30 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)A change that compiles and passes its own tests can still hide two bugs. ShopLite teams catch them with a second pair of eyes. What should that reviewer look at first?
What reviewers check
A pull request (PR) is a request to merge one branch into another, shown with its diff, a description and the review discussion.
Example 1 is a pull request that compiled and passed its author's test, yet had two real bugs. A reviewer found them by asking questions.
Example 1 · What a careful review caught
import java.util.HashMap;import java.util.Map;
/** A pull request adds "customers can cancel their own orders". Two reviewers' findings, shown by running it. */public class ReviewCatchDemo { enum Status { PLACED, SHIPPED, CANCELLED }
static class Order { final int id; final int customerId; final String sku; final int quantity; Status status = Status.PLACED;
Order(int id, int customerId, String sku, int quantity) { this.id = id; this.customerId = customerId; this.sku = sku; this.quantity = quantity; } }
static final Map<String, Integer> stock = new HashMap<>();
static String cancelAsSubmitted(Order order, int customerIdFromToken) { if (order.status != Status.PLACED) { return "409 Order " + order.id + " can no longer be cancelled"; } order.status = Status.CANCELLED; stock.merge(order.sku, 1, Integer::sum); // returns ONE unit, whatever the quantity return "200 Order " + order.id + " cancelled"; // and never looks at customerIdFromToken }
static String cancel(Order order, int customerIdFromToken) { if (order.customerId != customerIdFromToken) { return "404 Order " + order.id + " was not found"; // same answer as a missing order } if (order.status != Status.PLACED) { return "409 Order " + order.id + " can no longer be cancelled"; } order.status = Status.CANCELLED; stock.merge(order.sku, order.quantity, Integer::sum); return "200 Order " + order.id + " cancelled"; }
public static void main(String[] args) { stock.put("BK-ATOMIC", 10); System.out.println("As submitted: Ravi (customer 2) cancels Asha's order 16 for 3 books"); System.out.println(" " + cancelAsSubmitted(new Order(16, 1, "BK-ATOMIC", 3), 2) + ", stock now " + stock.get("BK-ATOMIC"));
stock.put("BK-ATOMIC", 10); System.out.println("After review:"); System.out.println(" Ravi: " + cancel(new Order(16, 1, "BK-ATOMIC", 3), 2) + ", stock " + stock.get("BK-ATOMIC")); System.out.println(" Asha: " + cancel(new Order(16, 1, "BK-ATOMIC", 3), 1) + ", stock " + stock.get("BK-ATOMIC")); }}As submitted: Ravi (customer 2) cancels Asha's order 16 for 3 books 200 Order 16 cancelled, stock now 11 After review: Ravi: 404 Order 16 was not found, stock 10 Asha: 200 Order 16 cancelled, stock 13
What this code does
- The submitted code compiled and the author's own test passed, because it only cancelled the author's order for one unit. A reviewer asking "whose order?" and "how many units?" found two real bugs.
- After review, another customer gets 404 (as for any order they cannot see) and stock gets all 3 books back.
Most real bugs are found by asking a few questions in order of importance:
- Correctness: does it do what the description says, including edge cases (empty list, 0, the boundary, someone else's data)?
- Tests: is the new behaviour tested with real assertions, including error paths? Do the tests fail without the change?
- Names and design: is it easy to read, in the right class and layer, without duplicated rules?
- Errors and security: right status codes and exceptions, no secrets in code or logs, owner and role checks, no SQL built from user text?
Find the bug
What would a reviewer flag?
Each snippet is from a pull request. Tap the line a careful reviewer would stop at.
The order had 3 books. Tap the line with the correctness bug.
Round 1 of 3Small pull requests and clear descriptions
A small pull request does one thing and changes a few hundred lines at most, so a reviewer can read every line carefully in one sitting.
A good description tells the reviewer what changed, why and how it was tested. This one is for the order-cancel pull request:
## WhatCustomers can cancel their own PLACED orders: POST /api/v1/me/orders/{id}/cancel.## WhySupport gets ~30 cancel requests a day by e-mail (issue #57).## How it was tested- OrderServiceTest: cancel own order, someone else's order (404), SHIPPED order (409)- Stock returns the full quantity (3 books -> stock +3)- Manual: curl as Asha and as Ravi (see below)## Not in this PRPartial cancellation of single items (issue #58).Example 2 · Checking a pull request's size and tests
import java.util.List;
public class PrSizeDemo { record ChangedFile(String path, int added, int removed) { int lines() { return added + removed; }
boolean isTest() { return path.startsWith("src/test/"); } }
record PullRequest(String title, List<ChangedFile> files) { int changedLines() { return files.stream().mapToInt(ChangedFile::lines).sum(); }
boolean hasTests() { return files.stream().anyMatch(ChangedFile::isTest); } }
static final int COMFORTABLE_REVIEW_LINES = 400; // a common rule of thumb, not a law
static void assess(PullRequest pr) { System.out.println(pr.title() + ": " + pr.files().size() + " files, " + pr.changedLines() + " lines changed"); if (pr.changedLines() > COMFORTABLE_REVIEW_LINES) { System.out.println(" - large: consider splitting it into smaller pull requests"); } if (!pr.hasTests()) { System.out.println(" - no test files changed: how is this change checked?"); } if (pr.changedLines() <= COMFORTABLE_REVIEW_LINES && pr.hasTests()) { System.out.println(" - ready to review"); } }
public static void main(String[] args) { assess(new PullRequest("feat(order): customers cancel their own orders", List.of( new ChangedFile("src/main/java/com/shoplite/order/OrderService.java", 24, 3), new ChangedFile("src/main/java/com/shoplite/order/MyOrderController.java", 18, 0), new ChangedFile("src/test/java/com/shoplite/order/OrderServiceTest.java", 61, 0)))); assess(new PullRequest("Order changes + refactoring + new logging", List.of( new ChangedFile("src/main/java/com/shoplite/order/OrderService.java", 310, 280), new ChangedFile("src/main/java/com/shoplite/common/LogMask.java", 40, 2), new ChangedFile("src/main/resources/application-prod.properties", 6, 1)))); }}feat(order): customers cancel their own orders: 3 files, 106 lines changed - ready to review Order changes + refactoring + new logging: 3 files, 639 lines changed - large: consider splitting it into smaller pull requests - no test files changed: how is this change checked?
What this code does
- The first PR does one thing, changes about 100 lines and includes tests: it can be reviewed properly in one sitting.
- The second mixes a feature, a refactoring and a logging change in 639 lines with no tests. Splitting it into three PRs makes each one quick to review and safe to revert.
A 2,000-line pull request gets a quick "looks good" because nobody can hold it all in mind. Split work into steps: a refactoring PR, then the feature PR, then the clean-up.
Link the issue, list anything you are unsure about, and add a screenshot or a curl example for API changes. Example 2 measures two pull requests by size and tests.
Order of execution
A pull request's journey
Put the steps from opening a pull request to merging it in order.
Writing kind, specific review comments
A nit is a small, optional review point, written "nit:", that does not block the merge. It tells the author which comments are important and which are polish.
A review comment is about the code, not the person. Compare vague, harsh and specific wording for the same problem:
Vague: "This is wrong."Specific: "cancel() never checks customerIdFromToken, so any customer can cancel any order. Could we compare it with order.customerId and answer 404, like getOrder() does?"Harsh: "Why would you put SQL in a controller??"Kind: "Could this query move to OrderRepository? Controllers in ShopLite stay free of SQL."nit: "nit: `cnt` -> `cancelledCount`? Not blocking."A good comment says what, why and what to do: "This returns one unit to stock even when 3 were ordered; order.quantity would fix it. Could you add a test with quantity 3?" Questions invite discussion; orders end it.
Mark how important a comment is: prefix optional ideas with nit: and say clearly which comments block the merge. Praise good work too: "Nice guard clause here" is useful feedback, and it makes the hard comments easier to hear.
Predict the output
Which comment helps more?
Read both comments on the same code. Pick the one the author can act on.
Round 1 of 3
Which comment helps the author more?
Receiving feedback, and a checklist for your own code
Self-review is reading your own diff with the checklist before you ask anyone else to review it.
Before you request a review, review your own diff. Most small problems are found in this one pass:
git diff --staged # read every line you are about to commitmvn verify # tests, coverage rule and static analysis all passgrep -rn "System.out\|printStackTrace\|TODO" src/main/javagit log --oneline main..HEAD # small commits with clear messages?Example 3 · A quick self-review of my own diff
import java.util.List;import java.util.regex.Pattern;
public class SelfReviewDemo { record Check(String problem, Pattern pattern) { }
static final List<Check> CHECKS = List.of( new Check("debug print left in", Pattern.compile("System\\.(out|err)\\.print|printStackTrace\\(")), new Check("TODO without an issue number", Pattern.compile("TODO(?!\\(#\\d+\\))")), new Check("commented-out code", Pattern.compile("^\\s*//.*;\\s*$")), new Check("possible secret", Pattern.compile("(?i)(password|secret|token)\\s*=\\s*\"")), new Check("Optional.get() (use orElseThrow)", Pattern.compile("\\.get\\(\\)\\s*;")));
public static void main(String[] args) { // The added lines (+) of my diff, as `git diff --staged` would show them List<String> added = List.of( " Order order = orders.findById(id).get();", " // order.setNote(\"cancelled by customer\");", " System.out.println(\"cancelling \" + id);", " // TODO handle partial cancel", " // TODO(#58) partial cancel: separate PR", " private static final String SMS_TOKEN = \"sk_live_123\";", " order.setStatus(OrderStatus.CANCELLED);");
int found = 0; for (int i = 0; i < added.size(); i++) { for (Check check : CHECKS) { if (check.pattern().matcher(added.get(i)).find()) { System.out.println("line " + (i + 1) + ": " + check.problem()); found++; } } } System.out.println(found + " things to fix before asking for a review"); }}line 1: Optional.get() (use orElseThrow) line 2: commented-out code line 3: debug print left in line 4: TODO without an issue number line 6: possible secret 5 things to fix before asking for a review
What this code does
- Five of the seven added lines need work, and every one is something the author can find alone in a minute: a debug print, dead code, a vague TODO, a secret and an
Optional.get(). - A TODO with an issue number (
TODO(#58)) is fine: the work is tracked. Simple checks like these can also run in CI, so reviewers never see them.
Comments are about the code, so read them as help, not as a judgement of you. Answer every comment: fix it, or explain your reasoning and agree on a decision. If a thread goes back and forth more than twice, talk for five minutes instead.
A leftover System.out, a commented-out block and a missing test are all easy to find yourself. Example 3 scans the added lines of a diff for five common leftovers.
Next step
Check your understanding
12 quick questions