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: 103376,f66d11aeda114c52 X-Google-Attributes: gid103376,public From: bobduff@world.std.com (Robert A Duff) Subject: Re: Building blocks (Was: Design By Contract) Date: 1997/10/02 Message-ID: #1/1 X-Deja-AN: 277371231 References: <34316EC3.5B62@dynamite.com.au> <199710011402.QAA02444@basement.replay.com> Organization: The World Public Access UNIX, Brookline, MA Newsgroups: comp.lang.ada Date: 1997-10-02T00:00:00+00:00 List-Id: In article <199710011402.QAA02444@basement.replay.com>, Anonymous wrote: >..."While" was said to be undesireable because it tends >to require the use of negative logic, which is less readable than >positive logic: It requires negative logic in *this* case, because there happens to be an End_Of_File function, rather than (say) a Within_File function. Sure, *some* while loops require negative logic, but some don't: "while X in Some_Subtype loop" vs. "exit when X not in Some_Subtype" "while X <= Max loop" vs "exit when X > Max" "while Is_Good(X) loop" vs "exit when Is_Evil(X)" I always prefer a while loop if there is exactly one exit condition at the start. I think this is usually the case -- I would guess I've written perhaps 10 times as many while loops as loops-with-exit in my life (in Ada, I mean -- other languages sometimes have different features). >Read : while not End_Of_File (Fred) loop > >Read : loop > exit Read when End_Of_File (Fred); > >It was included in the language for the same reason as "goto": to >facilitate automated translation from languages that include the >feature. I find this statement rather dubious. For one thing, I can't believe that Ichbiah et al were that down on while loops. (I can believe they advocated avoiding "not"s, but not all while loops need nots.) For another thing, there's a trivial transformation from "while" to loop-with-exit-at-the-start, so how is this necessary for automated translation? In the goto case, the transformation is not so trivial. >Certainly "while" is preferred by those doing program correctness >proofs; all the techniques for this that I have seen have been for >"while" loops. Avoiding "while" does usually make for more readable >code. In this specific example, "while" requires a flag variable, which >is less readable than using "exit". I certainly agree that using an "exit" is better than having extra flags and whatnot. But I think this is the less common case -- in the most common case, "while" does just fine. I certainly don't believe that "while considered harmful"! I'm also a bit suspicious of "program correctness proof" arguments, if the argument pushes toward writing less readable code (or lots more code). In such cases, it seems like the proof techniques are lacking, not the code one is trying to prove something about. (E.g. if somebody says (and they have), don't use generics, because they're hard to prove correct, and that means I have to write 17 different Stack packages, instead of one generic one, I blame the proof techniques, not the generic.) >...For this reason, "exit" and the possiblity of multiple >exits were included in Ada, and are considered acceptable by all >competent software engineers. OK, then I'll claim that "while" is considered acceptable by all competent software engineers. ;-) Is this a statement about what the authorities say, or is it a definition of who's competent and who isn't? ;-) By the way, speaking of negative logic, what do people think about negative logic in "if" statements? I tend to try to reduce the number of "not"s in the code. But other people tend to use some other heuristic, such as "do the normal case first" or "do unusual case first". For example: if Is_Evil(X) then print error message; else ... -- 37 lines of code doing the normal thing end if; vs if not Is_Evil(X) then ... -- 37 lines of code doing the normal thing else print error message; end if; vs if Is_Good(X) then ... -- 37 lines of code doing the normal thing else print error message; end if; vs if not Is_Good(X) then print error message; else ... -- 37 lines of code doing the normal thing end if; Or perhaps even: if Is_Evil(X) then print error message; return; end if; ... -- 37 lines of code doing the normal thing - Bob