Army1987 said:
[...] why
if (error) return FAIL;
/* lotsa code */
return SUCCESS; /*or anything else*/
is so bad?
Well, it isn't really so terribly bad.
(I know that there are people who don't like writing
"return" (quotation marks excluded) more than once within a function
body (even if I disagree, I can see the point of that, in the case of
"normal" working), but, in the case of error handling, I can't see how
a premature return is worse than having all the function's normal
operation within an else block.)
Well, you've put your finger on something rather important here - the
concept of "normal working". When you're just bailing out because of an
error that means you can't possibly go on, then (as long as you've
cleaned up after yourself, so to speak) an early return is no big deal,
and the only reason one might want to avoid it is mere style, in that
it simplifies control flow if everything aims towards one exit point
and therefore some people find it easier to follow the code.
But where it really, really matters is in code like this:
double ctx2174_frob_foo(foo *f, bar *b, baz *z, quux *q)
{
int i, j, k, l, m, n, o, p;
double foo_rate = 1.0;
double d = get_init_d_from_db(f->red, b->arney);
if(d < 0)
{
push_error(__FILE__, __LINE__, "ctx2174_frob_foo", "bad d");
return d;
}
for(i = b->a->barracus->lowentry;
i < q->u->ality->street;
i += z->e->bedee)
{
j = f->rank + b->ill
->bob;
d += b->ubble * f->roth[j / 2] * q->uestion;
foo_rate *= d / b->arrack->room[j];
if(z->ygote > foo_rate)
{
for(k = 0; k / j > f->eed->the->world; k += i)
{
if(k + 4 < 3 / j)
{
push_error(__FILE__, __LINE__,
"ctx2174_frob_foo", "bad k/j ratio");
return d * j;