Doron Holan [MSFT] wrote:
> it is all a matter of style. typically you see one of three options
> 1) alot of nested if()s which check for success
> 2) a flat strucure of if()s, on failure goto error;
> 3) a do { } while (FALSE) loop. on error, you see a break; this is the
> same as #2
Or, in C++ (hehe), using the "resource allocation is initialization"
(RAII) paradigm to clean up from error conditions (or any other exit
from a block) automatically in a destructor. Kind of a pain to write in
the general case, but it does make maintaining the code a lot less
hazardous for junior programmers.
Lots of nested if's tend to make my head hurt, especially when it's time
to maintain the code. So to me this is one of the rare circumstances
where goto's actually make a lot of sense.
I don't much like the do while false trick because it leaves you stuck
in lots of situations like this:
do {
for(...) {
success = DoSomething();
if (!success) {
?????????
}
}
} while(FALSE);
It's especially pernicious when this happens accidentally because
another programmer came along and added the for loop.
Yes, you can get around that by having a error holding stack variable
that is checked after any nested loops, but ugh.
--
Ray
|