comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Building blocks (Was: Design By Contract)
Date: 1997/10/04
Date: 1997-10-04T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023680000410970119430001@news.ni.net> (raw)
In-Reply-To: PdRYMWAQ1cN0Iw4X@treetop.demon.co.uk


In article <PdRYMWAQ1cN0Iw4X@treetop.demon.co.uk>, Paul Johnson
<Paul@treetop.demon.co.uk> wrote:

>>(One of the while-loop problem is that for a tidy
>>record processing program you have to write
>>       record = file.read()
>>       Do while(~file.eof())
>>               thing.accept(record)
>>               record = file.read()
>>       End while
>>)
>
>I know that this was an aside, but it still puzzles me.  Surely it will
>be wrong if the file is empty: there will be no record to read.  The
>correct version (in Eiffel) would be
>
>   from file.open("foo") until file.eof loop
>      thing.accept (file.record)
>   end -- loop
>
>If you have to read a dummy record before file.eof becomes valid then
>you have a badly designed file object.

That's the idiom in Ada too:

while not End_Of_File (F) loop
   Read (F, X);
   <do something with X>;
end loop;

I think the original example, though, was this: read some numbers from the
user interactively, process each one at a time, and stop processing when
the user enters a zero.  In Ada, it would be 

loop
   Read (N);
   exit when N = 0;
   <do something with N>;
end loop;

This is a very clean solution, and in the Soloway paper, they showed that
there were fewer errors using this loop structure compared to a loop which
disallows an exit from the middle.  Something like:

Read (N);
while N /= 0 loop
   <do something with N>
   Read (N);
end loop;

The issue here is that the read statement must be written twice.  Exiting
from the middle is a more natural idiom for certain problems. 

So don't assume that there's a "badly designed" file, because we might not
be literally talking about a file.  That the loop termination depends on
what you've read (the "dummy" value) is a common occurrence, and the
simplest loop idiom in this case is:

loop
   <get value>
   exit when <value is special>
   <process value>
end loop;

A while loop is more complex and error prone solution for this type of
problem.  Soloway showed that there was a statistically significant
difference between two populations, one using a test-at-the-top, and the
other using a test-in-the-middle, with the latter group having fewer
errors.  The reference is:

Coginitive Strategies and Looping Constructs: An Empirical Study
Elliot Soloway et al
CACM, Vol. 26, No. 11, Nov 83, p. 853 - 860


A typical example of termination depending on input is a scanner.  If
you're scanning an identifier (say), then you exit when you've read some
whitespace:

Scan_Identifier:
loop
   <get character>
   exit Scan_Identifier when <char is whitespace>
   <buffer char>
end loop Scan_Identifier;

<process Identifier token>

Another common idiom is a linear search.  When you find the item you're
looking for, bail out:

Find_Item:
declare
   Found : Boolean := False;
   Position : Positive range Items'Range;
begin
   for Index in Items'Range loop
      if Items (Index) = Item then
         Position := Index;
         Found := True;

         exit;
      end if;
   end loop;

   if Found then
      <do something>
   else
      <do something else>
   end if;
end Find_Item;

Exiting from the middle of a loop also shows up in functions as an early return:

function Position (Items : Item_Array; Item : T) return Natural is
begin
   for Index in Items'Range loop
      if Items (Index) = Item then
         return Index;
      end if;
   end loop;

   return 0;  -- means Item not found
end;

In none of these cases is the "file" badly designed.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




  reply	other threads:[~1997-10-04  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   ` Jon S Anthony
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                   ` W. Wesley Groleau x4923
1997-09-30  0:00                     ` Matthew Heaney
1997-10-01  0:00                     ` Alan E & Carmel J Brain
1997-09-30  0:00                   ` Neil Wilson
1997-09-30  0:00                     ` Stephen Leake
1997-10-01  0:00                 ` Anonymous
1997-10-01  0:00                   ` Joachim Durchholz
1997-10-01  0:00                   ` Paul M Gover
1997-10-04  0:00                     ` Paul Johnson
1997-10-04  0:00                       ` Matthew Heaney [this message]
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-02  0:00                   ` Robert A Duff
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
  -- 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             ` Jon S Anthony
1997-09-09  0:00           ` Veli-Pekka Nousiainen
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               ` Jon S Anthony
1997-09-12  0:00                 ` Robert Dewar
1997-09-16  0:00                   ` Brian Rogoff
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-10  0:00             ` Paul Johnson
1997-09-10  0:00               ` Darren New
1997-09-10  0:00               ` Matthew Heaney
1997-09-09  0:00           ` W. Wesley Groleau x4923
replies disabled

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