I have felt for a long time that a talent for programming consists largely of the ability to switch readily from microscopic to macroscopic views of things, i.e., to change levels of abstraction fluently.
— Donald Knuth, “Structured Programming with go to Statements”
We’ve been looking at Edsger Dijkstra’s principles of structured programming for some time now. Today we’ll conclude that phase of this series with a look at Donald Knuth’s “Structured Programming with go to Statements” (1974). Since “Go To Statement Considered Harmful” was the most famous declaration of the structured programming ethos, Knuth’s title itself was a kind of provocative joke — like naming a paper “Judaism with Idols” or “Vegetarianism with Pork Chops.” And that combination of playfulness and attentive humor extends throughout the article, from the epigraphic references to Bob Dylan (and a laxative advertisement!) on. In the middle of a complex analysis of an algorithm in ALGOL, in which Knuth is reviewing ideas for new ways to notate a particular kind of loop, he interjects:
Readers who remember 1967 will also appreciate [this] second suggestion,
turn on begin S; when B drop out; T; end.
The levity’s a good thing, because, I confess, as a non-mathematician and only a rudimentary programmer I approached Knuth’s formula-laden text with trepidation. And there remain parts of “Structured Programming with go to Statements” that are beyond me. But for the most part, this text bears out Knuth’s reputation for brilliant clarity — it’s surprisingly accessible.
Knuth walks us through the history of the “go to” controversy and explores cases where eliminating “go to” doesn’t really work — particularly in efforts to optimize the efficiency of little loops of code that a program must frequently repeat. A short way into the discussion, Knuth admits his own “vested interest in go to statements”: They are a basic building block of the assembler code he uses for examples throughout his famous series of books on The Art of Computer Programming. Still, he’s fundamentally sympathetic to Dijkstra’s argument about structured programming; he just insists that the “go to” issue is a symptom, not the heart of the problem:
There has been far too much emphasis on go to elimination instead of on the really important issues; people have a natural tendency to set up an easily understood quantitative goal like the abolition of jumps, instead of working directly for a qualitative goal like good program structure…. Probably the worst mistake any one can make with respect to the subject of go to statements is to assume that “structured programming” is achieved byt writing programs as we always have and then eliminating the go to‘s. Most go to‘s shouldn’t be there in the first place! What we really want is to conceive of our program in such a w3ay that we rarely even think about go to statements, because the real need for them hardly ever arises. The language in which we express our ideas has a strong influence on our thought processes. Therefore, Dijkstra asks for more new language features — structures which encourage clear thinking — in order to avoid the go to‘s temptations toward complications.
I appreciated Knuth’s argument, but even more, his attention to the expressive power and comprehensibility of different ways of writing programs (“The word do as used in ALGOL has never sounded quite right to native speakers of English; it has always been rather quaint for us to say ‘do read (A[i])’ or ‘do begin‘!”). This sensitivity leads Knuth, in the latter part of the paper, towards the principle he would later expound in his advocacy of “literate programming”: “A programmer should create a program P which is readily understood and well-documented, and then he should optimize it into a program Q which is very efficient.” A go to at the second, optimized stage would be acceptable, he says, because the programmer has already expressed the program in terms that make its structure clear and allow for the removal of errors and bugs.
I don’t know how widely Knuth is read today (or ever was); his attention to assembler-level code may feel distant from the concerns of the everyday developer focused on getting some mundane job done. But it seems to me that the habits of thought you can observe at work in “Structured Programming with go to Statements” — patience, precision, a willingness to examine principles from all angles and an openness to being amused — are of interest and value to anyone, in any field.
BONUS LINK: In this anecdote from Andy Hertzfeld’s Macintosh Folklore site, Knuth calls Steve Jobs on his claim to have read “all” Knuth’s books.
[tags]code reads, donald knuth, go to, programming, software development[/tags]
There are no revisions for this post.