Effective Code Review Best Practices and Habits

Best practices for code review

Effective Code Review Best Practices and Habits

Set clear standards for quality and pull request workflow so reviewers avoid arguing over style, letting automation handle formatting instead.

Improve your reading skills by running the code locally and focusing on the specific purpose of the change rather than trying to learn the entire codebase.

Communicate with the author, explain your reasoning when giving feedback, and document your testing steps and design choices in the pull request description.

Key practices
  1. Clarify expectations Set standards and procedures to avoid style arguments.
  2. Practice code reading Read diverse codebases and run code locally to understand features.
  3. Leverage automation Use linters and spellcheckers in the pipeline for basic checks.
  4. Foster collaboration Discuss code with authors and explain feedback reasoning.
Effective Code Review Best Practices and Habits — infographic

To conduct effective code reviews, focus on clarifying expectations, practicing code reading, leveraging automation, and fostering collaborative communication. This approach helps ensure thoroughness without hindering development speed.

Clarify Expectations and Scope

Establish clear guidelines for code quality and review goals. "I understand the desire to be egalitarian/inclusive but having clear standards and procedures for PR workflow/coding standards is best, letting people argue about "style" just causes arguments and hurt feelings with little upside."
Prioritize critical issues over minor stylistic preferences to avoid unnecessary delays. "Why dont people make comments then approve if the PR works and improves the codebase?"
Ensure pull requests are small and focused to facilitate quicker and more effective reviews. "Also, huge PRs and badly written/organized code are always gonna be a pain to review."

Cultivate Code Reading Skills

Practice reading diverse codebases, including open-source projects, to develop a nuanced understanding. "You kinda just have to read more of other people's code to get used to it and develop your own system for inspection."
Run the code locally to understand its functionality and identify potential issues. "Run it locally - this may help you understand the feature itself better, then the code understanding will follow."
Focus on the code's purpose and how it fulfills the pull request description, rather than trying to comprehend the entire system immediately. "When reviewing, don't try to understand the entire codebase first."

Leverage Automation and Tools

Utilize linters, autoformatters, and spell checkers to handle stylistic and basic hygiene checks automatically. "Many of your issues are solved by linters, autoformatters, and spellcheckers that can be setup in the CI pipeline."
Consider AI-powered code review tools for identifying common mistakes, though be mindful of costs and potential for false positives. "I find using gpt to review opus, or vice versa helps achieve a stable state the best, but it can take quite a few pass sometimes."
Integrate automated reviews into your CI/CD pipeline, ideally at PR creation, to catch issues early. "My company does use it and we do it on PR creation."

Foster Collaborative Communication

Discuss code with the author to understand their logic and reasoning. "We do our code reviews as a team. This lets the developer explain their logic and code—we're a mixed bag, half front-end, half back-end, so it's a learning opportunity for everyone."
Provide constructive feedback that explains the reasoning behind suggestions. "What sets it apart from some of the ones you listed is how it approaches the actual review feedback like it doesn't just point out issues but explains the reasoning in a way that's actually useful for learning instead of just being another linter with fancy branding."
Document testing steps and design decisions within the pull request description to provide context for reviewers. "Finally, writing testing steps for a PR in the description can go a long way towards documenting the feature and why certain decisions were made."

Are you currently struggling with a specific aspect of code review in your team?

Key takeaways
  • Establish clear guidelines to prevent style debates and hurt feelings.
  • Keep pull requests small and focused.
  • Use linters and autoformatters for basic hygiene.
  • Run code locally and focus on the feature's purpose.
  • Explain reasoning in feedback and document design decisions.
Common mistakes to avoid
  • Leaving style and basic hygiene checks to human reviewers instead of automation.
  • Trying to understand the entire codebase before reviewing a specific change.
  • Submitting huge pull requests that are a pain to review.
  • Giving feedback without explaining the reasoning behind it.
Quick tips
  • Read more open source code to develop your own system for inspection.
  • Run the pulled code locally to understand the feature before reading the logic.
  • Put testing steps in the pull request description to document the feature.
  • Use AI to review your code, but expect some false positives.
FAQ
How do you handle arguments over code style?
Set clear procedures and coding standards. Let automation like linters and autoformatters handle style rules instead of leaving it up to personal preference.
What makes a pull request easier to review?
Keep pull requests small and focused. Include testing steps and explain design decisions right in the description.
Should you try to understand the whole codebase before reviewing?
No, focus on the specific purpose of the change and how it fulfills the pull request description. You can also run the code locally to understand the feature better.
Can AI tools help with code review?
AI tools can help find common mistakes and reach a stable state, but watch out for false positives and extra costs.
Comments (0)

No comments yet. Start the conversation.