Single ``return`` or multiple ``return`` I think is a potential code smell - based on design and code format agreements. I like the concept of having the guard statements and their ``return`` or ``throw`` exception small and tight at the beginning of a function/method. Putting the error handling into an ``else`` block can destroy the readability. We recently found a method in a work project where we were setting a ``results`` variable to ``true``, and then calling ``return false``. The last line of the method was to ``return results``. (Of course, no comments what team member that is no longer with the company was thinking.) This was in the logic and processing, not in guard statements. I think using negation logic can also be a code smell
void doSomething(var a)
{
if (a == null)
{
throw new Exception("a == null");
// or return with no meaningful "why"
}
else
{
//...
}
}
is more readable than
void doSomething(var a)
{
if (a != null)
{
//...
// Many lines so that the error handling is separated from the test
//...
}
else
{
throw new Exception("a == null");
// or return with no meaningful "why"
}
}