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.7 required=5.0 tests=BAYES_00,INVALID_MSGID, PDS_OTHER_BAD_TLD autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f43e6,9a0ff0bffdf63657 X-Google-Attributes: gidf43e6,public X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public X-Google-Thread: fac41,9a0ff0bffdf63657 X-Google-Attributes: gidfac41,public X-Google-Thread: 1108a1,9a0ff0bffdf63657 X-Google-Attributes: gid1108a1,public From: Matthew Heaney Subject: Re: Software landmines (loops) Date: 1998/08/30 Message-ID: #1/1 X-Deja-AN: 386118680 Sender: matt@mheaney.ni.net References: <6qfhri$gs7$1@nnrp1.dejanews.com> <35cb8058.645630787@news.ne.mediaone.net> <902934874.2099.0.nnrp-10.c246a717@news.demon.co.uk> <6r1glm$bvh$1@nnrp1.dejanews.com> <6r9f8h$jtm$1@nnrp1.dejanews.com> <6renh8$ga7$1@nnrp1.dejanews.com> <6rf59b$2ud$1@nnrp1.dejanews.com> <6rfra4$rul$1@nnrp1.dejanews.com> <35DBDD24.D003404D@calfp.co.uk> NNTP-Posting-Date: Sun, 30 Aug 1998 03:44:04 PDT Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-08-30T00:00:00+00:00 List-Id: Nick Leaton writes: > I have just done a quick check through some Eiffel code. > > Out of 633 loops, just 1 needed a flag for early termination of the > loop. My own experience is quite different from yours. Although I haven't counted, I use early loop termination ALL THE TIME. For example, I routinely implement an equality test (say, for a stack) as function "=" ... return Boolean is begin ... for Index in Positive range 1 .. L.Depth loop if L.Items (Index) /= R.Items (Index) then return False; end if; end loop; return True; end "="; If my loop termination depends on a value I just read, then exit from the middle is the simplest solution: loop Get (N); exit when N = 0; end loop; For example, this is a simplest way to tokenize a lexeme. You exit the loop in the middle, after determining that the character just consumed isn't part of the current token. Exiting is how you terminate passive iteration early: procedure For_Every_Item (Stack : in Stack_Type) is Done : Boolean := False; begin for Index in reverse Positive range 1 .. Stack.Top loop Process (Stack.Items (Index), Done); exit when Done; end loop; end For_Every_Item; Searching is also implemented using early loop termination: function Get_Position (C : Character; S : String) return Natural is begin for Index in S'Range loop if S (Index) = C then return Index; end if; end loop; return 0; end Get_Position; No auxilliary variables or flags are required. Below is a portion of my response to another thread on comp.lang.ada re the goto statement. Matt (start of quote) Any theory about using (or not using) a programming construct needs to be tested against observation, by performing an empirical study that measures the defect rates of programmers using the construct. If theory says use a construct, but observation reveals that programmers who use the construct produce more errors, then the theory needs to be thrown out. For example, there's a pernicious myth that exiting (or returning) from the middle of a loop is bad, and that the only proper way to write a loop is to state the termination condition explicitly, as a predicate appearing at the top of the loop. This theory was indeed put to the test, and guess what? Programmers using a test-and-exit from the middle of the loop produced fewer errors than those programmers who tried to put the test at the top of the loop. The researchers found that the exit-from-the-middle construct had a better "cognitive fit" than the other constructs. If you want to read the gory details, the article is Cognitive Strategies and Looping Constructs: An Empirical Study Soloway, Bonar, Ehrlich CACM, Nov 83, Vol 26, No 11, p 853-860 The goodness of a language construct should not be determined by doctrinaire computer scientists or mathematicians. The only thing that matters is whether working programmers think it's easier to understand, and whether by using the construct programmers inject fewer errors into the code. I think it was Stroustrup who said, "Programming is a human activity. Forget that, and all is lost." (end of quote)