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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC 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: Martin@nezumi.demon.co.uk (Martin Tom Brown) Subject: Re: Why C++ is successful Date: 1998/08/18 Message-ID: <903436224snz@nezumi.demon.co.uk>#1/1 X-Deja-AN: 382337924 References: <902934874.2099.0.nnrp-10.c246a717@news.demon.co.uk> X-Complaints-To: abuse@demon.net X-Mail2News-Path: news.demon.net!nezumi.demon.co.uk X-Trace: mail2news.demon.co.uk 903458694 mail2news:12732 mail2news mail2news.demon.co.uk Organization: Nezumi Reply-To: Martin@nezumi.demon.co.uk Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-08-18T00:00:00+00:00 List-Id: On 16 Aug, in article dewar@merv.cs.nyu.edu "Robert Dewar" wrote: > Patrick said > > Well, the fact that you had to add that hint suggests something about > your code's clarity... :-) > > How about this: > > from > J := 1 > until > J >= N > loop > if D(J) > D(J+1) then > Swap (D(J), D(J+1)); > J := 1; > else > J := J + 1; > end > end > > First the hint is simply because, although the algorithmic flow of the > original is completely clear, it is my experience that when people > contort to remove the gotos, they often end up with a non-equivalent > algorithm. I would argue that the above is clearer and easier to understand in that it now becomes obvious that after every swap at J the stated algorithm will do J-1 redundant comparisons. (and the "refinement" to bubble sort becomes obvious) > That says nothing about the clarity of the original, merely > the difficulty of removing the goto. Using a goto to jump back to the start of a for loop is exceptionally ugly. Somehow it looks more like an entry for an obfuscated code contest. > The above code is correct, but for my taste not clearer. I don't like > having a loop which at the outside looks like a perfectly normal > J from 1 .. N - 1 loop, but is not. Tastes differ I guess. What is quite telling is that several attempts to rewrite your original (even with a hint) have been completely wrong. Some failing even to sort at all, others failing to terminate. > Here the unusual structure of this loop is buried in an inner assignment, > rather than being evident at the top level in my presentation with a goto. I see your point. But still prefer the internal assignment version. > But certainly people differ in tastes and perhaps some will prefer the > above, but please prefer it because it is clearer to you, NOT, as is the > case for so many programmers, simply because the goto is eliminated. Clarity is in the eye of the beholder. Circumstantial evidence here has been that the original version though correct was opaque to a fair proportion of readers. Imagine a large program in that style... Regards, -- Martin Brown __ CIS: 71651,470 Scientific Software Consultancy /^,,)__/