[comp.misc] GOTO Wars

bernsten@phoenix.Princeton.EDU (Dan Bernstein) (05/10/89)

At the machine language level of almost every computer in existence,
the only ways to change the flow of control are conditional and
unconditional jumps. (Subroutine calls are unconditional jumps.)

High-level languages provide many flow control mechansisms; all of
them may be stated as restrictions upon the target of a jump, and a
mechanism for stating such restricted jumps. The more restricted the
target is, the more abbreviation the mechanism provides. For example,
longjmp does not restrict the jump in any way and hence is no more
concise than an assembly-language jump. while/for/do all restrict
the jump to a specific spot in the loop and hence need no labelling.

GOTO Wars uniformly consist of examples of algorithms that may or may
not be more concise or efficient with GOTOs than with a selected set
of higher-level flow-control mechanisms. The conclusion of the Wars
should be a set of mechanisms comprehensive enough to cover all jumps
that people want to use, without sacrificing any efficiency or
conciseness. Until such a set is known, we must include GOTO; when
such a set is known, GOTO can disappear.

Often the Wars lead to different conclusions that are in my opinion
off track. For example, someone presents an algorithm with nested for
loops and a GOTO, and concludes that GOTO is necessary. The response
*should* be that the GOTO can be handled with full efficiency by a break,
or by a multi-level break, or by ADA's exception mechanism, or by some
other technique; until someone finds a reasonably general mechanism
to handle the GOTO with the same efficiency or conciseness, there is
no good response.

Instead, the typical response is ``well, as you know from your computer
science class, all GOTOs can be replaced by flags, like this, so we keep
the structure: ...'' Of course the instruction pointer is just another
aspect of the computer's state, and changes in it can be represented
abstractly by setting a flag rather than directly by executing a jump---
but the abstraction is not as efficient, and I would have a hard time
calling it more structured. After all, the change of flow control is
still explicit and is just as annoying as a GOTO to trace.

To head the discussion in a positive direction, let me challenge
GOTO users as follows. Some flow control mechanisms in high-level
languages are as follows:

  1. subroutine (procedure, function)
  2. if()...elsif()...elsif()...elsif()...fi (case, select, switch)
  3. while...()do...repeat (for, do, until, repeat)
  4. ADA exception handling
  5. multi-level breaks

() means a condition and ... mean some number of statements,
expressions, operations, whatever. Give an example of an algorithm
or program than can be stated more concisely or efficiently by
including GOTO in the above list.

---Dan Bernstein, bernsten@phoenix.princeton.edu