Code Review
The main reason to do code reviews is to catch bugs before they're checked into the project. A reviewer can spot problems by understanding the code and its dependencies. They might catch assumptions made in the code, misuse of existing code, or changes that could break other parts of the system.
Not just about bugs
Code reviews are useful, but not always effective in finding bugs. Here's how our team does code reviews:
- Two people sit down together, screen to screen, and go through the changes.
- The reviewer asks questions and points out potential issues, while the reviewee explains their code.
- The reviewee takes notes on suggested changes and tests to run.
- The code review takes anywhere from 5 minutes to 5 hours, and often results in changes before check-in.
You do find bugs, but not in the way you might expect. Here are the three main ways we discover bugs:
- Self-review: Before asking for a review, you review your code yourself and find bugs.
- Review discussion: During the review, you explain your code and realize it has flaws.
- Reviewer's feedback: The reviewer points out a problem you didn't catch.
Sharing knowledge
A crucial reason to conduct code reviews is to share knowledge among team members. This is especially important for your team, where coders can work on various parts of the codebase.
Imagine dividing the team into "junior" and "senior" groups based on their familiarity with the codebase. Senior coders know the codebase well, while junior coders are still learning. There are four possible code review combinations: two useful, one forbidden, and one with a senior reviewing another senior.
Code reviews can be beneficial in three ways:
- A senior coder reviewing a junior coder's work can spot problems, such as formatting issues or misunderstandings, and provide guidance on improving the code.
- A junior coder reviewing a senior coder's work can ask questions and learn from the process, gaining a better understanding of the codebase and its structure.
- Two senior coders reviewing each other's work can identify bugs, discuss assumptions, and ensure that both coders understand the changes.
These interactions are part of the education process for new team members. Code reviews are an excellent way to transfer informal knowledge and ensure that all team members have a basic understanding of how the codebase works.
In summary, code reviews are essential for knowledge sharing among team members, particularly in our team where flexibility in assignments is key.
Benefits of Code Reviews
Code reviews have three benefits:
- Find and fix bugs.
- Everyone understands the code better.
- People write code they're proud to share.
Code reviews are social interactions between two people. They're not a solitary activity. Most value comes from discussing the code. You might find a bug, learn a new technique, or clean up code before sharing it.
To get the most out of code reviews, make sure they're interactive. If the review is quiet or arguments ensue, something is wrong.
A healthy code review benefits both the codebase and the team. It's a professional and open conversation where both participants learn something new.