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: jbm@jbm.nada.kth.se (Jonas M�ls�) Subject: Re: Software landmines (loops) Date: 1998/09/09 Message-ID: <6t6l4n$rep@jbm.nada.kth.se>#1/1 X-Deja-AN: 389491801 Content-Transfer-Encoding: 8bit References: <6sbuod$fra$1@hirame.wwa.com> <35f51e53.48044143@ Organization: Royal Institute of Technology, Stockholm, Sweden Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-09-09T00:00:00+00:00 List-Id: Matthew Heaney writes: > > > >I can almost live that. The real problem, however, is that using an >extra flag to terminate VASTLY complicates the predicate. In fact, the >flag doubles the number of states I have to think about when mentally >evaluating the predicate! That's the real source of the loop >termination errors. > > >Using an exit from the middle avoids the headaches (literally) >engendered by using an extra flag in the predicate. When you want to >exit, you just say that you want to exit, directly. No mental >gymnastics are required in order to determine whether you'll "really" >exit, as would be the case using the flag approach. Using an exit from the top avoids the headaches (quite literally) of having to figure out the condition for completing another round in the loop, which is one of the main conditions to specify, in order to avoid loop termination errors. Also, I have never encountered a real situation where I have needed an extra flag in the predicate. One can actually do fine without them. As an example, I hereby present to you three variations of str_index. The first one strictly exits at the top. The other two in the middle. The first one is much easier to reason about, and to understan, IMHO. Anyway, introducing an extra flag in the predicate is very often a sign of "patching the loop". Done right, one does not need it. /** str_index * * str_index returns a pointer to the first occurrence of * the given character in the given string. * If the given character does not occur in the given * string, str_index returns NULL. * * If the given pointer is NULL, the result is undefined. * */ char * str_index ( char *s, /* string to search */ char c) /* character to search for */ { while (*s != '\0' && *s != c) ++s; return (*s != c) ? NULL : s; } char * str_index2 ( char *s, /* string to search */ char c) /* character to search for */ { while (*s != '\0') if (*s == c) return s; else ++s; return 0; } char * str_index3 ( char *s, /* string to search */ char c) /* character to search for */ { while (*s != '\0') { if (*s == c) return s; ++s; } return 0; }