Code review is one of the most important practices engineers should have. Your code will always need to be reviewed, and you will always need to review your coworkers' code. If you approach code reviews as a learning process, everyone wins.
Code reviews are one point of contact between engineers during the development process. They are the perfect time to ensure that the team is in sync regarding standards, best practices, and code style.
When done properly, the team will advocate for best practices and share knowledge to raise the average level of the engineering squad. That way, Junior developers will be able to learn quickly and reach their maximum potential with peer feedback.
Code reviews ensure that:
the test code is robust and valid,
the design and implementation are consistent across the application.
They are not about finding bugs, that's what tests are for.
As Google says, “technical facts and data overrule opinions and personal preferences.” (Google principles for code reviews). As a reviewer, if you request changes on the current implementation, provide resources and explain the benefit of your solution with specific pieces of knowledge. If no resources are found about a specific topic, reach approval and create some documentation for the next time this conflict appears.
You are reviewing a teammate or are being reviewed by one. The goal of a code review is to share knowledge, not to hurt the ego. If you are reviewing or responding, think before sending a derogatory comment.
Most productive engineering teams have a detailed process for code reviews. In this document, explain who you should be asking for a review (is it in a squad? depending on the updated code?), how many approvals do you need (generally from one to three), how to create tests before a code review, and which checks are mandatory to pass before merging the pull request.
If your team does not have a detailed process yet, it is ok to ask for a review. Ping the people you think should review your code, do not wait for someone to find it by themselves.
Code reviewing should be a daily habit. As our focus decreases over time, it is best to review code in short sessions and often.
You won't be able to provide an effective code review if you do not understand what you're looking at.
Having a brief checklist before looking at the code is a great way to be sure that that the code is ready to be reviewed. Inspired by Hugo Dias in his code review article, here is a template checklist that we advise you to customize and follow:
If an item is checked, you should ask for more information before digging into the code.
You don't want to be the reviewer that approved a pull request where the code is not working. Approving without testing is hazardous. No matter the number of lines changed, you should test the code.
According to Google code review guidelines about speed, a slow code review process has three negative consequences.
The product development process slows down. Not reviewing someone's code might fasten your work, but you're slowing down the rest of the team.
The team starts to protest the code review process. Waiting days for someone to review a few lines of code is frustrating, especially if the review ends by requesting substantial changes.
Code health is deteriorating. It's easier to accept refactoring or cleanups when you are not yet working on another issue. Fast code reviews raise the code health level.
If you receive a code review notification and are not working on a focused task, try to review the code as soon as possible. If you do not have time during your day to review the code, do it first thing the next morning.
Separating large tasks into small pull requests helps the team gain in velocity for two reasons:
Small pull requests are easier to review, so reviewed faster.
Small changes to the codebase are easier to dive into and understand. So code reviews are more effective.
The team behind Accelerate: The Science Behind Devops noticed that most efficient engineering teams were working on independent and short-lived branches. That way, every engineer focuses on only one subject at a time, and code reviews are more manageable.
Furthermore, in Best Practices for Peer Code Review, SmartBear conducted a study and found out that developers should not review more than 400 lines of code at a time. Beyond 400 lines, the ability to identify defects diminishes as our brain can not process so much information.
So next time your pull request has more than 400 lines changed, try to separate it into two different pull requests! The best practice is to open stacked pull requests. They are feature branches that are checked out from other feature branches to build small and coherent units to represent changes.
A good pull request provides content for the reviewer. If the author of the pull request wrote some comments on the code in tricky parts, it will help the reviewers and prevent avoidable questions from being asked. Provide screenshots, write comments and help your reviewer better understand your work before requesting a review.
A good pull request template will help you better prepare for the review. They are useful to avoid common mistakes, provide clear expectations on what should be ready when requesting a review, and avoid useless back-and-forth during the code review. A checklist inside your pull request template is usually the easiest way to prepare your pull request. You should update your template along the way to customize it to your needs.
If styling is important for your team, you should create a style guide. As we said in our first post, what is not written does not exist. Develop your style guide with the approval of the whole team and what is not inside it, will be a matter of personal preferences.
Here are our internal rules! What do you think?
First Published here