Code review checklist for .NET teams
A practical .NET code review checklist covering design, readability, correctness, errors, security, performance, testing, operations, and constructive review comments and discussion.
Code review checklist for .NET teams
Code review is one of the cheapest ways to find the defects early and to share the knowledge within the team. But in many teams, the code review is becoming either a formality (“looks good to me”) or a long debate about the tabs and spaces. Both are not helping. A simple checklist which everybody agrees makes the review consistent, faster and focused on the things which really matter. In this article, we are going to see why we need a code review checklist and the checklist which we can use for .NET projects.
TL;DR Summary
- Code review finds defects early, when they are cheapest to fix.
- It spreads the knowledge, so no module depends on one person.
- Use a checklist to keep the review consistent and objective.
- Automate what tools can check (formatting, static analysis, build, tests), review what only humans can check.
- Keep the changes small, the review should take less than an hour.
- Review the code, not the person.
Challenges
Reviews without focus
Without agreed criteria, each reviewer looks for different things. One reviewer only checks the naming, another only checks the performance. Important problems like security issues or missing error handling are missed, because nobody was looking for them.
Big pull requests
When a change has 2000 lines in 40 files, the reviewer is skimming instead of reading. The defects are hiding in the volume, and the review is approved just to unblock the release.
Personal debates
When the rules are not written down, the review comments are becoming personal opinions. It creates friction in the team and the developers start to avoid the reviews.
What is Code Review?
Code review is a systematic examination of source code by one or more developers, other than the author, before it is merged into the main branch.
In simple terms, it is a second pair of eyes on your code. Think about a pilot and co-pilot going through the pre-flight checklist. The pilot is experienced, but still the checklist is followed every time, because missing one item can be very expensive.
Why Code Review is important?
In order to find the defects before they reach the customers, to keep the code base consistent and to spread the knowledge across the team, we should make the code review mandatory for every change going into the main branch. It is much cheaper to fix a bug during the review than after it is deployed in production.
Before the review
It is mandatory to do the following before raising a review request, otherwise the reviewer’s time is wasted on things a tool could check.
- Code compiles without warnings.
- All unit tests are passing locally and in the CI build.
- Code is formatted as per the team’s settings (Visual Studio formatting, StyleCop).
- Static code analysis (Visual Studio Code Analysis / FxCop rules) has no new violations.
- The change is small and focused on one feature or bug. If it is big, split it.
- The description explains what is changed and why, with link to the work item.
The Checklist
Find below the checklist, grouped by area. Not every item applies to every change, use your judgement.
Design
- Does the change follow the existing architecture and layering of the solution?
- Does each class have a single responsibility? Refer the SOLID Principles - Part 1 and Part 2 articles for detailed information.
- Are dependencies injected through constructor and not created with
newinside business logic? - Is there any duplicated code which can be reused?
- Is the solution simple enough? Is there any over-engineering for the future which may never come?
Readability
- Are the names of classes, methods and variables meaningful?
- Are the methods small and doing one thing?
- Are there magic numbers or hard-coded strings? Use constants, enums or configuration.
- Is there commented-out code? Delete it, source control remembers it.
- Do comments explain “why” and not “what”?
Correctness
- Does the code do what the requirement asks?
- Are edge cases handled, i.e. null, empty list, zero, negative values, max values?
- Are
IDisposableobjects (SqlConnection,Stream,HttpClientresponse) disposed withusing? - Are dates and times handled consistently (UTC vs local time, time zones)?
- Is decimal used for money and not double?
Error Handling
- Are exceptions caught only where we can handle them?
- Are there empty catch blocks swallowing exceptions?
- Is the exception re-thrown with
throw;and notthrow ex;, so the stack trace is preserved? - Are errors logged with enough context?
- Is the stack trace hidden from the API clients and end users?
We will discuss exception handling best practices in .NET in an upcoming article.
Security
- Is all user input validated?
- Are SQL queries parameterized (or using Entity Framework), no string concatenation with user input?
- Is the output encoded to avoid Cross-Site Scripting (XSS)? Is
Html.Rawused only with trusted content? - Are
[Authorize]attributes in place for the controllers and actions which need them? - Is
[ValidateAntiForgeryToken]used for the POST actions in MVC? - Are secrets (passwords, keys) kept out of the code and source control?
Performance
- Are there database calls inside loops (N+1 queries)? Use
Includeor a single query. - Is
ToList()called too early, loading the whole table into memory? - Are there blocking calls (
.Result,.Wait()) on async methods? We will discuss async and await in C# and its common mistakes in an upcoming article. - Is caching used where the data doesn’t change often?
- Are strings concatenated in big loops? Use
StringBuilder.
Testing
- Are there unit tests for the new or changed logic?
- Do the tests follow the AAA pattern and the upcoming FIRST properties?
- Do the tests cover the edge cases and error paths, not only the happy path?
- Are the test names describing the scenario and expected result?
Logging and Operations
- Are important business events and errors logged?
- Is sensitive data (passwords, personal data) kept out of logs?
- Are new configuration values added to all environments?
- Are database scripts / migrations included and reversible?
How to give review comments?
The checklist helps on “what” to review, but “how” we give the comments is also important. Here are some few guidelines,
- Review the code, not the person. Say “this method can throw a null reference when fund is null” instead of “you forgot null check again”.
- Explain why. “Use
throw;here, otherwise we lose the original stack trace” is more helpful than “wrong”. - Separate must fix from nice to have. Prefix optional comments with “Nit:” or “Suggestion:”.
- Appreciate the good things also. A short “nice refactoring” motivates the developer.
- If the discussion takes more than two rounds of comments, talk directly.
Note: Keep the review under 400 lines and under one hour where possible. After that, the attention drops and the reviewer misses the defects.
Wrap up
In this article, we have looked at why code review is important, what to do before the review, the checklist grouped by design, readability, correctness, error handling, security, performance, testing and operations, and how to give review comments. Thanks for reading this article. In the next article, I will walk you through exception handling best practices in .NET.
Related Posts
How to write unit tests with NUnit and Moq?
Unit testing with NUnit and Moq explained through FIRST properties, the AAA pattern, test doubles, test cases, exceptions, and verification of interactions.
What is a God object and how to break it?
God objects explained through their warning signs, causes, and a step-by-step refactoring approach using characterization tests, extraction, interfaces, and dependency injection.
Why we must not use magic numbers and hard-coding?
Magic numbers and hard-coded values explained with their maintenance and security risks, plus practical replacements using constants, enums, configuration, and read-only values.