comp.lang.ada
 help / color / mirror / Atom feed
From: "Charles Lindsey" <chl@clerew.man.ac.uk>
Subject: Re: "continue/next" for "loop"
Date: Wed, 5 Nov 2003 10:36:11 GMT
Date: 2003-11-05T10:36:11+00:00	[thread overview]
Message-ID: <HnvK4B.6Mu@clerew.man.ac.uk> (raw)
In-Reply-To: slrnbqckoa.m1.lutz@taranis.iks-jena.de

In <slrnbqckoa.m1.lutz@taranis.iks-jena.de> Lutz Donnerhacke <lutz@iks-jena.de> writes:

>But both are the result of an oversimplification!

>outer: loop
>  inner: loop
>    Complex_Actions_1;
>    if First_Condition then
>      Some_Justify_Actions;
>      goto next_outer;
>    end if;
>    Complex_Actions_2;
>    if Second_Condition then
>      Some_Other_Justify_Actions;
>      goto next_outer;
>    end if;
>    Complex_Actions_3;
>  end loop inner;
>  Complex_End_Statments;
>  exit outer when Other_Condition;
>  More_Statments;

Yes, that is fine, and it is well-structured. But it is still not the most
general case.

Here is what I used to teach. The original idea came from Eberhard Wegner,
and if you recognize who he was (or who I am), you will realize that this
argument has been going on for a very long time (see my paper in SIGPLAN
Notices for November 1977). There have been no new arguments added since
then that have not been repeated ad nauseam ever since (least of all in
this latest thread).

1. You can regard a well-structured program as a set of nested boxes. Or of
nested levels of indentation as in Lutz's example above.

2. Those boxes are not just the bodies of loops. They include the THEN and
ELSE part of conditionals, the individual alternatives of CASEs, and even
BEGIN...END blocks introduced just for the hell of it.

3. There are just TWO acceptable forms of GOTO:

   3a. The REPEATER

   This is a jump from the inside of one box to the START of some
   enclosing box (cutting through as many intermediate boxes as you wish).

   3b. The COMPLETER

   This is a jump from the inside of one box to the END of some
   enclosing box (cutting through as many intermediate boxes as you wish).

NOTE 1: The words "START" (for the completer) and "END" (for the repeater).
Another way of saying the same thing is that the only acceptable places
for a LABEL are at the START of a new level of indentation, or ar the END
of an old level of indentation. 

NOTE 2: It doesn't just apply to loops.

NOTE 3: You may be able to restructure the program (add BEGIN...END
blocks, move stuff outside of blocks, etc) so as to achieve the effect,
but there exist some programs which cannot be restrutured that way, and
which will force you to jump INTO a box from outside. Those are the
unstructured "spaghetti" programs, which should indeed be "considered
harmful".

NOTE 4: Of course, this shows that GOTOs and LABELS were the wrong
language tools to have invented in the first place. Various languages seek
to remedy this by inventing constructs such as EXIT, or CONTINUE, or
EXCEPTIONS; but the mistake most of them make is to restrict these
constructs only to LOOPS, or only to exit through one level of nesting.


Essentially, the Repeater means "I have got myself into this deeply nested
mess where I cannot proceed further; I just want to START OVER".

The Completer means "I have got myself into this deeply nested
mess where I cannot proceed further; I just want to BAIL OUT".

It turns out, in practice, that the Completer is the most useful tool. The
effect of the Repeater can as easily be obtained in most cases by a
conventional WHILE loop.

And yes, even Spaghetti programs can be made to look structured by
inventing a sufficiently large number of boolean variables. That too has
been known for the last 25 years, so do not bother repeating it. It DOES
NOT make a program any clearer, as opposed to well-structured jumps as
above. It just assuages the consciences of those who believe the myth that
ALL GOTOs are harmful, which nobody (no, not even the late lamented
Edsger) ever said they were.

-- 
Charles H. Lindsey ---------At Home, doing my own thing------------------------
Tel: +44 161 436 6131 Fax: +44 161 436 6133   Web: http://www.cs.man.ac.uk/~chl
Email: chl@clerew.man.ac.uk      Snail: 5 Clerewood Ave, CHEADLE, SK8 3JU, U.K.
PGP: 2C15F1A9      Fingerprint: 73 6D C2 51 93 A0 01 E7 65 E8 64 7E 14 A4 AB A5



  parent reply	other threads:[~2003-11-05 10:36 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-03 12:55 "continue/next" for "loop" amado.alves
2003-11-03 13:06 ` Lutz Donnerhacke
2003-11-03 13:07 ` Lutz Donnerhacke
2003-11-03 13:25   ` Peter Hermann
2003-11-03 16:49     ` Robert I. Eachus
2003-11-03 18:21       ` (see below)
2003-11-03 18:52       ` Jeffrey Carter
2003-11-03 20:11         ` Lutz Donnerhacke
2003-11-04  1:23         ` Robert I. Eachus
2003-11-03 13:33   ` James Rogers
2003-11-03 13:46     ` Lutz Donnerhacke
2003-11-03 13:39   ` Dmitry A. Kazakov
2003-11-03 13:54   ` Ole-Hjalmar Kristensen
2003-11-03 14:56     ` Lutz Donnerhacke
2003-11-03 15:08   ` Stefan Lucks
2003-11-03 15:40     ` Lutz Donnerhacke
2003-11-05 10:36   ` Charles Lindsey [this message]
2003-11-05 18:05     ` Lutz Donnerhacke
2003-11-06  9:48     ` Martin Dowie
2003-11-07 14:54       ` Charles Lindsey
2003-11-07 16:24         ` Martin Dowie
2003-11-07 17:34         ` Jeffrey Carter
2003-11-05 14:45   ` Jim Rogers
  -- strict thread matches above, loose matches on Subject: below --
2003-11-03 15:44 amado.alves
2003-11-03 22:12 ` Dmytry Lavrov
2003-11-03 22:27 ` Gautier Write-only
2003-11-03 13:54 amado.alves
2003-11-03 13:45 christoph.grein
2003-11-03 14:23 ` Preben Randhol
2003-11-03 15:01   ` Lutz Donnerhacke
2003-11-03 15:19     ` Dmitry A. Kazakov
2003-11-03 18:33       ` Chad R. Meiners
2003-11-03 15:48     ` Preben Randhol
2003-11-03 18:50     ` Georg Bauhaus
2003-11-03 15:00 ` Lutz Donnerhacke
2003-11-03 10:48 Lutz Donnerhacke
2003-11-03 10:51 ` Preben Randhol
2003-11-03 10:55   ` Preben Randhol
2003-11-03 11:01   ` Lutz Donnerhacke
2003-11-04  3:11 ` Steve
2003-11-05 15:54 ` sk
2003-11-06 15:40   ` Stephen Leake
2003-11-06 18:27     ` sk
2003-11-06 15:39 ` Stephen Leake
replies disabled

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