Re: if clause
- From: Bill Medland <billmedland@xxxxxxx>
- Date: Mon, 30 Oct 2006 16:40:47 GMT
Michal Nazarewicz wrote:
0: if (out < 0)
1: return (fileError)
2:
3: if (permission < operator)
4: return (permissionError)
5:
6: if (data.len() <= 0)
7: return (dataError)
8:
9: write(out, data, data.len)
Makes me feel uncomfortable
"Ancient_Hacker" <grg2@xxxxxxxxxxx> writes:
(1) Some people think a function should have just ONE clear exit
point, at the bottom. I realize that can get a bit long-winded, but it
can make the code MUCH CLEARER,
I agree totally, especially when maintaining code that has grown to a
hundred lines per function.
Or much less clear when it comes into nesting ifs which, IMO, is less
readable then returning when error condition occurs.
which is why I prefer "exceptions first with else if" or goto error exit
much easier to set breakpoits, much
easier to add code you're SURE will get run every time. Multiple entry
points went out with FORTRAN II, why do we still have multiple exit
points in this 21sh century?
I believe, I've heard about some research showing that programmers
tend to make less errors when they are allowed to use 'break' - maybe
the same goes to multiple exit points.
Now to get just one exit point TAKES A LITTLE MORE WORK and a little
more nesting or use of "break".
Isn't "break" really cheating? Like in:
#v+
answer;
do {
some stuff;
if (error 1) {
answer = error1;
break;
}
some stuff();
if (error 2) {
answer = error2;
break;
}
some stuff;
if (error 1) {
answer = error2;
break;
}
some stuff;
answer = success;
} while (0);
return answer;
#v-
I would say yes, since it can easily be handled with a reasonable amount of
indenting (and if it can't then it's time to consider two smaller
functions)
It looks just like the same function with many exit points but
uglier.
Just showing my point of view though.
Personally I'm a nervous nelly and won't use "break" (see ATT $400
million looss due to the vagaries of "break"). So I end up with
code like the stuff below. Your opinion may vary. :
ErrorType Answer;
if( out >= 0 ) {
if( permission >= operator ) {
if( data.len > 0 ) {
Answer = write( ....) > 0;
} else Answer = BADLEN;
}
else Answer = BADPERMS;
} else Answer = BADOUT
return Answer;
For that depth that would satisfy me
Why not:
#v+
ErrorType Answer;
if (out<0) {
Answer = BADOUT
} else if (permission<operator) {
Answer = BADPERMS;
} else if (data.len<=0) {
Answer = BADLEN;
} else {
Answer = write(...) > 0;
}
return Answer;
#v-
although I prefer that
And then, of course, since I prefer data-oriented to process-oriented, there
is
Answer = out < 0 ? BADOUT :
permission < operator ? BADPERMS :
data.len() <= 0 ? BADLEN :
write(...) <= 0 ? BADWRITE :
SUCCESS;
--
Bill Medland
.
- References:
- if clause
- From: rogz
- Re: if clause
- From: Ancient_Hacker
- Re: if clause
- From: Michal Nazarewicz
- if clause
- Prev by Date: Re: C code to calculate gradient of a function
- Next by Date: Re: how to use bit operation to do \sqrt(n)
- Previous by thread: Re: if clause
- Next by thread: Re: if clause
- Index(es):