The Trick Of The Mind -The Strange Incident of The Goto Considered Harmful
Written by Mike James   
Monday, 31 January 2022
Article Index
The Trick Of The Mind -The Strange Incident of The Goto Considered Harmful
Spaghetti

Trick360

Spaghetti

So far so good. It sounds theoretically nice that all we need to create any flow of control in a program is the Goto – a single principle has to be simple? Yes, but what it can be used to create is far from simple.

Early programmers, indeed any programmers who used the Goto, tended to be opportunistic. What this means is that if they were writing some instructions and noticed that a Goto to some location in the list would be useful they just did it. The result is that most programs of the Goto era have a flow of control graph that resembles a bowl of spaghetti or tangled string. The Goto caused jumps to far flung locations within the program without any rhyme or reason to the innocent reader of the program. This made programs unintelligible even to the programmer who had written them!

This situation came to the attention of Dutch computer scientist Edsger Dijkstra who was very interested in the deeper ideas behind programming. He thought that programs were cleaner and easier to understand using a different set of constructs to the Goto. This was a revolutionary idea, but at the time programmers weren’t really interested in such elevated thinking.

In 1968 the idea that Dijkstra had come up with burst upon the wider world in a letter from him to the influential Communications of the ACM (Association for Computing Machinery) entitled, "The GOTO considered harmful".

The letter was an explanation of why the GOTO was a dangerous command in intellectual terms, but it also carried an emotional overtone which many found difficult to take:

"For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the density of go to statements in the programs they produce. More recently I discovered why the use of the go to statement has such disastrous effects, and I became convinced that the go to statement should be abolished from all "higher level" programming languages (i.e. everything except, perhaps, plain machine code). At that time I did not attach too much importance to this discovery; I now submit my considerations for publication because in very recent discussions in which the subject turned up, I have been urged to do so."

Dijkstra underestimated the magnitude of the task he had set himself. There are still programmers today who do not understand Dijkstra's argument, even though it has been refined over the years. Dijkstra coined the terms "goto-less programming" and "structured programming" and started a revolution in programming methods. However, this was not well received. The reason was that the machines that were running the programs worked in terms of Goto, which they generally called a “jump instruction”. What Dijkstra was suggesting was moving away from how the machine worked to something more abstract and hopefully better. This is a trend that continues today, but back then it seemed unnecessary and heretical.

Dijkstra continued his attack by singling out languages that used the Goto:

"The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense."

"It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration."

The claim of “mental mutilation” may sound extreme, but it really isn’t far from the truth. Much of the resistance to the idea was due not just to the rational rejection of an idea that seemed unnecessary but an inability to see how to program using the new constructs. Even today, if a programmer encounters the apparent restrictions of structured programming they find it difficult to give up the freedom of the unrestricted Goto.

In book but not in this extract

  • Structured Programming
  • The Goto Can Be Tamed!
  • How One May Think
  • Turing Completeness Revisited
  •  Nesting

Now?

It is difficult to put ourselves back in the position of the early programmers trying to do something on a regular basis that was once a rare occupation. The Goto incident is an example of the move away from the way programs are created to a more abstract view. In this case feelings were running high as programmers were being told that the way that they had been programming was very wrong and it was implied that they were damaged beyond recovery by the experience.

This may sound extreme, but in many cases it was true. It was difficult to see how obviously correct the Goto-less approach was because the people that needed to be persuaded were unable to see how to program using it. They saw it as an unnecessary straitjacket and confirmation bias made it possible to aggressively reject it.

It took a long time for programmers to see the error of their ways and retrain themselves to think in terms of the structured programming primitives. Even today you will still find new questions being asked on forums “What IS so bad about the Goto”, usually with a rider that the questioner wouldn’t dream of using a Goto or that they are asking for a “friend”.

Today there really is no dissent from the party line and all of the major programming language support structured Goto-less programming. What is perhaps strange is that many still have instruction labels and a Goto instruction often hidden in the documentation with big warning signs that something terrible will happen to any programmer who dares to use them!

Is there a better way than structured programming?

Probably not.

Summary

  • Initially the only way to modify the flow of control was to use the Goto or an equivalent which transferred control to another instruction.

  • Line numbers were the first way to locate a particular instruction, but these change if the list of instructions is modified.

  • Line number were replaced by the idea of labels which can be numbers or meaningful titles.

  • The problem with the Goto is that it gives you no guidance in how to use it. Early programmers just used to change the flow of control as seemed useful at the time. The result was that the flow of control graph was often a tangled mess – hence the term ”spaghetti programming”

  • Edsger Dijkstra decided to put things right with an attack on the Goto in a famous academic paper – The GOTO considered harmful.

  • Dijkstra formulated an alternative to the Goto in the form of structured programming which provided a way to construct a conditional and a loop without the use of the Goto.

  • Despite the fact that you could do anything with structure programming that you could do with the Goto and the flow of control graph was easier to keep simple using them programmers were resistant to the change.

  • Today few programmers would argue with Dijkstra’s ideas and many use them without even being aware that there once was a much worse way of doing things.

 

Related Articles

The Goto, Spaghetti and the Velociraptor

Edsger Dijkstra - The Poetry Of Programming

I TOLD You Gotos Are Dangerous!  

The Trick Of The Mind - Programming & ComputationalThought

Buy Now From Amazon

Trick360

Chapter List

  1. The Trick Of The Mind

  2. Little Languages
       Extract: Little Languages Arithmetic

  3. Big Languages Are Turing Complete

  4. The Strange Incident of The Goto Considered Harmful
       Extract: The Goto Considered Harmful

  5. On Being Variable  

  6. Representation

  7. The Loop Zoo
       Extract The Loop Zoo
      
    Extract Advanced Loops ***NEW!!

  8. Modules, Subroutines, Procedures and Functions
       Extract Modular Programming

  9. Top-Down Programming 

  10. Algorithms
       Extract: Binary Search 

  11. The Scientific Method As Debugging 

  12. The Object Of It All
       Extract Why Objects 

 <ASIN:1871962722>

<ASIN:B09MDL5J1S>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


GitHub Introduces Code Scanning
26/03/2024

GitHub has announced a public beta of a code scanner that automatically fixes problems. The new feature was announced back in November, but has now moved to public beta status.  



SnapCode: A Java IDE for the Web
27/02/2024

Thanks to CheerpJ and WebAssembly you can now run a Java IDE inside your browser and local first.This is SnapCode, and while lightweight and in-browser, is to be not underestimated.


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Tuesday, 01 February 2022 )