20130117
We
discussed the early days of programming, and the difficulties of
understanding programs that used "GOTO" statements to jump from line to
line. I mentioned the seminal 1968 paper "GOTO Considered
Harmful" by Edsger Dijkstra, and Dijkstra's contributions to the
development of what is now called structured programming.
As
an example of oldstyle programming we looked at a program written in
BASIC which plays a game called Hammurabi. If you look at the
program and trace the many jumps created by the GOTO statements you
will see why this type of programming is called spaghetti code.
Structured Programming
The
basic principle of structured programming is that for each discrete
segment of a program (a function, a loop, an if statement, etc.)
there should be only one entry point (ie there is no way to jump
into the middle of it), and ideally only one exit point (ie there is
only one way to get out of it). The second of these
requirements is often relaxed - we allow multiple exit points as long
as they all go to exactly the same place. Pure structured
programming (one way in, one way out) is always possible, and it can be
a good programming exercise to take a piece of unstructured code and
rewrite it so that it is fully structured. For example,
We
then looked at a translation of the Hammurabi game from BASIC into
Python. This translation removes all GOTOs and uses
functions to clean up some of the code but does not conform to the
principles of structured programming.