The world of unstructured programming, i.e. spaghetti code

Much of the programming prior to 1970 was what is now considered unstructured. It is often synonymous with the pejorative term spaghetti code, meaning source code with a complex and tangled control structure. Languages such as Fortran were heavily reliant on programming structures such as goto, which provided much of their control framework. It provided jumps from one part of the program to another part – often in an extremely unstructured manner. By the mid-1960s, programming pioneers such as Edsger W. Dijkstra were starting to become concerned about programming clarity, correctness and structure. He contended that modular, goto-less programs are more easily created and understood by humans. In 1968 Dijkstra published a letter, “Go To statement considered harmful” in which he condemned the indiscriminate use of the goto statement [1]. One statement stands out: “The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one’s program”. Instead he advocated the use of procedures, and conditional and repetition for program control.

Fortran may have been one of the greatest offenders when it came to spaghetti code due to all the control structures which relied on jumping a label. Fortran contained the DO looping structure, the computed GO TO, the unconditional branch, the assigned GO TO, and the arithmetic if. One of the worst offenders may be the ability to use a goto to enter and leave a still executing do loop. Consider the small piece of code below, which was once legal (now the compiler will throw a warning, but still compile. It basically jumps from inside the DO loop to outside the DO loop, to back inside the DO loop again, ad infinitum. The value of the index variable I, actually never changes from its initial value 1.

      PROGRAM DO_LOOP
      J=0
  10  DO 15,I = 1,10
      GO TO 13
  12  J=J+1
  13  WRITE(*,*) "inside loop ", J
      GO TO 20
  15  CONTINUE
  20  J=J+1
      WRITE(*,*) "outside loop ", J
      GO TO 12
      END

Fortran didn’t introduce structured programming until 1978, gradually removing or restricting the offending jump statements. The goto statement is therefore intrinsically linked to the notion of spaghetti code. Adding one goto statement to a program does not make it spaghetti code. Spaghetti code is best understood by analyzing a legacy Fortran or Basic program. If it is impossible to follow the algorithmic flow of a program, then it is likely spaghetti code.

Consider an algorithm which takes a year and numeric value for a day in the year and calculates the day of the particular month the number represents.

doy_unstructured

Now compare the structured code to the spaghetti version. Which program is more readable, or can be easily traced?

doy_structured

Now if you have a haphazardly structured, sprawling, sloppy, duct-tape-and-baling-wire, spaghetti-code jungle it’s called a big ball of mud [2].

 

[1] Dijkstra, E., “Go To Statement Considered Harmful”, Communications of the ACM, Vol.11:3, pp.147–148, 1968.

[2] Foote, B., Yoder, J., “Big ball of mud”, Conf. on Patterns Languages of Programs (1997).

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.