Best practices for code review

Oshini Nugapitiya
9 min readMar 11, 2022

Imagine within the corporation, your colleague requested you to review a piece of code written by him. This can be a little nervous for you if you’re not very familiar with reviewing someone else’s code as it may seem like a test of your own abilities. But literally, it’s just another effective technique used by almost every software company to detect errors at an earlier stage of the software development life cycle. This article will provide some best practices you can use while reviewing the code.

What is a code review?

Code review is also known as peer code review. It is a systematic collaborative process that includes one or more team members checking on another teammate’s work to provide some comments, potential improvements or a list of questions to answer.

There are four common code review approaches.

1. The Email Thread

When a code is ready to review, the file will be emailed to the relevant partners to evaluate. This approach is more adaptable and technical than traditional techniques like gathering five people in a room to inspect the code. But the email thread of suggestions and different opinions can quickly become complicated while leaving the original coder to sort it all.

2. Pair Programming

As the name implies, pair programming is when two developers work on the same code together using a single machine. Here one programmer is writing the code while the other one is observing the code as it is written, proofreading and spell-checking it, and deciding the next steps. These two roles can be swapped at any time with the author becoming the observer and vice versa. This is a good technique for senior developers to mentor junior developers as it is an effective way to share knowledge and develop their interpersonal skills.

3. Over the shoulder

This is the easiest and informal of code reviews. Once the code is completed, simply find some qualified developers to stand over the author’s workstation and review the code while the author explains the purpose of the code and clarifies it to the reviewer. Over the shoulder, review leads the team to learn and share knowledge between developers and it is simple to deploy whenever you need it. But on the other hand, there is no documentation, metrics or tools to track and measure this procedure.

4. Tool-Assisted

Tool-assisted code review involves the use of software-based tools to facilitate the process of code review. In general, these tools can assist you with organizing and displaying files that have been updated, facilitating effective communication between developers and reviewers, providing automated metrics to understand the process. A code review tool automates the code review tool, allowing the reviewer to entirely focus on the code. To integrate a tool into your workflow effortlessly, you can select one that is compatible with your technologies.

Why code review?

Whether you are a lead software developer or a programmer, you might be ignoring the advantages of code reviews. When done correctly, peer review saves time and reduces a considerable amount of work required of QA teams. Code review is a part of the software development process which involves testing the source code and ensuring that your code is free of bugs at an early stage. It reduces the chances of getting any defects later. Also, it improves the code’s efficiency and confirms that the new code is complying with the guidelines. Code review helps to improve the knowledge of the team members. As the code review is often performed by senior software developers, a junior developer can use the advice or comments to enhance his/her own coding skills.

Code review best practices

Code review is all about collaboration over competition. We’ve got some simple tips and best practices that might help you to convenience your team members to trust you with reviewing the code they wrote.

1. Understand what the code is doing.

The first and the most important thing you should do in a code review is to determine the purpose of the code. Knowing the objective of the code is critical since it will allow you to provide the perfect code review with helpful comments.

Usually, the author is explaining the code before the review occurs and guide the reviewer through the changes, important files and provide an in-depth context. In this process, look for key things such as code structure, logic, performance, design and styles, readability and maintainability, functionalities and test coverage.

You can automate particular tasks like reviewing structure and logic but there are some the factors like design and functionality which requires human intervention to evaluate. At the end of the walk-through process, you can ask the following questions yourself to ensure that you understand the code.

● Do you have all the necessary background information?

● Do you properly understand the requirements of the code?

● Do you understand how the code contributes to full fill those requirements?

By reviewing the code thoroughly, with these questions in mind, you can guarantee that you properly checked the right thing.

2. Set goals, use a checklist and measure the progress

Before implementing the process, the team should be clear on the goals of conducting a review and the expectations of reviewers. It is difficult to get the entire team engaged with the code review. It must be incorporated into the daily work schedule of the staff members.

It is extremely useful to have a checklist with the most significant things to check in the code. If the code is not so clean at the beginning, the best approach will be to use a short checklist. It will increase help the team to improve the quality of the code, quickly and easily. Here we have provided an example of a short checklist.

● Verify that the code you are reading is understandable.

● Verify that the variable names have been chosen correctly.

● Verify that the methods are properly named and parameterized.

● Verify that the code is adhering to general coding principles.

● Verify that the code fulfils the specified requirement.

Once the basic issues are solved, you can add more criteria to the checklist. Like,

● Verify that the code is compliant with the SOLID principles

● Verify that there are no unnecessary duplications in the code

● Verify that the code is properly handling exceptions.

● Verify that all the necessary comments are added to the source code.

These are just some common samples you can use while reviewing the code, but make sure to update your checklist systematically as the technologies are rapidly improving and our knowledge also develops so fast.

Code review checklists will provide team members with clear expectations and a consistent. So the programmers can review each other’s code with the same criteria in mind. By clearly communicating goals and expectations, the team can save time in the review process. To measure the progress, you can use the following indicators but they might differ from one code to another.

● The speed at which a review is completed

● The number of bugs discovered per hour

● The average number of defects discovered per line of code

There are automated tools that collect data automatically, which can ensure that information is correct and free of human errors.

3. Review a maximum of 400 lines at a time

Concentrating on fewer than 400 lines at a time improves the effectiveness of your code review. The brain’s ability to find defects will diminish if you try to evaluate too many lines at once. Hence, it is more convenient to analyze small code snippets and it results in a short code review and an improved code. In a code review, each session should be limited to 200 to 400 lines and it is known as Line of Code (LOC).

4. Don’t review for more than 60 minutes

So how much time you can practically assign to each review session? 60 minutes is a good number that you can simply focus on the task without getting tired. It is better to perform code reviews in short sessions. After an hour, have a stretch break, drink a cup of coffee and be relax. Then get back to work with a refreshed mind. Taking small breaks will allow your brain to recharge and as a result, you can go through everything again with new eyes. You’ll be more efficient in finding errors and defects. This will eventually increase the quality of the codebase.

5. Cultivate a positive culture

Try to spread a positive vibe around reviews. This is very important because peer review has the tendency to affect interpersonal connections between team members as there is a collection of people with different behaviours and some might take critique personally. So it is the manager’s responsibility to create a culture of collaboration and learning with a positive attitude. What really matters is the bug that was found before releasing the product. Not the author who introduced the error. It is something to be thankful for and celebrate. Fostering a positive culture will help the team to appreciate their colleagues, break bad habits and expand their capabilities.

6. Let everyone involve in the code review process

Everyone in the team, from junior to senior, must review others’ code and be reviewed by teammates. People tend to work better when they are aware that someone is going to evaluate their work. That is human nature but it improves the quality of the work. When conducting reviews include a software engineer, software architect, a UI/UX engineer, even junior developers and interns. Basically, it should be a combination of different job roles. So they can identify different issues not only in the codebase but also in the overall design of the software.

Another special benefit is that junior developers can learn from senior developers during the review process. Especially when it comes to new joiners, they might find it a little difficult and overwhelming to begin a new job at a new firm. Their style of programming can be different and the codebase is also new for them. So it is good if the team can pay additional attention to make the first few reviews more gentle for new joiners to get used to the new environment. The reviewers should understand that the newcomer may not be familiar with all the coding guidelines. They must be kind enough to explain alternative approaches to newbies in a quite encouraging tone.

7. Create a process to fix defects

There should be a suitable procedure to fix defects and confirm they are fixed. The most ideal approach is using a collaborative platform like GitHub. It allows your team to create review processes that improve the quality of code and fit nicely into your workflow. In GitHub, you can simply leave detailed comments on code syntax, ask questions or provide feedback alongside your code. There are a few facts you should keep in mind when adding comments and approving changes.

One is nitpicking. This refers to unimportant comments like,

● Variable declaration is not in alphabetical order,

● Unit tests don’t follow a specific format,

● Brackets are not in the same line.

These simple mistakes can be fixed through automated linting. Too many nitpicks can be frustrating and grab the attention away from the critical parts of code review.

The second tip is, be respectful. Always try to avoid negative behaviours and absolute judgements. Don’t be offended by the suggestions that your reviewer is giving you even when you don’t agree with them. Respond to each and every comment and maintain good work ethics. When making your comments, try to be specific and descriptive. Sometimes it is appropriate to provide a little more explanation as it will be helpful for the developer to understand your intentions.

Usually, the changes should be pushed to the same branch but in a different commit. Different companies use various policies when it comes to merging. Some teams allow the project managers to merge while others allow contributors to merge after the approval of the change.

Conclusion

On a final thought, code review can be an essential part of your organization’s culture. The discussions and the knowledge sharing that happens in a peer review will make you a better programmer, no matter it’s your code or someone else’s code being reviewed. Make code review an opportunity for newcomers to improve and adapt to the new system. Code review is an excellent method to enhance your abilities, mentor your juniors and develop effective communication skills. Finally, most contemporary organizations support investing in lightweight, automated tools to optimize time and measure results effectively. Do some more research on code review and the newest tool-assisted processes in the industry, communicate with your team members and identify the perfect match for your company.

--

--