Urgh, that was a mistyping.. I meant foc-breaking statements, like goto.
Break statements are necessary for the use of switch. I have never used goto or continue in the 6 years I have been programming in C, and I have never used break for anything other than switch statements. I also do not simply flag the iteration terminator when I want to break out of a loop; if your code is well structured you simply will never have to do this.
But you're reading code complete, so you would know what I'm talking about.
Okay, I'll bite. I have been programming for 15 years now, with a substantial portion of that in C. I have worked on projects from a few 10s of thousands of lines of code up to a couple million. I have used goto... a lot. In nearly every case, it has been for error handling. So for the sake of argument, let's say you have the following code:
Code:
while(condition1) {
if (condition2) {
for ( i = 0; condition3; i++ ) {
val = networkOperation(&data[i]);
if ( val == -EHOSTUNREACH ) {
// catastrophic router failure outside our system!
retVal = -EHOSTUNREACH;
goto err_exit;
}
}
}
}
err_exit:
if ( resource1 ) free( resource1 );
if ( resource2 ) free( resource2 );
return retVal;
How do you efficiently (both in terms of maintenance and code execution speed) handle a deeply nested catastrophic error that can only be caught at runtime (in this case I'm supposing that the network went down in the middle of a series of transactions)?
If you're lucky, you're using a language with exceptions... but exceptions are goto's with a little more structure. In plain C, how do you handle that? I'm very curious because I'm overseeing about 100,000 lines of code right now, and hardware failures in the middle of your program are expected. I don't particularly like goto's, but they are the cleanest way to deal with code that looks a lot like what is above.