Code Review 基础
Reviewing 拉取请求 effectively — giving constructive feedback and using review checklists.
你将学到
- 如何 review 拉取请求 systematically
- Giving constructive, kind feedback
- 什么到 look for: logic, security, 样式, 测试
- Using review tools and inline comments on GitHub
概念讲解
Why Code Review Matters
Code review is the practice of having another developer examine your code before it merges. It catches bugs, improves code quality, shares knowledge across the team, and ensures more than one person understands each part of the codebase.
A good review process is the single most effective quality practice in software development. Studies show it catches 60-90% of defects before they reach production.
The Review Checklist
When reviewing a PR, check these areas:
1. Correctness
- Does the code do what the PR description says?
- Are edge cases handled (null, empty, negative, overflow)?
- Are error paths covered?
- Does the logic match the requirements?
2. Security
- Are user inputs validated and sanitized?
- Are secrets, keys, and passwords kept out of the code?
- Are SQL queries parameterized (no injection)?
- Are authentication and authorization checks present?
3. Tests
- Are there tests for the new code?
- Do the tests cover edge cases, not just the happy path?
- Do the tests actually assert meaningful behavior?
- Will the tests pass in CI?
4. Style and Readability
- Does the code follow the project's style guide?
- Are names clear and descriptive?
- Is complex logic explained with comments?
- Is the code DRY (no unnecessary duplication)?
5. Performance
- Are there obvious performance issues (N+1 queries, unnecessary loops)?
- Are database queries efficient?
- Are large allocations or copies avoidable?
6. Design
- Does this change fit the existing architecture?
- Are responsibilities in the right modules?
- Is the public API well-designed?
How to Review on GitHub
- Open the PR and read the description
- Check out the branch locally for complex reviews:
git fetch origin
git checkout feature-branch
# run the code, run the tests
- Review the diff file by file on GitHub
- Leave inline comments by hovering over a line and clicking the
+icon - Suggest changes with the suggestion button (creates a code suggestion the author can apply with one click)
- Submit your review — choose "Comment", "Approve", or "请求更改"
Giving Constructive Feedback
Good review comments are kind, specific, and actionable:
- ✅ "这个循环能是 replaced 使用筛选() call for clarity. 什么做你 think?"
- ✅ "I think 那里's 边缘情况当
用户是空 — reduce 会 throw. 能我们添加 guard?" - ✅ "Nice use 的策略模式这里! One suggestion: 函数 name 能是更多特定, 如
calculateDiscount相反的calculate."
Bad review comments are vague, harsh, or unhelpful:
- ❌ "这个是错误."
- ❌ "为什么会你编写它这个方式?"
- ❌ "坏 code."
The Review Etiquette
- Be kind — there is a human on the other side. Critique the code, not the person.
- Be specific — point to exact lines and explain the issue.
- Ask questions — "能这个是空这里?" is better than "这个将 crash."
- Praise good work — highlight things done well, not just problems.
- Respond to feedback — if you are the author, address every comment, even if just to acknowledge it.
- Don't block on style nits — use "nit:" prefix for minor style suggestions that should not block the merge.
Reviewing Your Own Code First
Before requesting review, self-review your PR:
- Read through the diff on GitHub
- Check that you have not left debug code (console.log, print statements)
- Verify the CI checks pass
- Make sure the description is complete
Self-review catches many simple issues before they reach your reviewer.
示例代码
# --- Check out a PR locally for thorough review ---
# Method 1: fetch and checkout the branch
git fetch origin pull/42/head:pr-42
git checkout pr-42
# Method 2: using GitHub CLI
gh pr checkout 42
# Run the code and tests locally
npm install
npm test
npm run lint
# --- Review workflow on GitHub ---
# 1. Read the PR description
# 2. Review the diff file by file
# 3. Leave inline comments on specific lines
# 4. Use the suggestion feature:
# - Click the "+" icon next to a line
# - Write your suggestion in a code block
# - The author can apply it with one click
# --- Submit the review ---
gh pr review 42 --approve --body "Great work! Just a few nits inline."
gh pr review 42 --request-changes --body "Please address the security concern about SQL injection."
gh pr review 42 --comment --body "Looks good overall, leaving some questions inline."
# --- Apply a suggestion from a reviewer (as the PR author) ---
# On GitHub, click "Apply suggestion" on each suggestion comment
# Then push the changes:
git push
# --- View review status ---
gh pr view 42 --comments
gh pr checks 42 # see CI status
# --- Keep the PR branch updated with main ---
git fetch origin
git rebase origin/main
git push --force-with-lease
Shows 检查 out PR locally for thorough review, 运行测试, 提交 reviews 通过 GitHub CLI (approve, 请求更改, comment), applying reviewer suggestions, and keeping PR 分支更新. gh pr checkout command 是 fastest 方式到 review locally.
动手试一试
- Code Diff
Compare code versions — 核心的 reviewing PR diff.
- CI/CD YAML Generator
Generate CI 检查那个运行 automatically on PRs for review.
常见错误
错误写法
Rubber-stamping PRs 不使用 actually 读取 code
正确做法
review 是仅 valuable 如果你 actually 读取 code. 读取 diff, 理解更改, 检查边缘情况 and 测试. 如果 PR 是也 大到 review thoroughly, ask author 到拆分它. 'LGTM' 不使用读取 helps 否 one.
错误写法
是 harsh or personal in review comments
正确做法
Review code, 不 person. Use phrases 如 '这个能 是 simplified 通过...' 相反的 '这个是 坏'. Ask 问题而不是制作 demands. Remember 那里是 colleague on other 边. Kind reviews 构建 trust; harsh reviews 创建 resentment and 慢下 team.
相关资源
相关速查表
- Git Cheatsheet
PR checkout, review, and 合并 commands.
练习 Code Review 基础
1 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。