home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13195 < prev    next >
Encoding:
Text File  |  1992-09-02  |  4.0 KB  |  108 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!wupost!gumby!destroyer!terminator!potts
  3. From: potts@itl.itd.umich.edu (Paul Potts)
  4. Subject: Re: GOTO, was: Tiny proposal for na
  5. Message-ID: <1992Sep2.131733.20676@terminator.cc.umich.edu>
  6. Sender: news@terminator.cc.umich.edu (Usenet Owner)
  7. Organization: Instructional Technology Laboratory, University of Michigan
  8. References: <2318@devnull.mpd.tandem.com> <rmartin.715001372@thor> <4192@papaya.bbn.com>
  9. Date: Wed, 2 Sep 1992 13:17:33 GMT
  10. Lines: 96
  11.  
  12. In article <4192@papaya.bbn.com> cbarber@bbn.com (Chris Barber) writes:
  13. >In article <rmartin.715001372@thor> 
  14. >
  15. >Well I must admit that it seems that most people posting on this topic
  16. >say that they do not use gotos, even those who disagree with you. So I
  17. >guess I will just have to come out of the closet and admit that I have
  18. >been using them for several years and with no regrets. I can't recall 
  19. >finding any bugs that were related to my use of gotos and I haven't heard
  20. >any complaints about the readability of my code. In fact, I resent your
  21. >implication that because I use gotos I am lazy and write unstructured
  22. >code. Although I use gotos frequently, I use them almost entirely for 
  23. >clean up code used in error handling. For example:
  24.  
  25. Good for you! I also resent this implication. I learned structured
  26. programming years ago but still occasionally find uses for goto.
  27.  
  28. I am pleased to see someone standing up for goto. Although in the case of
  29. what you are doing, it could probably be handled well using try/catch
  30. exceptions if they were widely available. I hope to use exception-handling
  31. for such things as soon as I can do it properly with Borland C++. However,
  32. goto is probably more clear in your case, and works now.
  33.  
  34. I posted this some time ago to comp.lang.c, but it seems applicable now:
  35.  
  36. -------
  37.  
  38. I use "goto" sparingly as well, and agree that it can be very useful in
  39. cases, and even make code *more* readable. BASICally <grin> I use it 
  40. only to exit from loops where adding additional conditions to the loop
  41. would make the code much more obscure and slow, or to skip down past a
  42. piece of code to the end of the loop under some circumstances. 
  43.  
  44. To make GOTO more readable, NEVER go backwards in your code, only
  45. forwards; use only one GOTO in a block, and comment explicitly. If GOTO
  46. is used only under certain conditions, the argument against writing
  47. spaghetti code really doesn't apply.
  48.  
  49. Here is an example of a mergesort with sentinels borrowed from Sedgewick's
  50. _Algorithms_ text. This is Pascal; for the Algorithms class I was taking at
  51. the time, I used C. My comments have been taken out - for anyone really
  52. interested in the details, I refer you to Sedgewick's Algorithms texts.
  53.  
  54. procedure do_no_sentinels (l, r: integer);
  55.     label    1;
  56.     var    i, j, k, m: integer;
  57. begin
  58.     if r - l > 0 then
  59.     begin
  60.         m := (r + l) div 2;
  61.         do_no_sentinels(l, m);
  62.         do_no_sentinels(m + 1, r);
  63.         for i := l to m do
  64.             b[i] := a[i];
  65.             b[m + 1] := sentinel;
  66.                 for j := m + 1 to r do
  67.                     b[j + 1] := a[j];
  68.             i := l;
  69.             k := l;
  70.             j := m + 2;
  71.             while (b[i] <> sentinel) do
  72.             begin
  73.                 if (j <= r + 1) then
  74.                 begin
  75.                     if (b[i] < b[j]) then                                begin
  76.                         a[k] := b[i];
  77.                         i := i + 1
  78.                     end
  79.                         else
  80.                     begin                                            a[k] := b[j];
  81.                         j := j + 1;
  82.                     end;                                            k := k + 1
  83.             end
  84.                 else
  85.                 begin
  86.                     repeat
  87.                         a[k] := b[i];
  88.                         i := i + 1;
  89.                         k := k + 1;
  90.                     until (b[i] = sentinel);
  91.                     goto 1
  92.                 end;
  93.         end;    {of j<= r test }
  94. 1:
  95.     end;    {Of test for empty sublist}
  96. end; {of do_no_sentinels procedure}
  97.  
  98. I wouldn't hazard attempting an accurate guess at just how much more
  99. efficient the use of goto makes this code in a typical case, but it
  100. will lop off the need for loops to continue and terminate. In this
  101. case the need could have been met using an exit(n), which I also support
  102. using, occasionally, when it is available and when it fits the need and
  103. can be used cleanly.
  104. -- 
  105. "It'sVerySad / toSeeTheAncientAndDistinguishedGameThatUsedToBe / aModelOf
  106. DecorumAndTranquilityBecomeLikeAnyOtherSportABattlegroundForRivalIdeologies
  107. toSlugItOutWithGlee." (from _Chess_). -potts@itl.itd.umich.edu-
  108.