home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!wupost!gumby!destroyer!terminator!potts
- From: potts@itl.itd.umich.edu (Paul Potts)
- Subject: Re: GOTO, was: Tiny proposal for na
- Message-ID: <1992Sep2.131733.20676@terminator.cc.umich.edu>
- Sender: news@terminator.cc.umich.edu (Usenet Owner)
- Organization: Instructional Technology Laboratory, University of Michigan
- References: <2318@devnull.mpd.tandem.com> <rmartin.715001372@thor> <4192@papaya.bbn.com>
- Date: Wed, 2 Sep 1992 13:17:33 GMT
- Lines: 96
-
- In article <4192@papaya.bbn.com> cbarber@bbn.com (Chris Barber) writes:
- >In article <rmartin.715001372@thor>
- >
- >Well I must admit that it seems that most people posting on this topic
- >say that they do not use gotos, even those who disagree with you. So I
- >guess I will just have to come out of the closet and admit that I have
- >been using them for several years and with no regrets. I can't recall
- >finding any bugs that were related to my use of gotos and I haven't heard
- >any complaints about the readability of my code. In fact, I resent your
- >implication that because I use gotos I am lazy and write unstructured
- >code. Although I use gotos frequently, I use them almost entirely for
- >clean up code used in error handling. For example:
-
- Good for you! I also resent this implication. I learned structured
- programming years ago but still occasionally find uses for goto.
-
- I am pleased to see someone standing up for goto. Although in the case of
- what you are doing, it could probably be handled well using try/catch
- exceptions if they were widely available. I hope to use exception-handling
- for such things as soon as I can do it properly with Borland C++. However,
- goto is probably more clear in your case, and works now.
-
- I posted this some time ago to comp.lang.c, but it seems applicable now:
-
- -------
-
- I use "goto" sparingly as well, and agree that it can be very useful in
- cases, and even make code *more* readable. BASICally <grin> I use it
- only to exit from loops where adding additional conditions to the loop
- would make the code much more obscure and slow, or to skip down past a
- piece of code to the end of the loop under some circumstances.
-
- To make GOTO more readable, NEVER go backwards in your code, only
- forwards; use only one GOTO in a block, and comment explicitly. If GOTO
- is used only under certain conditions, the argument against writing
- spaghetti code really doesn't apply.
-
- Here is an example of a mergesort with sentinels borrowed from Sedgewick's
- _Algorithms_ text. This is Pascal; for the Algorithms class I was taking at
- the time, I used C. My comments have been taken out - for anyone really
- interested in the details, I refer you to Sedgewick's Algorithms texts.
-
- procedure do_no_sentinels (l, r: integer);
- label 1;
- var i, j, k, m: integer;
- begin
- if r - l > 0 then
- begin
- m := (r + l) div 2;
- do_no_sentinels(l, m);
- do_no_sentinels(m + 1, r);
- for i := l to m do
- b[i] := a[i];
- b[m + 1] := sentinel;
- for j := m + 1 to r do
- b[j + 1] := a[j];
- i := l;
- k := l;
- j := m + 2;
- while (b[i] <> sentinel) do
- begin
- if (j <= r + 1) then
- begin
- if (b[i] < b[j]) then begin
- a[k] := b[i];
- i := i + 1
- end
- else
- begin a[k] := b[j];
- j := j + 1;
- end; k := k + 1
- end
- else
- begin
- repeat
- a[k] := b[i];
- i := i + 1;
- k := k + 1;
- until (b[i] = sentinel);
- goto 1
- end;
- end; {of j<= r test }
- 1:
- end; {Of test for empty sublist}
- end; {of do_no_sentinels procedure}
-
- I wouldn't hazard attempting an accurate guess at just how much more
- efficient the use of goto makes this code in a typical case, but it
- will lop off the need for loops to continue and terminate. In this
- case the need could have been met using an exit(n), which I also support
- using, occasionally, when it is available and when it fits the need and
- can be used cleanly.
- --
- "It'sVerySad / toSeeTheAncientAndDistinguishedGameThatUsedToBe / aModelOf
- DecorumAndTranquilityBecomeLikeAnyOtherSportABattlegroundForRivalIdeologies
- toSlugItOutWithGlee." (from _Chess_). -potts@itl.itd.umich.edu-
-