Shelves / Senior Engineering Craft
SC·02
Code Review
What code review is really for (defects, yes, but mostly shared understanding, maintainability and teaching), what research and large companies have found about review size, speed and effectiveness, how to make changes reviewable, a reviewing order from design to details, writing comments that help, the standard for approving, review latency as a team metric, reviewing AI-generated code, and the social side; with a lab that measures 643 merged pull requests from the Flask project.
Start here: what a healthy project's changes look like
The lab examined every pull request merged into Flask (a widely used Python web framework) through a merge commit between 2011 and 2023: 643 PRs. For each it measured lines changed, files touched, commits in the PR, and hours from the PR's first commit to its merge.
| Lines changed | PRs | Median files | Median commits | Median hours, first commit → merge |
|---|---|---|---|---|
| 0–9 | 340 (53%) | 1 | 1 | 2.8 |
| 10–49 | 153 | 2 | 1 | 12.4 |
| 50–199 | 93 | 4 | 1 | 5.8 |
| 200–499 | 28 | 8 | 2 | 13.3 |
| 500+ | 29 | 13 | 2 | 13.6 |
The median PR changed 8 lines; three-quarters changed 43 or fewer; only 1 in 10 exceeded 167. Larger PRs touched many more files and needed more rounds of commits. (Time to merge was noisy and not a clean function of size here; open-source PRs wait on volunteers' schedules, and commit timestamps can be rewritten by rebasing.)
Now the 1,800-line PR from TiffinBox's new hire, touching 31 files: by this distribution it's larger than 99% of Flask's merged changes (only 4 of 643 were bigger). No reviewer can review it well. The fix isn't a heroic review; it's helping the author split it, which is a mentoring conversation as much as a review (chapter 05).
How deep to go
Volume 17 defines six mastery levels. For code review:
| Level | What you can do |
|---|---|
| L1 | Submit changes with clear descriptions and respond to feedback |
| L2 | Review teammates' changes for correctness, tests and readability |
| L3 | Review for design and maintainability; make your own changes small and reviewable |
| L4 | Give feedback that teaches; unblock contentious reviews; spot systemic quality issues from review patterns |
| L5 | Set a team's review norms and tooling, and measure review health |
| L6 | Shape review culture across an organisation |
What review is for
Teams say they review to find bugs, and it does find them. But studies of what actually happens tell a broader story. A 2013 study at Microsoft (Bacchelli and Bird) found that while finding defects was the top expectation, a large share of review comments were about code improvements, understanding and alternative approaches, and that knowledge transfer and team awareness were among the main outcomes. Google's 2018 case study of its own review process (Sadowski and colleagues) described review primarily as a way to keep code readable and maintainable and to spread knowledge, with education an explicit goal.
So review does five jobs:
- Correctness: bugs, edge cases, security issues, concurrency problems.
- Design and fit: is this the right change, in the right place, consistent with the system?
- Maintainability: will the next person understand and safely change it?
- Shared ownership: at least two people know every change; the bus factor rises (chapter 05).
- Teaching: reviewers learn the code; authors learn from reviewers; norms spread.
Review is weaker at some things: it rarely finds deep performance problems or complex integration bugs, and it can't substitute for tests. Automate what machines check better.
Size and speed
Smaller is better
Evidence from industry consistently favours small changes. A widely cited study of about 2,500 reviews at Cisco (published by SmartBear in 2006) reported that reviewers' defect-finding fell off sharply beyond 200–400 lines per review and when reviewing faster than about 500 lines per hour, and that sessions longer than 60–90 minutes became less effective. It's a single-company study, but its direction matches everyone's experience. Google reported a median change size of a few dozen lines. The Flask lab: median 8 lines, 90% under 170.
Making changes small is the author's job:
- Separate refactoring from behaviour change: a rename touching 40 files is easy to review alone and impossible inside a feature.
- Stack changes: interface first, then implementation, then callers, each reviewable and safe to merge (behind a feature flag if incomplete; Volume 8).
- Split by layer or by vertical slice, whichever keeps each step coherent and tested.
- Delete in separate changes.
Faster is better
Review latency (time to first response and to approval) shapes everything else. When reviews take days, authors batch work into bigger changes, start parallel work (more WIP; chapter 07), and lose context. Common norms: respond within one business day (Google's public guidance says one business day at most), and reply faster to small changes; a quick "I'll look this afternoon" counts. Many teams track median time-to-first-review and treat it as a team health metric.
Speed doesn't mean shallow. A reviewer who can't review properly soon should say so and suggest someone else.
Making a change easy to review (the author's side)
- A description that answers why: the problem, the approach, alternatives rejected, how it was tested, and what to focus on. Screenshots for UI; before/after numbers for performance.
- Self-review first: read your own diff in the review tool before requesting review. You'll find the debug print, the stray file and the confusing name.
- Green CI before asking for review.
- Point to the risky parts: "the retry logic in
sync.ktis where I'm least sure". - Respond to every comment: fix it, explain, or agree to follow up (with a ticket). Don't silently ignore comments.
How to review (the reviewer's side)
Work from the outside in:
- Context: read the description and linked ticket or design doc. Is this the right problem?
- Design: is the approach sound and in the right place? Does it fit the architecture? Would a different design be much simpler? If the design is wrong, say so now and stop; detailed comments on code that will be rewritten waste everyone's time.
- Behaviour and correctness: edge cases, error handling, concurrency, security (input validation, authorisation, secrets; Volume 10), data migrations, backwards compatibility.
- Tests: do they test behaviour, including failure cases? Would they fail if the code were wrong?
- Readability: names, structure, comments that explain why.
- Details: smaller improvements, clearly marked as optional.
Run the code or check out the branch when behaviour is subtle. Read the tests first sometimes: they show what the author thinks the code does.
Writing comments that help
- Be specific and explain why: "This query runs once per order in the loop (N+1); fetching all kitchens in one query first would avoid ~200 round trips on the plan screen."
- Mark severity so authors know what blocks merging. Many teams use labels such as Conventional Comments (
issue:,suggestion:,question:,nit:,praise:) and treat unlabelled comments as blocking. - Ask genuine questions when unsure: "What happens if the kitchen is deleted while this runs?" is better than asserting a bug that isn't there.
- Comment on the code, not the person: "this function" not "you".
- Offer a concrete alternative when you criticise a design, or say you don't have one.
- Praise what's good: it reinforces the patterns you want and makes criticism easier to hear.
- Move long debates to a call, then record the outcome in the thread.
The approval standard
Google's public reviewer guide puts the standard well: approve once the change definitely improves the overall health of the code, even if it isn't perfect. Reviewers who demand their own preferred style stall teams; reviewers who wave through anything let quality decay. Things that block: bugs, security problems, missing tests for important behaviour, designs that will be expensive to undo, and violations of agreed standards. Things that don't: personal preferences, hypothetical future needs, nits.
If you and the author disagree after discussion, prefer the codebase's existing conventions and agreed guides; escalate to a tech lead only for genuinely important disagreements, and record the result as a guideline so it isn't argued again.
Automate the mechanical
Formatters (Spotless/ktfmt, Prettier, Black/Ruff), linters, static analysis, type checkers, dependency and secret scanners, test coverage reports and size warnings run in CI so humans spend review attention on design and behaviour. Remove a lint rule rather than repeatedly overriding it in review.
Reviewing AI-generated code
AI assistants now write a large share of many teams' first drafts (Volume 14). The craft adjusts:
- The author owns every line: they must understand, test and be able to explain the change. "The assistant wrote it" isn't an answer to a review question.
- Size discipline matters more: generating 800 lines is easy; reviewing them isn't. Keep AI-assisted changes as small as hand-written ones.
- Look for its typical faults: plausible but non-existent APIs, subtly wrong edge cases, duplicated code rather than reuse, outdated patterns, over-broad error handling, and tests that assert what the code does rather than what it should do.
- AI review tools can catch issues and summarise changes, but treat them as one more automated check, not as the reviewer of record.
Review health as a team
Track a few measures, and use them to fix the process, never to rank individuals:
| Measure | Healthy direction |
|---|---|
| change size (median, p90) | small |
| time to first review | hours, not days |
| review rounds per change | few |
| share of changes reviewed by someone outside the author's sub-area | some, for knowledge spread |
| reviews done per person | spread across the team, not concentrated on one senior |
Seniors who review everything become bottlenecks and single points of knowledge; teach others to review your areas.
Common traps
- Giant PRs, and approving them unread.
- Nitpicking while missing the design problem.
- Unlabelled comments, so authors can't tell what blocks.
- Slow reviews, which grow PR size and WIP.
- Style debates that a formatter should settle.
- Harsh or personal comments, especially to new team members.
- Treating AI-generated code as pre-reviewed.
Lab: the shape of 643 merged pull requests
Setup. A full clone of the Flask repository (github.com/pallets/flask, 5,566 commits at the time of the lab) and a Python script using only git. It walks the main branch's first-parent merge commits whose subject is "Merge pull request #N" (Flask merged PRs this way until 2023, then switched to other merge styles, so later PRs aren't included), measures each PR's diff against the main branch (git diff --numstat merge^1 merge: lines added plus deleted, files touched), counts the PR's commits (merge^1..merge^2), and computes hours from the earliest commit's author time to the merge. Binary files are skipped. This is real history, not a simulation, but it describes one open-source project; a company team's numbers will differ.
Results
- 643 PRs, merged between 28 June 2011 and 1 May 2023.
- Lines changed: p25 2, median 8, p75 43, p90 167, maximum 9,193.
- Only 4 of 643 changed 1,800 lines or more.
| Lines changed | PRs | Median hours first commit → merge | Median commits | Median files |
|---|---|---|---|---|
| 0–9 | 340 | 2.8 | 1 | 1 |
| 10–49 | 153 | 12.4 | 1 | 2 |
| 50–199 | 93 | 5.8 | 1 | 4 |
| 200–499 | 28 | 13.3 | 2 | 8 |
| 500+ | 29 | 13.6 | 2 | 13 |
What it shows
- Most changes in a mature, well-reviewed project are tiny: half change 8 lines or fewer.
- Size brings breadth: large PRs touch a median of 13 files, so a reviewer must understand much more of the system at once.
- Large PRs take more rounds (more commits per PR).
- Latency isn't explained by size alone in volunteer-run projects: the smallest PRs merged fastest, but the middle buckets aren't ordered. Inside a company, where reviewers are colleagues with norms, latency is more controllable and worth measuring directly.
- Limits of the method: first-commit time is the author's timestamp, which rebasing can change; PRs merged by squash or rebase aren't counted.
Try it yourself
- Run the script on your team's repository (adapt the merge-subject pattern) and compare the size distribution with Flask's.
- Add "time from PR opened to first review" using your code host's API, and plot it against size.
- Take your largest recent PR and plan how you'd split it into three to five reviewable steps.
- Review a 400-line change in one sitting and a 100-line change, noting how many issues you find per 100 lines in each.
- Adopt Conventional Comment labels for two weeks and ask authors whether it was clearer what blocked merging.
Revision sheet: code review
Key ideas
- Review finds defects but mainly builds shared understanding, maintainability and skills.
- Small changes get better, faster reviews; making them small is the author's job (separate refactors, stack changes, flags).
- Fast reviews (within a business day) keep changes small and flow high.
- Authors: description with why, self-review, green CI, point to risk, answer every comment.
- Reviewers: context → design → correctness → tests → readability → details; stop at a wrong design.
- Comments: specific, with reasons, severity-labelled, about the code, with alternatives and praise.
- Approve when the change improves code health with no blocking issues.
- Automate formatting and mechanical checks; authors own AI-generated code fully.
- Measure size, latency, rounds and reviewer spread to improve the process, not to rank people.
Numbers to remember
- Flask: median PR 8 lines; p90 167; 500+ lines → median 13 files.
- Cisco/SmartBear (2006): effectiveness drops beyond 200–400 lines per review and above ~500 lines/hour.
Glossary
- Stacked changes: a series of dependent small PRs.
- Conventional Comments: labels such as
issue:,suggestion:,nit:,praise:. - Review latency: time until a change gets its first review or approval.
- Rubber-stamping: approving without real review.
- Code health: how easy the codebase is to understand and change safely.