Understanding the harmfulness of goto statement referring to Edsger Dijkstra's Journal Paper of ACM-1968

Ok; I've now read the actual paper.

There is certainly some of that.

I think that a lot of his argument is that once you have a label that is the target of GOTOs, it becomes non-deterministic how you have reached that point if you're trying to analyze the "dynamic" behavior of the program - it all looks fine there on your programming form ("static"), but actual analysis is hard at runtime.

So, yeah, I guess that that's a thing. But it's largely a result of how programming languages were implemented and used back before 1968. If you look at BASIC and Fortran 4 (or 66) (the languages that I'm somewhat familiar with), there were no code blocks, and the only looping construct was a counting FOR-loop-like thing. There wasn't even ELSE...
BASIC had if (condition) then <linenumber> - so if/else code would look like:

100 IF (B > 0) THEN 200
110   REM B <= 0 (else clause)
120   C =  COS(-B)
130   D = SIN(B)
130   GOTO 300
200   REM B > 0 (if clause)
210   C = COS(B)
220   D = SIN(-B)
300 REM end of if/else

Fortran let you put any single statement after your IF, so it'd look more like:

       IF (B<0) GOTO 200
C else clause
         C = COS(-B)
         D = SIN(B)
         GOTO 300
C if clause
200      C = COS(B)
         D = SIN(-B)
300

Those are pretty awful. It's hard to say whether Dijkstra's point was more "don't use crappy languages" (which is how such arguments tend to be stated now - "my language has this great feature that allows better clarity in the expression of ideas!") See also Timeline of programming languages - Wikipedia
(Just for completion, here's C/C++):

  if (B >= 0) {
    C = cos(B);
    D = sin(-B);
  } else {
    C = cos(-B);
    D = sin(B);
  }

I ALMOST think that part of the argument includes "if you use GOTO, it should be in the context of standard code structures like if/else, while loops, and so on. While the above code is pretty ugly, it doesn't get really bad until some other part of the program does "goto 300" or (worse) "goto 200"!

presented as a Flow Chart

Not fair. A flow chart is biased toward "goto-like" actions. In my PL/1 class where there were emphasizing "structured programming", they actively discouraged "flow charts" for that reason, in favor of another form of diagram (heh. Which doesn't seem to have caught on. Nassi–Shneiderman diagram - Wikipedia, I think? (oops. Already mentioned. I confess to have only scanned the 100+ messages in this thread.) The whole diagramming thing wasn't THAT strongly encouraged; sort of "if you use proper code structures, it's probably obvious.")

You want this, right? Which IMO is clearer than the flow chart in the first place.

  while (!elapsed(3*ONESEC)) {
    if (buttonOpen()) break;
  }
  LED2Off();

Or perhaps:

   while (buttonClosed()) {
     if (elapsed(3*ONESEC)) break;
   }
   LED2Off();
2 Likes

Having watched the evolution of hardware and software from the mid 1970s, my thoughts are that as languages and compilers in particular have evolved, the importance of keeping the stack balanced became critical.

You can do anything in assembly language, and interpreters naturally ‘compartmentalise’ the code to avoid these issues - both with tradeoffs.

Optimizing Compilers however, are smart enough to look at the code, and other than obvious structural flaws, they try to work around the programmers expectations with warnings and errors

Other than frivolous misuse of memory in design, poorly placed goto is probably the single fastest way to leak memory, and crash your app without explanation.

3 Likes

I don't see the use of GOTO as the main issue here. That is only a symptom (a) of a restriction in the language or (b) of a bad approach to system design.

In the former case the example of Fortran IV has already been presented. Where a simple programming metaphor for the missing if/then/else construct or the missing do/while loop construct is built using GOTOs then the program is quite readable.

In the latter case, where the programmer has not taken the trouble to model the real world problem by breaking it down into simple procedures which have some analog in the real world but simply codes in an ad-hoc style jumping around between bits of code, maybe setting some flag so he knows where to jump back to, there is likely to be a high density of GOTOs and an unreadable program. However, simply banning the use a specific program statement, in this case, GOTO, is not going to magically create better programs/programmers.

Neither do I see a basic problem with flow charts, at least not those at the higher level above that of say simple selection and repetition groups. For example, a finite state machine is (IMO) best represented by a (specialised) flowchart. However, to have used the state machine design pattern optimally, the programmer has gone through the mental exercise of identifying the states in a real world problem and brought and grouped these with their triggers and actions somehow onto paper in the form of a state diagram.

Using the example of the finite state machine, I'd have no problem say in the case of state "waiting for key press" was exited, using a GOTO, to state "in sending IR code sequence". In that case, the GOTO is simply representing a passing of control between entities which are tangible in the real world and not simply jumping around between program lines which are many levels of abstraction away from the original real world problem.

3 Likes

I disagree with the notion of "word salad." I think the intent was to bring formalism to the discussion.

During the 1960s, significant efforts were directed towards establishing a mathematical foundation for programming languages, including proofs of code correctness.

Dijkstra was a prominent computer scientist and researcher in that field. He made significant contributions to algorithm design, programming language theory, and software engineering in general. His pioneering work in structured programming laid the foundation for many modern programming practices and concepts.

At the time of his article, researchers were trying to devise a mathematical theory and notation that could represent the code and its evolution during operations. This was part of a broader effort in the computer science community to formalize and improve programming practices, ensuring that code could be proven correct and more easily understood, maintained, and reasoned about.

His article is grounded in his observation that the more GOTOs you find in someone's code, the more likely it is that this programmer is not good. As he puts it more formally, "the quality of programmers is a decreasing function of the density of GOTO statements in the programs they produce." ➜ that's researcher/mathematical talk.

Then he infers that for simple statements, written in a sequential way, building a mental representation of the process is easy— and it is easy to model mathematically. But the use of functions (procedures), repetitions, and recursion opens up the complexity (vectors were introduced in the formalism), and having to deal with GOTO was wreaking havoc in the otherwise beautiful mathematical construct.

It introduced hard-to-model evolution of the process, and if it can't be expressed easily mathematically, it's likely not mastered by the developer in terms of consequence.

Hence, the more GOTOs you have, the less likely you have quality code (or coder).

2 Likes

Your remark mirrors what Dijkstra said back in 1968 in the referred Journal paper by this precise statement: "The unbridled use of the goto statement has an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress." which when submitted to ChatGPT for analytic meaning, it has offered the following that does not require any paraphrasing.

This statement highlights the negative impact of using the goto statement excessively. Here's a breakdown of the analysis:

  1. Unbridled Use of the goto Statement: This refers to using the goto statement without restraint or moderation. When programmers use goto frequently and without careful consideration, it can lead to code that is difficult to understand and maintain.
  2. Immediate Consequence: This indicates that the negative effects of excessive goto usage are felt immediately. There is no delay in experiencing the problems caused by its use.
  3. Difficulty in Finding a Meaningful Set: This suggests that due to the chaotic nature of code with excessive goto statements, it becomes challenging to identify a coherent and meaningful set or structure within which to describe the progression of the program. In simpler terms, it's hard to establish a logical framework to explain how the program flows from one point to another.
  4. Describing the Process Progress: This refers to explaining or understanding how the program moves forward or progresses. The statement implies that because of the disorganization caused by excessive goto statements, it becomes difficult to track and describe the sequence of actions or steps the program takes to accomplish its tasks.

Overall, this analysis underscores the importance of using goto judiciously and highlights the immediate and detrimental effects of its excessive usage on code clarity and maintainability.

Since AI arguments have already begun to be made here, I think it’s time for me to unsubscribe from the topic.

Is it against the rule of Forum that AI's opinion cannot be referred? AI collects information from the database what the other people have said on the issue in question.

Yes, AI-suggestions are strictly not welcome in the forum, see the topic

well, the consensus is well phrased in there

AI generated content

Artificial intelligence (AI) services such as ChatGPT are able to generate Arduino project code and answers for Arduino-related questions. As with other non-authoritative sources of information found on the Internet, this should be used with caution.

Questions about information provided by an AI

If you would like to ask for assistance with AI-generated code or information here on the forum, please clearly identify the source.

Using AI-generated content to provide assistance

When providing assistance, copy/pasting content without giving any thought to the information is irresponsible, regardless of the source of that information.

Please carefully evaluate AI-generated content for accuracy, relevance, and appropriateness within the context before using it to provide assistance. If you are not able to make such an evaluation, then please don't share the content here on the forum.

AI-generated content is not forbidden, context must be expressed clearly and used with caution.

So I don't see the former post 135 infringing this since @GolamMostafa clearly stated

which when submitted to ChatGPT for analytic meaning, it has offered the following that does not require any paraphrasing

but I see limited value in the post, who cares about what chatGPT takes out of this - @GolamMostafa's personal opinion or experience would have been more valuable for the discussion.

2 Likes

Agree, the cause of my message was that I don’t see the slightest point in introducing the opinion of AI into our discussion.... and for many other discussions as well.

got it - fair.

gotos are useful when handling exceptions.
when done properly, they can very significantly simplify the code and make it easier to maintain

Does GOTO allow multiple entrances into contexts? I can see escaping from a deeply nested structure, but would it allow the converse of re-entering into that structure? From potentially multiple entry points?

Ancient FORTRAN and BASIC serial code didn’t really have stack frames to discard.

Does goto discard the intervening stack frames?

Edit:
Yes, it does discard the local variables:

https://en.cppreference.com/w/cpp/language/goto

Interesting as well at the linked article is the part that begins

If transfer of control enters the scope of any automatic variables…

and I'm glad I don't have to even worry about it.

a7

2 Likes

From this thread I learned that the common “combine these two sketches” query is often like an ugly “combine these two flowcharts“ problem that would be better served by “translate the sketches’ flowcharts into NSD diagrams“

Hat-tip:

I make my coding style up as I go along and my mental picture is informed from something I read somewhere at some time that just happened to use the word "wrapper". So, what if the main thing? What is the next thing inside that that depends on the main thing? What's the next thing inside that second thing and so on.

@Robin2's planning tutorial is excellent in demonstrating working down through the "wrappers" in an Arduino context:

1 Like

Thanks, bud! First I've seen of this, I'll check it out! Respect to @Robin2 and a :saluting_face: to you!

2 Likes

A journal paper typically spans 10 to 15 pages on A4-sized paper; whereas, Dijkstra's paper is only one and a half pages long which implies that the information has been condensed by a factor of eight. I have seen similar papers in many technical conference proceedings and journals, often submitted by renowned experts.

I have been expecting that some of the contributors of this thread by virtue of their knowledge and experience, would elaborate on the glittering meanings of this widely read journal paper although some posters have provided general insights and NOT paragraph-by-paragraph.

Due to my own limitations, I have not felt capable of analyzing the paper; instead, (with good intentions), I have submitted my preferred paragraph #11 of the paper to ChatGPT to explore its analytical meaning being known that the AI collects information from the knowledgebase of other people.

Being inline with my plan, I have submitted the outcome of AI unchanged in this thread for constructive criticism by the forum members to asses how close/apart the AI's interpretation is to/from what Dijkstra's has said in his condensed paper.

ChatGPT/AI is not always as good as a human being, which has been revealed by its recent inability to provide the synonym "uncontrolled" for the word "unbridled" of paragraph #11 of the said journal paper. However, it is still helpful in offering clues and codes to resolve complex programming issues.

History of science shows that people have always strived to devise new tools to meet their needs, and ChatGPT/AI is one of such tools that should be used with proper judgement.

ChatGPT is nowadays used in an extensive way to write papers, especially in the non-tecnical world where nobody tries to replicate or verify results. "Surprisingly" the quality of papers did not suffer. I think this tells a lot of the overall quality of scientific papers (there were some profound studies on the decline of papers published some 5 years ago - > 90% of all published papers are not worth the virtual paper they are written on).

1 Like