Sub topic 6
Reviewing the diff and commenting
This chapter assumes you can create a branch, commit, and push, and that you have opened a pull request (PR) as covered in earlier chapters. Here you learn how to read a PR's diff and leave useful comments.
Start with the conversation, then the diff
Open the PR and read the title and description first. They tell you what the author intended. Then open the Files changed tab, which shows the diff: the lines added (green) and removed (red) between the base branch and the head branch.
Large PRs are easier to review in pieces. Useful options on the Files changed tab:
- Use the file tree to jump between files.
- Tick Viewed on each file you finish. GitHub remembers this and shows your progress.
- Choose the unified or split view in the diff settings.
- Hide whitespace-only changes when a reformat buries the real edits.
- Select a single commit from the commit list when the PR tells its story commit by commit.
Comment on a line
Hover over a line number in the diff and click the blue + button. To comment on several lines, drag across the line numbers first. You can also comment on a whole file.
Each comment starts a conversation on that spot. Others can reply, and the author can later resolve it.
Single comments versus a review
When you add a comment, GitHub offers two buttons:
- Add single comment posts it immediately and notifies the author.
- Start a review (or Add review comment) saves the comment as pending. Nobody sees it until you submit the review.
Prefer a review for anything beyond one remark. The author gets one notification with all your feedback, and you can edit or delete pending comments before submitting. Submitting is covered in a later chapter.
What to look for
A good review reads the code for more than style. Check these in order:
- Intent. Does the change do what the description says, and nothing more?
- Correctness. Look for edge cases, error handling, and off-by-one mistakes.
- Tests. Does a test cover the new behavior?
- Clarity. Could a new teammate understand it in six months?
- Style. Leave this to automated linters where possible.
Writing comments that get acted on
State what you see, why it matters, and what you suggest. Compare two comments on the same line:
- Weak: "This is wrong."
- Better: "If
itemsis empty,items[0]throws. Can we return early here?"
Ask questions when you are unsure, and mark minor points as optional, for example with "nit:". Link to documentation when you cite a rule.
Exercise
You have added six line comments to a PR using "Start a review". The author has not been notified. Why, and what must you do?
Show answer
The comments are pending and visible only to you. You must submit the review, choosing comment, approve, or request changes, so GitHub publishes them and notifies the author.