Five things I check in a .NET code review
A long review checklist is a checklist nobody uses. So mine is short. These five patterns cause a lot of trouble in .NET code, they’re easy to spot, and they’re easy to fix when you catch them early.
1. Blocking on async code
If I see .Result, .Wait() or .GetAwaiter().GetResult() on a task, I stop and read carefully.
// Works on your machine. Falls over under load.
var user = repository.GetUser(id).Result;
// Better
var user = await repository.GetUser(id, cancellationToken);Blocking a thread to wait for async work can deadlock in some app types, and in a web app it eats threads that were supposed to be serving other requests. It rarely fails in testing, which is exactly why it keeps reaching production. The same goes for async void. Outside of event handlers it should almost never exist, because nothing can await it and an exception inside it can take the process down.
2. Exceptions that go nowhere
try
{
await SendInvoice(order);
}
catch (Exception)
{
}That empty catch is a promise that this will fail someday and nobody will ever know why. Either handle the error (retry it, show a message, return a useful result), or log it with the details and let it continue, or don’t catch it at all. Catching a specific exception you can actually do something about is fine. Catching everything to keep the red text away isn’t.
3. new HttpClient() inside a method
Creating an HttpClient for every call can run you out of sockets, and it makes DNS changes hard to pick up. Inject IHttpClientFactory, or register a typed client, and let the framework manage the connections.
builder.Services.AddHttpClient<PaymentsClient>(client =>
{
client.BaseAddress = new Uri("https://api.example.com/");
});Small change, and a whole class of strange production errors never happens.
4. SQL built from strings
// Please no
command.CommandText = $"SELECT id, name FROM users WHERE name = '{name}'";
// Yes
command.CommandText = "SELECT id, name FROM users WHERE name = @name";
command.Parameters.AddWithValue("name", name);This is the oldest bug in the book and it still shows up. Parameters aren’t only about security. They also handle quotes and special characters in the data for you, so a customer called O’Brien doesn’t break your query. The same rule applies to anything you paste into an ILIKE search: escape the user’s input first.
5. Dates that quietly depend on the server’s clock
DateTime.Now gives you local time on whatever machine is running the code. Move the app to a server in another timezone and half your comparisons are now wrong, with no error to tell you.
// Depends on where the server lives
var expires = DateTime.Now.AddDays(7);
// Doesn't
var expires = DateTimeOffset.UtcNow.AddDays(7);I prefer DateTimeOffset for anything stored or compared, because the offset travels with the value. Convert to local time only at the very edge, when you show it to a person.
The sixth one, which isn’t about code
I also read the pull request title and ask whether I can tell what the change does without opening a single file. If I can’t, the code is rarely the real problem. Usually the change is doing two things at once and should have been two pull requests.
That’s the whole list. If a review catches these five, it has already done most of its job. Everything else is taste, and taste can wait for the next review.