Course contents · Lesson 13 of 17

Code Review Basics: Giving and Getting Feedback

Git and Collaboration · Lesson 13 of 17 · 10 min Engineer

You open your first pull request at a new job, and by lunch it has six comments. One asks "What happens if the form is empty?", another says "Nit: this name could be clearer". Nobody is grading you. This is code review: a teammate reads every change before it joins main. This lesson explains why teams do it, how to make your change easy to review, what to check when you review, and how to word comments that help.

Why teams review code

Code review means someone other than the author reads a change and comments on it before it is merged. The author wrote the change; the reviewer reads it. On GitHub this happens on the pull request page which, as Branches and Pull Requests showed, holds the description, the diff (the lines the change adds and removes) and the checks in one place.

Teams spend that time for four reasons:

  • Catch bugs early. A mistake found in review costs one comment; found by a customer, it costs an urgent fix.
  • Share knowledge. The reviewer learns the new code, and the author learns from the reviewer's experience.
  • Keep the code consistent. Similar problems get solved in similar ways, so the project reads as one.
  • Spread ownership. At least two people understand each change, so one person's holiday does not leave code that nobody else can fix.

Review is a loop, not a single step. In the flowchart, each lane is a role. The automated checks run on every push, so a reviewer who waits for them reads code whose mechanical questions are already answered.

Flowchart in three lanes: Author, Automation and Reviewer. 1, the author opens the PR, small and explaining why. 2, automation runs the checks: format, lint, tests. 3, the reviewer reads and comments, with questions and must-fix labels, and submits one review: Comment, Approve or Request changes. A decision asks approved? If changes are requested, a dashed arrow goes up to 4, the author updates the branch by replying, fixing and pushing, and the push runs the checks again. If yes, 5, merge into main once approved with green checks. A banner says machines check every push; people judge the design
Every push sends the change through the machines again, so the reviewer's time goes to questions only a person can answer.

For authors: make your change easy to review

Keep it small. As the pull request lesson said, a good PR holds one change a reviewer can read carefully in one sitting. Google's code review guide explains why: small changes are reviewed faster and more thoroughly, are less likely to introduce bugs and are simpler to roll back, or undo.

Explain the why. The diff shows what changed; only you know why. In the description, say what problem the change solves, why you chose this approach and how you tested it, so the reviewer can judge whether the code reaches your goal.

Review it yourself first. Read your own diff as a stranger would. This command lists the files your branch changes. The three dots mean "since my branch split off main", the comparison GitHub's Files changed tab uses, and HEAD is the branch you are on, as What Is Git? explained:

git diff --stat main...HEAD

You should see one line per file, with the number of changed lines and a bar of + for added and - for removed lines, then a total (example numbers):

 contact.html | 12 +++++++++---
 contact.js   | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

Drop --stat to read every changed line, and look for leftover debug output, commented-out code and files you did not mean to touch. No output means you have not committed yet, because this compares commits only. If Git replies with an error starting fatal: ambiguous argument 'main...HEAD', your main branch has another name, such as master.

Answer every comment. Reply in its thread: fix it and say so, explain why the code should stay as it is, or ask what the reviewer means. Push the fixes to the same branch, then click Resolve conversation on each finished thread. The comment is about the code, not about you.

For reviewers: what to check and how to say it

Read the description first, so you know what the change is meant to do, then go through the diff file by file. The checklist holds the five questions to ask of every pull request.

A clipboard with five ticked rows. Correct: does it do what the PR says? Try empty, zero and missing values. Readable: could a new teammate follow it? Clear names, comments that say why. Tested: would a test fail if it broke? New behaviour comes with tests. Secure: could someone misuse it? Input checked, no secrets, permissions checked. Small: can you review it in one sitting? If not, ask the author to split it. A banner says spacing and style are left to the formatter
The five questions cover what tools cannot judge: whether the change is right, clear, proven, safe and small enough to judge at all.

For correctness, try edge cases, unusual inputs at the limits such as an empty form, a zero or a missing value, because that is where bugs hide. For security, check that user input is validated, that no password or key sits in the diff, and that the code checks who may do what.

Ask questions instead of giving orders. "What happens if the list is empty?" invites the author to look, and they may find a better fix than yours. "Handle the empty list." gives no reason. A question also leaves room for the chance that you missed something.

Say how much each comment matters. Google's guide suggests labels: Nit: for a minor point, Optional: for a good idea that is not required, and FYI: for something to think about later. Say plainly when something is must-fix. Then submit with Review changes as Comment, Approve or Request changes. Keep Request changes for must-fix problems such as bugs, because GitHub describes it as feedback that must be addressed before merging.

Tone: criticise the code, not the person

Written comments lose your tone of voice, so a quick note can read as harsh. Compare the two comments below on the same line of code. Both spot the same bug, but only one tells the author what is wrong and what to do next.

One line of code under review, contact.js line 14, saveMessage(form.email, form.text). Before, blunt: ravi commented, this is wrong, did you even test it? It judges the person, gives no reason and offers no next step. After, kind and specific, labelled must-fix: what happens if email is empty? We'd save a message we can't answer. Could we check it before saving? It is about the code, explains why and suggests a next step. A band below adds a nit, maybe call the check hasEmail, and praise, nice test for the empty form
The kind comment is also the more useful one: it names the risk, says why it matters and points to a fix.

Talk about the code ("this function skips…", "could we…") rather than the author ("you forgot…"). If a thread goes back and forth several times, stop typing and talk; a short call settles what ten comments cannot.

Give praise where it is earned. "Nice test for the empty form" tells the author what to keep doing, and Google's guide notes that people learn from what they did well, not only from what they could do better. Keep praise specific and honest.

Let automation do the boring parts

People are slow and inconsistent at mechanical checks, and arguing about spaces wastes review time. So teams hand those jobs to tools that run as checks on every push:

  • A formatter rewrites code into one consistent layout, such as Prettier for JavaScript or Black for Python, so nobody comments on spacing.
  • A linter, as you saw with pull request checks, flags style problems and likely mistakes, such as a variable that is never used; ESLint is one for JavaScript.
  • Tests run the code and report what broke; they get their own lesson next, in Software Testing Basics.

No tool can tell you whether the change solves the right problem, fits the design or is simpler than it could be. That is the reviewer's job. A useful rule: if the same style comment comes up twice, add it to the formatter or linter settings instead of typing it again.

Check yourself

A reviewer and an author have argued about spacing in three pull requests this month. What should the team change?

Add a formatter or linter rule that runs as a check on every push. The tool settles style the same way every time, so review time goes to logic and design.

Your PR adds a feature, renames files across the project and upgrades a library. Why split it before asking for review?

Each smaller PR can be read carefully in one sitting, so bugs are easier to spot, and a part that causes trouble can be rolled back on its own.

The code works, but you think one variable name could be clearer. Should you submit the review as Request changes?

No. It is a nit, so label it Nit: and approve or comment. Request changes means something must be fixed before merging, so keep it for real problems such as bugs.