Article View: comp.std.c
Article #3405Re: GOTO single exit point (was: Re: How to eliminate GOTO?)
From: Paul Long
Date: Wed, 06 Sep 1995 22:37
Date: Wed, 06 Sep 1995 22:37
57 lines
3667 bytes
3667 bytes
volpe@bart.crd.ge.com (Christopher R. Volpe) wrote: >In article <4258ak$cj7@news0.rain.rg.net>, Paul Long <plong@perf.com> writes: >>volpe@bart.crd.ge.com (Christopher R. Volpe) wrote: >>>In article <41t0sb$15e@news0.rain.rg.net>, Paul Long <plong@perf.com> writes: >>[snip] >> >>I mean the readability problem that Steve McConnell describes in _Code >>Complete_: "It's harder to understand a routine if, reading it at the bottom, >>you're unaware of the possibility that it returned somewhere above." > >With all due respect to Mr. McConnell, I don't read code from the bottom up. Neither do I, and apparently neither does Mr. McConnell. He says "at the bottom," not "from the bottom." Code structure should be random access. One shouldn't have to read in a particular direction. (Break is the only exception. I can't think of a better alternative to break.) >>Maybe you misunderstood me. I am _not_ proposing that gotos are a reasonable >>alternative to multiple return statements. Both are bad and avoidable. > >Ah... Ok. So you prefer the umpteen-levels-of-if-statement-nesting method? No, of course not. Give me an example, and I'll show you how I'd write it. If you're worried about nesting too deep, the function already has problems, such as being way too complex. Early returns (and gotos) are usually a way to cope with unnecessary complexity. [snip] >>Since I still have the book open, here's another quote from _Code >>Complete_: "Even if gotos aren't confusing when used carefully, once gotos are >>introduced, they spread through the code like termites through a rotting house. >>If any gotos are allowed, the bad creep in with the good, so it's better not to >>allow any of them." > >I agree somewhat with the spirit of that, but the conclusion is too absolute. >There are exceptions. I agree, there are exceptions, but they are _extremely_ rare. >>>Therefore, I use gotos only when the alternative is less readable. This is not >>>true for the general case of multiple return points. >> >>We disagree. > >On what? On the use of gotos only when the alternative is less readable? Or my >claim that multiple return statements are more readable than a bunch of >"goto exit_point;" statements? The last part. There is always a better alternative to using early returns. They are never warranted. Gotos are only warranted in (again) _extremely_ rare cases. One should use them infrequently enough to feel slightly guilty when the occasion arises. Genuflecting to Dijkstra might even be advisable. :-) -- Paul Long 45:29:14N 122:48:09W plong@perf.com http://www.teleport.com/~pciwww/ "Wagging head and tail, the red-tailed fish, unsupported, turns around."
Technical Details
Message-ID:
<42l7s7$2id@news0.rain.rg.net>Path:
arc1-ca.pugleaf.net!newsfeed.internetmci.com!psgrain!rainrgnews0!usenetReferences:
<3vn1ht$kj8@martha.utk.edu> <41cke4$gtd@sparcserver.lrz-muenchen.de> <41glno$3a0@caip.rutgers.edu> <41iebe$kec@news0.rain.rg.net> <41pm80$skp@inferno.mpx.com.au> <DE02nC.B58@eskimo.com> <41t0sb$15e@news0.rain.rg.net> <DE6G2z.JJD@crdnns.crd.ge.com> <4258ak$cj7@news0.rain.rg.net> <42hmj1$96o@rdsunx.crd.ge.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!newsfeed.internetmci.com!psgrain!rainrgnews0!usenet From: Paul Long <plong@perf.com> Newsgroups: comp.std.c,comp.lang.c Subject: Re: GOTO single exit point (was: Re: How to eliminate GOTO?) Date: 6 Sep 1995 22:37:59 GMT Organization: Performance Computing Incorporated Message-ID: <42l7s7$2id@news0.rain.rg.net> References: <3vn1ht$kj8@martha.utk.edu> <41cke4$gtd@sparcserver.lrz-muenchen.de> <41glno$3a0@caip.rutgers.edu> <41iebe$kec@news0.rain.rg.net> <41pm80$skp@inferno.mpx.com.au> <DE02nC.B58@eskimo.com> <41t0sb$15e@news0.rain.rg.net> <DE6G2z.JJD@crdnns.crd.ge.com> <4258ak$cj7@news0.rain.rg.net> <42hmj1$96o@rdsunx.crd.ge.com> NNTP-Posting-Host: yosemite.perf.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.1 sun4c) X-URL: news:42hmj1$96o@rdsunx.crd.ge.com