Get your checklist and best practice guidance here
Code reviews are an essential part of the software development process and can significantly improve the quality and maintainability of code. They also provide a valuable opportunity for team members to learn from each other by sharing knowledge and experience.
In this article, we'll cover C#-specific issues, best practices that will help guide decision-making about code, and other things to consider at the design and architecture levels.
It's important to remember that few things in software development are black and white – there’s always some room for interpretation. While the following items should be considered when reviewing C# code, make sure to use your judgment and apply common sense when making decisions about code. You should also be considerate in the comments you make to get your perception across without sounding arrogant.
Let's get into the checklist, which you can use as a starting point for code reviews.
C# has had switch statements since version 1.0, but in version 8.0, a new feature called switch expressions was introduced. switch expressions are more concise and easier to read than if-else and switch statements, making them a good thing to look out for when reviewing code.
A switch expression is written like a switch statement, but with the keyword "when" instead of "case", and with an arrow (=>) instead of a colon. For example, the following is a switch statement:
switch (fruit)
{
case "apple":
color = "red";
break;
case "banana":
color = "yellow";
break;
case "orange":
color = "orange";
break;
default:
color = "unknown";
break;
}The same outcome can be achieved with a switch expression like this:
color = fruit switch
{
"apple" => "red",
"banana" => "yellow",
"orange" => "orange",
_ => "unknown"
};The switch expression provides a more elegant solution as the logic expands, making the code easier to both read and maintain.