-
Notifications
You must be signed in to change notification settings - Fork 1
Code style
Atulin edited this page Sep 10, 2021
·
1 revision
This page talks about preferred code style, reasons behind the particular choices, and sometimes a bit about the philosophy behind it all.
Equality operators can be overridden, operator is
cannot
Good code
if (user is null)
{
return NotFound();
}
if (user is { } u)
{
return Ok(u.Name);
}
Bad code
if (user == null)
{
return NotFound();
}
Fail Fast to reduce nesting, cognitive load, and sometimes even performance
Good code
if (thing is null) return NotFound();
thing.Name = newName;
await _context.SaveChangesAsync();
return Ok(thing);
Bad code
if (thing is not null)
{
thing.Name = newName;
await _context.SaveChangesAsync();
return Ok(thing);
}
else
{
return NotFound();
}