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: 1108a1,9a0ff0bffdf63657 X-Google-Attributes: gid1108a1,public X-Google-Thread: f43e6,9a0ff0bffdf63657 X-Google-Attributes: gidf43e6,public X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public From: Charles Hixson Subject: Re: Software landmines (loops) Date: 1998/08/30 Message-ID: <35E9C0CD.D225D1F8@earthlink.net>#1/1 X-Deja-AN: 386244616 Content-Transfer-Encoding: 7bit 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> <6sbuod$fra$1@hirame.wwa.com> X-Posted-Path-Was: not-for-mail Content-Type: text/plain; charset=us-ascii X-ELN-Date: Sun Aug 30 14:12:38 1998 Organization: Mandala Fluteworks Mime-Version: 1.0 Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-08-30T00:00:00+00:00 List-Id: In my experience it is USUALLY a bad idea to exit from the middle of a loop. It is USUALLY a bad idea not to place the loop continuation test at the start. But there are many exceptions. There are many loops that require either a very complex initial test, or multiple tests, e.g. (psuedo-code) if (str (0) != ';') { while (len (str) < 0) { select (str (0) ) { case 'A'..'Z': .... exit; case 'a'..'z': -- do something else -- chop off head of string case '0'..'8': -- do something else -- chop off head of string case '9': -- do something else -- set loop exit flag -- chop off head of string otherwise: -- reset loop exit flag } -- do some stuff when (exitFlag and lastChar != '9') exit; } } Now maybe this is what was meant by a "state-machine", as when this kind of construct comes up I do think of goto's, which WOULD make writing the code easier. I just don't think that they would make understaning it easier. On the other hand, the test that one would need to emplace to put all exit tests at the beginning of the loop is apalling, and I don't think that a calculating a compound boolean flag, together with the internal if tests to skip over code that isn't to be executed in this pass, improves things. Personally, I try to avoid this situation, but sometimes I just can't ("Parse this list of mailing labels that different people have created in different systems using different software. We can get it into csv form, and within each list the fields will usually be in the same order, and they won't interpolate new field very often. Oh, yes, sometimes people used their mailing list to attach notes to themselves about the company being mailed to. Could you save those separately.") When this comes up, the idea of NOT being able to exit a processing loop would be apalling. Sometimes I end up just throwing the entire label into comments (even after looking at it). Once in awhile I'll discard it (after looking at it). But I've GOT to be able to handle data with any type-compliant degree of variation. And sometimes that means exiting from deeply imbedded loops. Robert Martin wrote: > > Matthew Heaney wrote in message ... > > >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 ... > > Robert C. Martin | Design Consulting | Training courses offered: > Object Mentor | rmartin@oma.com | Object Oriented Design > 14619 N Somerset Cr | Tel: (800) 338-6716 | C++ > Green Oaks IL 60048 | Fax: (847) 918-1023 | http://www.oma.com > > "One of the great commandments of science is: > 'Mistrust arguments from authority.'" -- Carl Sagan