News for hackers. Stuff that matters.
Re:“No-else-after-return” considered harmful by cperciva on Sunday August 30, 2009 @10:24PM (#795280)
I always go a step further, and use at most one "return" in a function.

I opt for one return per exit type. For example:

* If I have a success/failure function, I will have "return (0);" and "return (1);" statements.

* If I have a pointer-or-fail function (e.g., an object constructor), I will have "return (r);" and "return (NULL);" statements.

* If I have a true/false/fail function (e.g., check if a file exists; return 1 if it exists, return 0 if it doesn't exist, and return -1 if something failed such that we couldn't figure out if the file exists), I will have "return (0);", "return (1);", and "return (-1);" statements.

In trivial functions, this tends to make my code somewhat more verbose than necessary; but I've found that sticking to this coding style dramatically reduces the odds of me making mistakes when I come back to code later and modify it, since different exit types usually require different cleanup code.

There are no comments on this comment yet.