Code Review Basics: Giving and Getting Feedback
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.
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.
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.
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.