1. Understand the User's Goal: The user wants a description of the "process of code review." This is a standard software engineering question. The goal is to provide a clear, structured, and comprehensive explanation of how code reviews work in a professional setting.
-
Target Audience: Likely a software developer, student, or someone involved in software development processes (like a PM or lead). The tone should be professional, informative, and practical.
-
Key Concepts to Cover:
- Definition: What is it? (Systematic examination of source code).
- Purpose: Why do we do it? (Bugs, quality, knowledge sharing, style consistency).
- The Lifecycle/Steps: This is the core of the prompt.
- Preparation (Writing good code).
- Submission (Opening a Pull Request/Merge Request).
- The Review (The actual inspection).
- Discussion/Correction (Fixing issues).
- Approval/Merging.
- Best Practices: How to do it well. (Be kind, automated tools, context).
- Tools: What software is used? (GitHub, GitLab, Bitbucket, Gerrit).
-
Structuring the Response:
- Introduction: Brief definition and value proposition.
- The Step-by-Step Process (The "Meat"): Break this down into chronological phases (Preparation -> Submission -> Review -> Revision -> Conclusion).
- What Reviewers Look For: Specific criteria (Logic, Style, Security, etc.).
- Best Practices/Guidelines: "Soft skills" aspect (Constructive feedback, not nitpicking).
- Automation: Mentioning CI/CD.
- Conclusion: Summary.
-
Drafting the Content (Iterative Refinement):
-
Intro: Start strong. Code review is a quality assurance activity. It happens before merging.
-
Phase 1: Author's Prep (The "Ask"):
- Self-review first.
- Small batches (atomic commits).
- Context: Write a good PR description. Why am I doing this?
-
Phase 2: The Review (The "Action"):
- Who does it? Peers.
- What do they check?
- Functionality (Does it work?)
- Design (Is it clean?)
- Complexity (Is it too clever?)
- Tests (Are there unit tests?)
- Style/Naming.
-
Phase 3: The Feedback Loop (The "Conversation"):
- Comments vs. Blocking issues.
- Differentiation: "Must fix" vs. "Nitpick".
- Discussion about trade-offs.
-
Phase 4: Resolution:
- Author fixes issues.
- Push new commit.
- Re-review (optional but common).
-
Phase 5: Merge:
- Approval given.
- Merge into main branch.
-
Best Practices Section:
- For Authors: Don't dump 2000 lines. Provide context.
- For Reviewers: Be nice. Explain why something is wrong. Don't block on personal preference.
-
Refining Terminology:
- Use standard terms like "Pull Request" (PR) or "Merge Request" (MR).
- Mention "Linters" and "CI/CD".
-
Self-Correction/Review during drafting:
- Initial thought: Just list the steps 1-5.
- Correction: That's too dry. I should also explain what is being looked for and how to behave. The human element is crucial in code reviews.
- Addition: Emphasize that it's not just about finding bugs; it's about knowledge sharing. That's a huge benefit often overlooked.
-
Final Polish (Simulated Output Structure):
- Title: The Code Review Process.
- Overview: Short summary.
- The Workflow (Step-by-Step):
- Coding & Self-Review.
- Submission (PR).
- Automated Checks.
- Peer Review.
- Discussion & Iteration.
- Merge.
- Review Criteria (What to check): Correctness, Design, Readability, Testing.
- Best Practices: Golden rules for authors and reviewers.
- Conclusion.
-
Final Output Generation: (This matches the detailed response provided previously.)
Code review is a systematic examination of computer source code. It is intended to find bugs and improve the quality of the code before it is merged into the main codebase. While the specifics vary by company and team, the process generally follows a standard lifecycle.
Here is a description of the process, broken down by phase and best practices.
1. The Preparation (The Author)
Before a review is requested, the author (the person who wrote the code) must prepare the submission. Sending "messy" code is the quickest way to annoy reviewers and slow down the process.
- Self-Review: The author reviews their own
git diff first. They look for obvious logic errors, leftover debug code (like console.log), and formatting issues.
- Run Tests: The code must compile and pass all existing automated tests. A build that fails should never be sent for review.
- Linting/Formatting: Automated tools (like Prettier, ESLint, or Black) should be run to ensure the code adheres to style guidelines. Reviewers should not waste time commenting on indentation or spacing.
- Drafting the Description: The author opens a Pull Request (PR) or Merge Request (MR). They must write a clear description explaining:
- What changed?
- Why it changed (linking to a Jira ticket or bug report)?
- How to test it manually (if applicable).
2. The Assignment (The Hand-off)
Once the PR is ready, the author assigns reviewers.
- Selecting Reviewers: Usually, this involves 1 to 3 people.
- Domain Experts: People who know that specific part of the code well.
- Generalists: People who can spot architectural issues or logic errors.
- Automation: In many teams, bots automatically assign reviewers based on which files were edited (using tools like CODEOWNERS in GitHub).
3. The Review (The Reviewer)
This is the core of the process. The reviewer reads through the code changes. A good reviewer looks at several layers of the code:
- Correctness: Does the code do what it is supposed to do? Are there logic errors or edge cases (e.g., null pointers) missed?
- Design & Architecture: Is the code structured efficiently? Does it follow the project's design patterns? Is it scalable?
- Readability & Maintainability: Is the code easy to understand? Are variable names descriptive? Are functions too long?
- Security: Are there vulnerabilities like SQL injection risks or improper authentication checks?
- Tests: Did the author write new unit tests or integration tests to cover the new functionality?
4. The Discussion (The Feedback)
Reviewers leave comments on specific lines of code. Feedback usually falls into three categories:
- Blocking / Critical: "There is a security flaw here; this must be fixed before merging."
- Suggestion / Question: "Have you considered using a different library here? This approach might be slow." (Non-blocking, but encouraged).
- Nitpick: "You misspelled 'variable'." (Minor issues that do not affect functionality).
The Golden Rule: Comments should be about the code, not the coder. Feedback should be constructive.
5. The Iteration (The Fixes)
The author reads the feedback and takes action:
- Agreement: The author fixes the code, pushes a new commit, and marks the conversation as "Resolved."
- Discussion: If the author disagrees with a comment, they reply with a justification. (e.g., "I didn't use that library because it adds too much bloat to the bundle size.")
- Offline Discussion: If a debate becomes lengthy or heated, it is best to hop on a video call or walk over to their desk to resolve it quickly. Do not argue in comments for 3 days.
6. The Approval and Merge
Once all "Blocking" comments are resolved and the reviewers are satisfied:
- Approval: Reviewers click the "Approve" button.
- Final Check: The Continuous Integration (CI) pipeline runs one last time to ensure the merge will not break the build.
- Merge: The PR is merged into the main branch. Usually, the author is the one who clicks "Merge," though some teams allow the reviewer to do it.
Best Practices Summary
For the Author (How to get your code merged fast):
- Keep PRs Small: A 50-line change is reviewed thoroughly. A 1,000-line change is usually just skimmed and rubber-stamped. Break large features into smaller chunks.
- Provide Context: Don't make the reviewer guess what the feature is supposed to do.
For the Reviewer (How to be a good teammate):
- Be Speedy: Try to review code within 24 hours. Code reviews are often the biggest bottleneck in software delivery.
- Distinguish Preferences from Standards: Don't block code just because you would have written it differently. Only block if it breaks rules or functions incorrectly.
- Explain the "Why": Instead of saying "Change this to X," say "If you change this to X, it will prevent memory leaks."
Why do we do this?
It is not just to find bugs. Code review serves three vital purposes:
- Knowledge Transfer: It ensures more than one person understands the code.
- Shared Ownership: It prevents "silos" where only one person can fix a specific part of the system.
- Consistency: It keeps the codebase looking like it was written by a single entity, rather than a chaotic mix of styles.