Article View: comp.lang.misc
Article #4386Re: GOTO controversy
From: David E. Wallace
Date: Thu, 07 Mar 1996 03:30
Date: Thu, 07 Mar 1996 03:30
42 lines
2959 bytes
2959 bytes
In article <4hl8mt$4po@newshost.cyberramp.net>, John Noland <sinan@cyberramp.net> wrote: >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. Ok, I'll bite: why is a subroutine with multiple gotos to a single exit point a "lot" easier to maintain than that same routine with multiple returns? It isn't any easier to understand: the meaning of an early return due to an error is well understood and controlled, just as the meaning of "break" or "continue" is well understood with respect to loop termination. In contrast, the meaning of "goto foobarexit" requires scanning ahead to the definition of foobarexit to determine exactly what happens next, even if you can guess from the label that this is some sort of exit processing. If you want to know if a given routine has multiple exit conditions, it's just as easy to scan backwards for "return" as it is to scan for "goto foobarexit." The only maintenance tasks that I can see that get any easier are inserting special-purpose cleanup code that must run before a given function returns (regardless of why it is returning), or changing the return type of a function to a type that existing return values won't get automatically coerced to. I can't see that either of these comes up often enough to warrant claiming great advantages for the "single goto" solution. In the case of the cleanup code, it seems more likely that the type of cleanup will depend on the reason for termination (as it did in the original example, where you thought the goto solution to multiple labels was inappropriate). The "single goto" solution isn't horrible; I just don't see the great advantages claimed for it. On balance, I prefer multiple returns when a simple return is appropriate, just because it is slightly more self-documenting. I have no problems with a goto when complicated cleanup must be done, but I find this case to be rare. So what maintenance issues come up often enough in your environment that you find the "single goto" solution far superior to multiple returns? -- Dave Wallace (wallace@netcom.com)
Technical Details
Message-ID:
<wallaceDnvoEo.7L7@netcom.com>Path:
arc1-ca.pugleaf.net!netcom.com!wallaceReferences:
<rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net> <4grt4e$8fg@goanna.cs.rmit.EDU.AU> <4hl8mt$4po@newshost.cyberramp.net>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1 Path: arc1-ca.pugleaf.net!netcom.com!wallace From: wallace@netcom.com (David E. Wallace) Subject: Re: GOTO controversy Message-ID: <wallaceDnvoEo.7L7@netcom.com> Organization: NETCOM On-line Communication Services (408 261-4700 guest) References: <rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net> <4grt4e$8fg@goanna.cs.rmit.EDU.AU> <4hl8mt$4po@newshost.cyberramp.net> Date: Thu, 7 Mar 1996 03:30:24 GMT Sender: wallace@netcom7.netcom.com