Article View: comp.lang.misc
Article #4382Re: GOTO controversy
From: John Noland
Date: Wed, 06 Mar 1996 23:56
Date: Wed, 06 Mar 1996 23:56
73 lines
3132 bytes
3132 bytes
>Robert C Shields (rcshlds@mailserv.mta.ca) wrote: > I am trying to get a feel for how people feel about the GOTO statement... >: please post.. > >Somebody at school saw a goto in my code and freaked. Then again, this >person (who shall remain nameless) is used to machines with 100MB of >memory and dual SPARC CPU's. I on the other hand am not. >The code in question was a routine that allocated various resources. >Below is an example of such a thing (this time using OS/2 & C). I use Goto's >to clean up the mess in the event of an error. > >Example for a good use of goto: >--- ???? >------------------------------------------------------------------------------ > HEV hev1, hev2, hev3; /* Event semaphores */ > HMTX hmtx; /* Mutex semaphore */ > void *ptr; > if (!DosCreateEventSem(0, &hev1, 0, FALSE)) > goto hev1_failed; > > if (!DosCreateEventSem(0, &hev2, 0, FALSE)) > goto hev2_failed; > if (!DosCreateEventSem(0, &hev3, 0, FALSE)) > goto hev3_failed; > > if (!DosCreateMutexSem(0, &hmtx, 0, FALSE)) > goto hmtx_failed; > > if ((ptr = malloc(SOME_SIZE)) == NULL) > goto malloc_failed; > > /* Do some stuff here */ > return TRUE; /* We did okay */ > >malloc_failed: > DosCloseMutexSem(hmtx); >hmtx_failed: > DosCloseEventSem(hev3); >hev3_failed: > DosCloseEventSem(hev2); >hev2_failed: > DosCloseEventSem(hev1); >hev1_failed: > return FALSE; The above is NOT an example of a good use for the goto statement. A lot of programmers hate goto with fanatical fervor. I personally feel they can make for better code in certain situations. The first situation would be to eliminate multiple return statements. A goto branch to a SINGLE label is greatly preferable to multiple return statements with several exit points. A subroutine with a single exit point is a lot easier to maintain than one with several exit points. The second situation would be to break out of a nested loop. The alternative to a goto in this situation is to use of flag of some kind (while(more_data)), but this tends to make the code larger and harder to read. Some more guidelines to follow for goto's are: DON'T use a goto unless it's the only solution to the problem. You have many alternatives to a goto in your above code. A subroutine should have at most one label. All goto's should be above that label in the code. The label should be in the same code block or at a more outer nesting level than the goto itself. Your code could be easily reorganized to not use a goto. -John
Technical Details
Message-ID:
<4hl8mt$4po@newshost.cyberramp.net>Path:
arc1-ca.pugleaf.net!newshost.cyberramp.net!newsReferences:
<rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net> <4grt4e$8fg@goanna.cs.rmit.EDU.AU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newshost.cyberramp.net!news From: sinan@cyberramp.net (John Noland) Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1 Subject: Re: GOTO controversy Date: 6 Mar 1996 23:56:13 GMT Organization: Prose Software Message-ID: <4hl8mt$4po@newshost.cyberramp.net> References: <rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net> <4grt4e$8fg@goanna.cs.rmit.EDU.AU> NNTP-Posting-Host: ramp3-25.cyberramp.net X-Newsreader: WinVN 0.99.5