NH> How many is too many? Should the switch statement be treated the sa
NH> way goto is: only under duress? (and then, don't let anybody know yo
NH> did it)?
CD>
TM> No. There are some cases where switch
TM> statements make sense. There
TM> aren't any cases where gotos make sense.
DM> But... but... there ARE cases where gotos make sense. Avoiding gotos
DM> is good. Avoiding gotos at all costs is not. Sometimes, they just
DM> make sense.
CD>
CD> Being from Missouri, I've got to see it to believe it. Even the
CD> various published pundits who say essentially the same as you never
CD> show an example of "the judicious goto".
You want something from actual real-life code? Sorry - that's copyright
material. The basic gist:
unsigned long foo( /* parameters, in/out */ )
{
/* define a bunch of locals, including lots of pointers to
whatever - memory, FILEs, window handles, other API handles */
unsigned long ulRC = 0;
/* allocate the pointers. On failure, set ulRC and goto exit */
/* work with the pointers. On failure, set ulRC and goto exit */
ulRC = GOOD_RETURN_FROM_FOO;
exit:
/* clean up pointers in reverse order of allocation, only if they
* point to valid memory - don't fclose NULL FILE*'s, don't
* close DosFindFile's that were never initiated or were finished
* properly, etc. */
return ulRC;
}
Single entry. Single exit. Flow always goes down, except in properly
defined structural loops (while, do while, for). Clean-up code will always
get executed. Adding extra clean-up code will not break anything - it will
also get executed every time because it is part of the single exit.
In C++, however, I can avoid most of this, if not all of it. I create
classes that handle everything for me. No matter where I have to exit from,
the class will clean up whatever it points to in its destructor. Buffers are
now cchars. Arrays of characters (nul-terminated) are now strings. FILE*'s
are now streams. Mind you, I then throw away the single-exit practice...
DM> Reducing three levels of indentation via a single goto
DM> will actually increase readability... sometimes.
CD> I suppose. But I've found a fascinating correlation. There are some
CD> folks who find goto quite useful to exit from deep inside nested
CD> loops. Then there are those who don't get stuck inside of deeply
CD> nested loops in the first place.
I wish all my programs were so simple and never needed modification like
that. Unfortunately, the rest of us live in the real world where
requirements change faster than we can code them in. (We need better
managers, yes. However, that's another real-world factor we have to live
with. I can't afford to change jobs within 4 months of starting my last one
- it just looks bad.)
[Sorry, Mr. Moderator, but he's been using that "real-world" line so long
already, and I was just waiting to see it used against him... Now that I've
been sated, it won't happen again... I'll probably be twitted, too ]
--- Maximus/2 3.01
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)
|