4 ms·
I do this too. :P Especially in a language like Go or C where you don't have exceptions, I think the "fast fail" at the start of the function is a reasonable
by jwalton 5y ago
I do this too. :P Especially in a language like Go or C where you don't have exceptions, I think the "fast fail" at the start of the function is a reasonable "exception to the rule". Otherwise you can quickly end up in a nested if statement hell.
But back in the days when I was doing embedded work, I frequently came across problems where someone allocated some resource at the start of a function, freed the resource at the end of the function, but then either failed to realize there was a return in the middle of the function, or someone else came along later and added a return to the middle of the function; either way we ended up with a resource leak. I saw this pattern over and over again. "Fast fail" is reasonable design pattern, so long as it's at the top of the function and you're careful not to allocate things before you check for preconditions, but functions of the form:
blah
blah
if
blah
blah
return x
else
blah
blah
return y
are (IMHO) an anti-pattern.
- Izkata 5y agoYeah, I'm pretty sure those leaks are the origin of it. At some point it became a "best practice" without understanding the reasons behind it, and people started applying it everywhere without any thought to "why".