Article View: comp.std.c
Article #3489Re: Multi-level break and elimination of GOTO
From: Erik Corry
Date: Mon, 13 Nov 1995 22:05
Date: Mon, 13 Nov 1995 22:05
34 lines
2156 bytes
2156 bytes
Richard Krehbiel (rich@kastle.com) wrote: : : Just last week I found a defective piece of code that avoided a goto : to leave a doubly-nested loop. It broke the outer loop by : synthesizing it's exit condition, but it did it wrong. I extracted : both loops into a separate static function, so that the inner loop : could break both loops with "return". I was fortunate that these : loops' use of local variables was limited enough to make the : extraction viable. I could have used "goto" but it's bad practice. The original programmer had probably had 'goto is bad' bashed into him with such force that he started doing really horrible hacks like synthsising exit conditions to avoid it. This is silly. Why did you make it a separate function? Why not just do a goto? Call the label something like 'outer_break:'. This would probably be clearer than calling a new function, and there would be less chance of accidentally introducing a bug. For example you might accidentally reference a global variable in the new function that was hidden by a local in the original function. (That's just an example - obviously you don't have nasties like global variables in your code :-) Why would goto have been bad practice in this case, yet with a little syntactic sugar you consider the exact same thing a useful language extension. One two-level break doesn't make spagetti code. -- It's not so much an afterlife, more a sort of apres-vie. --DNA -- Erik Corry ehcorry@inet.uni-c.dk
Technical Details
Message-ID:
<DI05Cx.HsE@kroete2.freinet.de>Path:
arc1-ca.pugleaf.net!news.uni-c.dk!kroete2!not-for-mailReferences:
<43pdut$hoh@galaxy.csc.calpoly.edu> <DF8FE3.97H@news.cern.ch> <rnbohuqj8p3.fsf@euax7i3c01.eua.ericsson.se> <DHpCK1.HyG@kroete2.freinet.de> <1995Nov13.124908.11176@friend.kastle.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.uni-c.dk!kroete2!not-for-mail From: erik@kroete2.freinet.de (Erik Corry) Newsgroups: comp.std.c,comp.lang.c Subject: Re: Multi-level break and elimination of GOTO Followup-To: comp.std.c,comp.lang.c Date: Mon, 13 Nov 1995 22:05:20 GMT Organization: Home Sender: news@kroete2.freinet.de (news) Message-ID: <DI05Cx.HsE@kroete2.freinet.de> References: <43pdut$hoh@galaxy.csc.calpoly.edu> <DF8FE3.97H@news.cern.ch> <rnbohuqj8p3.fsf@euax7i3c01.eua.ericsson.se> <DHpCK1.HyG@kroete2.freinet.de> <1995Nov13.124908.11176@friend.kastle.com> NNTP-Posting-Host: arhppp8.uni-c.dk X-Newsreader: TIN [UNIX 1.3 950515BETA PL0]