From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,9a0ff0bffdf63657 X-Google-Attributes: gidfac41,public X-Google-Thread: f43e6,9a0ff0bffdf63657 X-Google-Attributes: gidf43e6,public X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public X-Google-Thread: 1108a1,9a0ff0bffdf63657 X-Google-Attributes: gid1108a1,public From: dewar@merv.cs.nyu.edu (Robert Dewar) Subject: Re: Why C++ is successful Date: 1998/08/16 Message-ID: #1/1 X-Deja-AN: 381668185 References: <6qfhri$gs7$1@nnrp1.dejanews.com> <35cb8058.645630787@news.ne.mediaone.net> <902934874.2099.0.nnrp-10.c246a717@news.demon.co.uk> <6r1e1a$mj$1@platane.wanadoo.fr> <35D59E3F.22B93993@catalina-inter.net> X-Complaints-To: usenet@news.nyu.edu X-Trace: news.nyu.edu 903280737 21245 (None) 128.122.140.58 Organization: New York University Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-08-16T00:00:00+00:00 List-Id: Mr. Ada proposes for J in 1 .. N - 1 loop exit when (D (J) <= D (J+1); Swap (D (J), D (J+1)); end loop; Oh dear, this sure is simpler than what I wrote, but a very cursory look at it indicates that it must be linear, so it is unlikely to be able to sort anything and indeed it does not. Perhaps that is the remarkable thing about this example, although the flow of control of the original is clear, it is my experience that even very good programmers often mess up in trying to eliminate the pesky goto :-) The feeling of the goto here is something like what a musician means when they say "take it from the top". So you have to be aiming at something similar when you remove the goto. I usually find that the introduction of boolean flags obfuscates code, so if I was in a language like Bliss where I could not use a goto, my taste is for the solution that resets the loop index (similar to what Patrick suggested). Here it is in Ada: J := 1; while J < N loop if D (J) > D (J + 1) then Swap (D (J), D (J + 1)); J := 1; else J := J + 1; end if; end loop; There are two reasons why, for my taste, this is less clear than the goto. First: the critical "take it from the top" is buried inside the inner loop as the assignment J := 1, whereas the label on the loop in my original version shows right away that something strange is going on. Second: I prefer the for loop to the while loop to give the sense of running through the array in the inner loop. I really think of this algorithm as being two nested loops, and I like the code to show this structure. The above statement meshes the two loops together in what for me is a more confusing form than the original. Once again, if your taste is to prefer the J:=1 form because it is clearer, that's fine, but please don't think that you can measure clarity by counting gotos, and avoid the knee-jerk reaction that the form without a goto is necessarily clearer.