And now we have a 21-year-old academic paper, the author ruffled by critique of 
a previous published item on switching their course from using Pascal to using 
C and teaching an "intuitive" use of a "goto". Mmmmm... "teaching an intuitive" 
doesn't work, does it. "Letting the students do what they 'feel' is the right 
thing, rather than teaching anything". That's better. Let's apply that to 
mathematics, other sciences, (human) languages and even history and geography.

OK, what is it that is "intuitive"? 
Get-out-of-trouble-quickly-and-move-on-to-the-next-issue. Apply until 
end-of-program. Anoint program as "working".

What is not intuitive to the new CS attendee is "design" when it comes to 
programming. Design is not about language constructs. Implementation is about 
language constructs. You can provide an implementation of a program without 
design (do it intuitively, or by rote application of constructs). In those 
cases it will only be a "structured" program (assuming that means something 
beyond rote) by coincidence and only up to the point when the same (or a 
similarly tutored) programmer changes it.

A new term to me is "loop and a half". So I search-engined. Ironically I found 
this (ironic because it references Roberts, I'm not going to follow the link 
yet, but I wonder...):

" Loop and a Half

You are writing a Process All Items loop.

How do you structure the loop when the number of items in the collection isn't 
known in advance and the items are provided one-at-a-time?

For example, you might be reading values from the keyboard or from a file. In 
general, you are processing all items until a sentinel is reached. The sentinel 
may be end-of-file, a valid value for the type of data being read, or an 
exception.

The loop requires priming to read/get the first value, a loop guard to test for 
the sentinel, and another read/get inside the loop. This leads to duplicate 
code for the read/get and makes the loop body harder to understand since it 
turns a read-and-process loop into a process-and-read loop.

Therefore, use a while-true loop with a break in the loop body to avoid 
duplicate code and to match your intuition that the loop is a read-and-process 
loop.

For example, consider pseudocode for a sentinel loop:

    read value
    while (value != sentinel)
        process value
        read value

This can be rewritten as

    while (true)
        read value
        if (value == sentinel) break;
        process value

Although the break is essentially a goto and thus violates a canon of 
structured programming, it results in code that it easier to develop Roberts. 
Because there is no code duplication the code should be easier to maintain and 
easier to understand"

"because there is no code duplication" is the key, well...

    execute a-procedure-that-yields-the-value
    while (value != sentinel)
        process value
        execute a-procedure-that-yields-the-value

Isn't that at least part of what procedures are for? Even better:

    execute 
a-procedure-that-yields-the-value-and-ensures-things-are-also-OK-like-at-least-one-value-is-present
    while (value != sentinel)
        process value
        execute a-procedure-that-yields-the-value

    procedure: 
a-procedure-that-yields-the-value-and-ensures-things-are-also-OK-like-at-least-one-value-is-present
        execute a-procedure-that-yields-the-value
        hey-let's-do-some-checking-for-things-that-should-halt-the-processing

Where's the "duplicated code"? It's where it should be. Now why do you need the 
GO TO?

I think most languages have procedures in one way or another, don't they?

Now if you want that control with the "ephemeral", then you have to set a flag 
when the ephemeral occurs, and if that is not possible (like when a while of a 
read results in a boolean in a loop control), then, err.... by repeating the 
test, repeating the code, or by making things more complex.

 PROCESS-MY-FILE.
     PERFORM UNTIL EXIT
         READ MY-FILE 
              INTO MY-RECORD
         AT END
             IF RECORD-COUNT GREATER THAN ZERO
                 EXIT PERFORM
             ELSE 
                 tell the world we're in a mess
             END-IF
         NOT AT END
             PERFORM PROCESS-MY-RECORD
         END-READ
         ADD 1 TO RECORD-COUNT
     END-PERFORM.

And I like files with "headers". And if they have headers, I want to check that 
the first is a header, and there is only one header, and have a trailer, and 
count the records, and check that the file is in sequence (if it need be) 
either uniquely or not, and I don't want all that stuff to mess up the look of 
the "process a file loop". And I can do all that without a GO TO, and whether 
the GO TO is disguised or not.

And if that is the loop for "advanced" cases, why don't I use the same loop for 
all cases, so whether the file is complex or not, the loop "looks" the same, 
there is nothing new or different to "understand".

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN

Reply via email to