Bitpath

Reading & Reviewing Code

Most of the job is reading code you did not write. Learn to enter an unfamiliar codebase, read a diff for what it changes and what it breaks, and give and receive reviews that make the code better.

  • Intermediate
  • 4 units, 12 lessons, 144 exercises
  • Your language, diffs
  • Builds on Clean Code & Refactoring; Git Internals helps

Unit 01 · 3 lessons

Finding your way

Enter an unfamiliar codebase and find the code that matters

  1. > start01Where to start
  2. 02Trace it
  3. BOSSA small codebase
Unit prize
  1. 01
    Where to start

    README, entry point, tests, then one request

  2. 02
    Trace it

    Follow calls down and values back

  3. BOSS
    A small codebase

    Four files, one picture in your head

Read the unit guide

Nobody reads a codebase from the first file to the last. You read it the way you use a map: find where you are, find where you are going, and follow one road between them.

Start outside. The README says how to run the thing. The manifest (a package.json, a pyproject.toml) says what it is built on and which command starts it. The entry point is where execution begins; from there, a table of commands or routes usually tells you which function handles what. The tests say what the code is supposed to do, in a form that cannot lie for long.

Then pick one concrete question and follow it. "What happens when this runs?" is read downward: from the entry point, call by call. "Where does this value come from?" is read backward: from the line that uses the value to every line that could have set it. Searching for a name and reading each call site answers "who depends on this?".

While tracing, read every exit of a function, not just the happy one: the guard clause at the top, the early return inside the loop, the path where no condition matches. Ask what a call changes as well as what it returns. A side effect never shows at the call site.

After a few functions you can draw a call map: who calls whom. That map is the mental model. With it, a bug report or a change request becomes a lookup: rounding lives here, prices live there, and nothing above needs to know.

The classic mistake is changing what a function returns without reading its callers. The function is right, its tests pass, and everything that used the old meaning is now quietly wrong.

Unit 02 · 3 lessons

Reading changes

Read a diff for what it changes and for what it breaks

  1. 01The unified diff
  2. 02Spot the bug
  3. BOSSWhy is it like this?
Unit prize
  1. 01
    The unified diff

    Hunks, plus and minus, before and after

  2. 02
    Spot the bug

    Boundaries, missing cases, defaults, callers

  3. BOSS
    Why is it like this?

    Ask the history before you remove the fence

Read the unit guide

Most code you read at work arrives as a diff: a description of how to turn the old file into the new one. A unified diff names the two files (--- old, +++ new) and then lists hunks. Each hunk starts with a header such as @@ -11,6 +11,8 @@: it covers 6 lines starting at line 11 of the old file, and 8 lines starting at line 11 of the new one.

Inside a hunk the first character of every line is a code. A space is context: unchanged, shown so you can find the place. A minus exists only in the old file, a plus only in the new. There is no "edited" marker; an edit is a minus followed by a plus. Read only the context and minus lines and you have the old version; context and plus lines give the new one.

A diff shows what changed. Bugs live in what did not: the caller that still passes one argument, the code that relied on the old default, the user who is now nothing where a check used to be. So read the minus lines as carefully as the plus lines, and for every change ask who depended on the old behaviour. Then search for them, because they are not in the diff.

Five places repay attention: boundaries of any changed arithmetic or comparison, the empty and missing cases, changed defaults, changed signatures, and tests that became weaker. A commit message is a claim, not evidence. "Refactor", "tidy" and "simplify" promise no change in behaviour; check that promise by running the boundary case in your head.

History answers the question the code cannot: why. Blame maps each line to the last commit that touched it, which may be a formatter; walk back until a message explains the behaviour. Before you remove something that looks pointless, find out why it was put there. If the reason still holds, it stays. If the reason is gone, remove it in its own commit and say what made that safe.

Unit 03 · 3 lessons

Reviewing

Find what matters in a change, and say it so it lands

  1. 01What to look for
  2. 02Saying it
  3. BOSSReview a pull request
Unit prize
  1. 01
    What to look for

    Correctness, design, tests, then taste

  2. 02
    Saying it

    Specific, about the code, with a next step

  3. BOSS
    Review a pull request

    Description, tests, diff, verdict

Read the unit guide

A review answers one question: is this change safe and good to merge? Your attention is the scarce resource, so spend it in order of cost. Correctness first: wrong results, lost data, security. Then design: is this the right place and shape? Then tests: do they prove what the description claims? Then readability. Taste comes last, and anything a formatter or a linter can decide should be decided by a machine in CI, not by a person in a comment.

Read in an order that finds the expensive problems early. Start with the description: it is a list of claims. Then the tests: they show which claims are proven. Then the diff, with the questions from the previous unit: boundaries, missing cases, defaults, callers. Then try the inputs the author did not. Green checks mean the existing tests pass; they say nothing about the tests that were never written.

A finding is only useful if the author can act on it. A good comment says where, what happens (with an input that shows it), why it matters and what to do next. It is about the code, never about the person. When you are not sure, ask a real question. When something is good, say so: praise that is specific teaches as much as criticism.

Say how much each comment weighs. One widely used convention, Conventional Comments, puts a label in front: issue, suggestion, question, nitpick, praise, with a decoration such as (blocking) or (non-blocking). The author then knows which two comments must be handled before merging and which ten are theirs to take or leave.

The verdict follows from the comments. Approve when nothing blocking is left, even if nits remain. Request changes when something is, and name the input, the wrong result and a possible fix. "LGTM" on a diff you did not read is not a review. And a fix pushed in response is a new change: read it as carefully as the first.

Unit 04 · 3 lessons

Being reviewed

Send changes that are easy to review and safe to ship

  1. 01The author's side
  2. 02Risky changes
  3. BOSSCode nobody wrote
Unit prize
  1. 01
    The author's side

    One purpose, a real description, your own review first

  2. 02
    Risky changes

    Expand and contract, flags, and what cannot be undone

  3. BOSS
    Code nobody wrote

    Plausible is not correct: run it, read the tests

Read the unit guide

Most of the work of a good review is done by the author, before anyone else looks. A change that does one thing can be read in one sitting and judged against one claim. So split by kind: formatting and renames apart from behaviour, a bug fix apart from a feature, and tests together with the behaviour they prove. When pieces depend on each other, stack them and merge in order.

Then be your own first reviewer. Read the whole diff as a stranger would. Leftover prints, a check you disabled while debugging, a file you did not mean to touch: you can find these in a minute, and a reviewer skimming may not find them at all. Write a description that says what changed, why, how you tested it and what could go wrong. The diff shows what; only you know why.

Some changes are risky however clean the code is. During a deploy, old and new code run side by side, so a breaking change is split into steps that are each safe: expand (add the new beside the old), migrate (move writers, data and readers), contract (remove the old). Give new parameters a default that keeps the old behaviour. Put new behaviour behind a flag, turn it on for a few users first, and know which steps cannot be undone: deleted data does not come back with a redeploy.

When the comments arrive, they are about the code. Answer each one: fix it, explain with the case you ran, or move it to a follow-up and say so. Resist "while you are here" additions: they turn a small change back into a big one.

Generated code changes none of this, and raises the stakes. It is written to look right: it reads well and arrives with tests. Its typical failures are a rule that is almost right, a function that does not exist, a test that asserts little, and a test edited to agree with a bug. Whoever opens the pull request is the author and answers for every line, so run it, read it and cut it to a reviewable size first.

Learn Reading & Reviewing Code on your phone.

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy