comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@world.std.com (Robert A Duff)
Subject: Re: Building blocks (Was: Design By Contract)
Date: 1997/10/02
Date: 1997-10-02T00:00:00+00:00	[thread overview]
Message-ID: <EHFvt5.D1G@world.std.com> (raw)
In-Reply-To: 199710011402.QAA02444@basement.replay.com


In article <199710011402.QAA02444@basement.replay.com>,
Anonymous <nobody@REPLAY.COM> 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




  parent reply	other threads:[~1997-10-02  0:00 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-09-09  0:00 Building blocks (Was: Design By Contract) Marc Wachowitz
1997-09-15  0:00 ` Joachim Durchholz
1997-09-17  0:00 ` Paul Johnson
1997-09-18  0:00   ` Robert Dewar
1997-09-18  0:00   ` Stephen Leake
1997-09-18  0:00     ` Mark L. Fussell
1997-09-19  0:00       ` Jon S Anthony
1997-09-23  0:00         ` Mark L. Fussell
1997-09-19  0:00       ` Robert A Duff
1997-09-20  0:00         ` Joachim Durchholz
1997-09-22  0:00           ` Matthew Heaney
1997-09-23  0:00             ` Joachim Durchholz
1997-09-23  0:00             ` Veli-Pekka Nousiainen
1997-10-03  0:00               ` Robert I. Eachus
1997-10-04  0:00                 ` Paul Johnson
1997-10-14  0:00                   ` Robert I. Eachus
1997-09-23  0:00           ` Jon S Anthony
1997-09-24  0:00           ` Richard A. O'Keefe
1997-09-24  0:00           ` Alan E & Carmel J Brain
1997-09-25  0:00             ` Anonymous
1997-09-30  0:00               ` Alan E & Carmel J Brain
1997-09-30  0:00                 ` Matthew Heaney
1997-09-30  0:00                   ` Neil Wilson
1997-09-30  0:00                     ` Stephen Leake
1997-09-30  0:00                   ` W. Wesley Groleau x4923
1997-09-30  0:00                     ` Matthew Heaney
1997-10-01  0:00                     ` Alan E & Carmel J Brain
1997-10-01  0:00                 ` Anonymous
1997-10-01  0:00                   ` Paul M Gover
1997-10-04  0:00                     ` Paul Johnson
1997-10-04  0:00                       ` Matthew Heaney
1997-10-15  0:00                         ` Paul Johnson
1997-10-15  0:00                           ` Matthew Heaney
1997-10-16  0:00                             ` Joachim Durchholz
1997-10-17  0:00                               ` Robert I. Eachus
1997-10-16  0:00                           ` Joachim Durchholz
1997-10-22  0:00                           ` Reimer Behrends
1997-10-01  0:00                   ` Joachim Durchholz
1997-10-02  0:00                   ` Robert A Duff [this message]
1997-10-02  0:00                     ` Tucker Taft
1997-10-02  0:00                       ` Matthew Heaney
1997-10-03  0:00                     ` Stephen Leake
1997-10-04  0:00                     ` Matthew Heaney
1997-10-07  0:00                       ` Robert A Duff
     [not found]       ` <11861963wnr@eiffel.demon.co.uk>
1997-09-19  0:00         ` Mark L. Fussell
1997-09-18  0:00     ` W. Wesley Groleau x4923
1997-09-21  0:00       ` Matthew Heaney
1997-09-18  0:00   ` Jon S Anthony
  -- strict thread matches above, loose matches on Subject: below --
1997-09-11  0:00 Robert Dewar
1997-09-09  0:00 Marc Wachowitz
1997-09-02  0:00 Design By Contract Jon S Anthony
     [not found] ` <JSA.97Sep3201329@alexandria.organon.com>
1997-09-04  0:00   ` Paul Johnson
     [not found]     ` <5un58u$9ih$1@gonzo.sun3.iaf.nl>
1997-09-06  0:00       ` Building blocks (Was: Design By Contract) Joachim Durchholz
1997-09-08  0:00       ` Paul Johnson
1997-09-08  0:00         ` Brian Rogoff
1997-09-09  0:00           ` Veli-Pekka Nousiainen
1997-09-09  0:00           ` Veli-Pekka Nousiainen
1997-09-09  0:00             ` Jon S Anthony
1997-09-09  0:00           ` W. Wesley Groleau x4923
1997-09-09  0:00           ` Matthew Heaney
1997-09-09  0:00             ` Brian Rogoff
1997-09-09  0:00             ` W. Wesley Groleau x4923
1997-09-10  0:00               ` Robert A Duff
1997-09-12  0:00                 ` Jon S Anthony
1997-09-10  0:00             ` Robert Dewar
1997-09-12  0:00               ` Paul Johnson
1997-09-14  0:00                 ` Robert Dewar
1997-09-15  0:00                   ` John G. Volan
1997-09-14  0:00                 ` Robert Dewar
1997-09-14  0:00                 ` Robert Dewar
1997-09-12  0:00               ` Jon S Anthony
1997-09-12  0:00                 ` Robert Dewar
1997-09-16  0:00                   ` Brian Rogoff
1997-09-10  0:00             ` Paul Johnson
1997-09-10  0:00               ` Matthew Heaney
1997-09-10  0:00               ` Darren New
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox