👌Best Practices of Code Review

👌Best Practices of Code Review

Oct 9, 2023
Chia sẻ
Sưu tầm
Code review is a critical process in software development that contributes to producing high-quality, maintainable code. It’s a collaborative effort where developers evaluate each other’s code to identify bugs, suggest improvements, and ensure adherence to coding standards
Code review is a critical process in software development that contributes to producing high-quality, maintainable code. It’s a collaborative effort where developers evaluate each other’s code to identify bugs, suggest improvements, and ensure adherence to coding standards. By following best practices in code review, teams can catch errors early, enhance code quality, and foster knowledge sharing. In this article, we will delve into the best practices of code review, supported by real-world examples in the C# programming language.
notion imagenotion image

1. Start with Clear Objectives

Before diving into a code review, it’s essential to establish clear objectives. Are you looking for bugs, ensuring compliance with coding standards, or evaluating performance? Defining these goals helps reviewers focus their attention and ensures that the code review process is efficient and effective.
Suppose you’re reviewing a C# function that calculates Fibonacci numbers. Your objective might be to ensure that the function is optimized for performance while maintaining readability.

2. Keep Reviews Small and Focused

Breaking down the review process into smaller, focused chunks makes it easier to manage and provides more accurate feedback. Avoid reviewing large chunks of code all at once, as this can lead to oversight and fatigue.
Instead of reviewing an entire module, focus on individual methods or classes. For instance, concentrate on the error-handling logic within a specific C# class rather than attempting to review the entire project.

3. Be Respectful and Constructive

Code review is a collaborative endeavor, not a platform for criticism. Maintain a respectful tone and provide constructive feedback. Frame your comments in a way that encourages the developer to learn and improve, rather than demotivating them.
Instead of saying, “This code is terrible and inefficient,” try saying, “I noticed an opportunity for optimization in this section. How about we consider using a different approach to improve performance?”

4. Understand the Context

Before reviewing the code, take the time to understand the context of the changes. Know why the code was written, what problem it aims to solve, and how it fits into the larger project. Contextual understanding helps reviewers provide relevant and valuable feedback.
Imagine you’re reviewing changes to a C# API endpoint that handles user authentication. Understanding the security implications and the specific user flows will help you provide more accurate security-related feedback.

5. Follow Coding Standards

Consistency in coding style enhances readability and maintainability. Ensure that the code adheres to the team’s coding standards, including naming conventions, indentation, and documentation.
If the team follows the C# naming convention of using PascalCase for method names, provide feedback if you come across a method named in camelCase during your review.

6. Identify Bugs and Vulnerabilities

Code review is an excellent opportunity to catch bugs and vulnerabilities before they reach production. Look for logical errors, boundary cases, and potential security vulnerabilities.
While reviewing a C# web application, you notice that user inputs are directly concatenated into SQL queries, leaving the application susceptible to SQL injection attacks. You flag this issue and recommend using parameterized queries.

7. Consider Code Readability

Readable code is easier to maintain and understand. Look for long and convoluted blocks of code, excessive comments, or complex logic that could be refactored for improved readability.
During a C# code review, you encounter a method with deeply nested conditional statements. Suggest refactoring the logic into separate, well-named methods to enhance readability.

8. Encourage Modularity and Reusability

Promote modular and reusable code by identifying opportunities to extract common functionality into separate methods, classes, or libraries. This improves code maintainability and reduces redundancy.
While reviewing the C# code, you notice that multiple classes implement similar logging functionality. Recommend extracting the logging logic into a shared utility class to centralize this functionality.

9. Testability and Test Coverage

Evaluate the code’s testability and test coverage. Well-tested code is more reliable and easier to maintain. Ensure that the changes are covered by unit tests and that existing tests still pass.
When reviewing C# code modifications, verify that new methods or classes have associated unit tests and that any changes to existing code do not break existing test cases.

10. Leverage Automation Tools

Utilize automated code analysis tools to catch common issues such as code smells, style violations, and potential bugs. These tools can save time and provide consistent feedback.
Integrate a static code analysis tool like Roslyn into your C# code review process. It can automatically highlight issues related to coding standards, potential bugs, and more.

11. Provide Clear Documentation

Document your review findings and suggestions comprehensively. This documentation serves as a reference for the developer and helps in knowledge sharing across the team.
After reviewing the C# code, create a document that includes your feedback, explanations for suggested changes, and references to relevant coding standards. This document can serve as a learning resource for the developer.

12. Iterate and Learn

Code review is an iterative process. Encourage continuous improvement by learning from each review. Discuss feedback with your team and adapt your review practices based on the outcomes.
After the completion of a C# code review, hold a brief meeting to discuss the review process itself. Identify what went well and what could be improved for future reviews.

Sample C# Code Review Example

Let’s illustrate these best practices with a real-world C# code review scenario. Imagine you’re reviewing a class that calculates the area of different shapes. Here’s how you might apply the best practices:

Scenario: Area Calculation Class

public class AreaCalculator { public double CalculateRectangleArea(double width, double height) { if (width <= 0 || height <= 0) { throw new ArgumentException("Width and height must be positive."); } return width * height; } public double CalculateCircleArea(double radius) { if (radius <= 0) { throw new ArgumentException("Radius must be positive."); } return Math.PI * radius * radius; } }

Code Review Best Practice Application:

1. Clear Objectives: Ensure correctness and robustness of area calculation methods.
2. Small and Focused: Review each method separately — “CalculateRectangleArea” and “CalculateCircleArea”.
3. Respectful and Constructive: Provide feedback in a way that encourages learning. For instance, “Great job on the error handling! Let’s also consider adding some comments to explain the formulas.”
4. Understand the Context: Understand that this class is intended to calculate areas of different shapes efficiently.
5. Follow Coding Standards: Ensure that the naming conventions are consistent and adhere to the team’s guidelines.
6. Identify Bugs and Vulnerabilities: Validate that the error handling is appropriate and that inputs are properly checked for validity.
7. Code Readability: Suggest adding comments to explain the mathematical formulas and their significance.
8. Modularity and Reusability: Consider whether any common functionality could be extracted into shared methods, such as error checking.
9. Testability and Test Coverage: Recommend adding unit tests for these methods to cover various input cases.
10. Leverage Automation Tools: Utilize a code analysis tool like Roslyn to catch potential issues with coding standards and provide consistent feedback.
11. Provide Clear Documentation: Document your review findings in a clear and organized manner. Explain the purpose of each method, the error handling logic, and any suggestions for improvement.
12. Iterate and Learn: After completing the review, discuss the process with the developer. Address any questions they have and use the experience to refine your review approach for future cases.

Applying Best Practices to the Example:

1. Clear Objectives: The objective is to ensure that the area calculation methods are accurate, handle errors appropriately, and maintain readability.
2. Small and Focused: Start by reviewing the “CalculateRectangleArea” method. Ensure that the error handling is correct, and the formula is accurate. Then, move on to the “CalculateCircleArea” method.
3. Respectful and Constructive: Comment on the precise error handling, such as “Nice work on checking for negative inputs! Consider adding comments to clarify the purpose of the formulas.”
4. Understand the Context: Understand that the class provides area calculation methods, and that accuracy and efficiency are important.
5. Follow Coding Standards: Check if the naming conventions for methods and variables align with the team’s guidelines.
6. Identify Bugs and Vulnerabilities: Verify that the error handling prevents invalid inputs and that calculations are correct.
7. Code Readability: Suggest adding comments above each formula to explain the calculation’s mathematical basis.
8. Modularity and Reusability: Consider whether error handling logic can be extracted into a reusable method, enhancing modularity.
9. Testability and Test Coverage: Recommend writing unit tests for both methods, covering cases with positive and edge inputs.
10. Leverage Automation Tools: Use a code analysis tool to ensure adherence to coding standards and highlight any potential issues.
11. Provide Clear Documentation: Document the review findings, including explanations of suggested changes and the importance of proper error handling.
12. Iterate and Learn: After discussing the review with the developer, take note of any insights for improving the review process in the future.
By applying these best practices to the code review of the area calculation class, you can ensure that the code is not only correct but also maintainable, adheres to standards, and provides a valuable learning experience for the developer.


Effective code review is an indispensable part of the software development process. By following best practices, you can catch bugs, improve code quality, and foster a culture of collaboration and learning within your development team. The real-world C# example of reviewing an area calculation class demonstrates how these best practices can be applied to ensure the code’s correctness, readability, and adherence to coding standards. As you incorporate these practices into your code review process, you’ll contribute to the creation of robust, maintainable, and high-quality software.