---
title: "Spaghetti code"
type: "antipattern"
slug: "spaghetti-code"
url: "http://localhost:3000/en/antipatterns/spaghetti-code.md"
description: "Software source code with poor structure"
---
# Spaghetti code

> Software source code with poor structure

**Spaghetti code** is [computer](https://en.wikipedia.org/wiki/Computer "Computer") [source code](https://en.wikipedia.org/wiki/Source%5Fcode "Source code") that encodes [control flow](https://en.wikipedia.org/wiki/Control%5Fflow "Control flow") that is convoluted, and therefore, hard to understand. Control statements direct [program](https://en.wikipedia.org/wiki/Computer%5Fprogram "Computer program") [execution](https://en.wikipedia.org/wiki/Execution%5F%28computing%29 "Execution (computing)") in ways that instead of having a quality of structure, resembles cooked [spaghetti](https://en.wikipedia.org/wiki/Spaghetti "Spaghetti"), twisted and tangled. The code tends to be hard to [maintain](https://en.wikipedia.org/wiki/Software%5Fmaintenance "Software maintenance").

Since control flow logic encoded via the [goto](https://en.wikipedia.org/wiki/Goto "Goto") statement tends to lead to convoluted control flow, use of goto is often associated with a classification as spaghetti code. The practice of [structured programming](https://en.wikipedia.org/wiki/Structured%5Fprogramming "Structured programming") was envisioned to eliminate the need for and use of the goto statement as one way to avoid the production of spaghetti code. Ensuring the creation of high-quality [software](https://en.wikipedia.org/wiki/Software "Software"), instead of spaghetti code, often involves aspects such as using better [tools](https://en.wikipedia.org/wiki/Software%5Ftool "Software tool"), [training](https://en.wikipedia.org/wiki/Training "Training") developers and improving [software development processes](https://en.wikipedia.org/wiki/Software%5Fdevelopment%5Fprocess "Software development process").

Spaghetti code can also describe an [anti-pattern](https://en.wikipedia.org/wiki/Anti-pattern "Anti-pattern") in which [object-oriented code](https://en.wikipedia.org/wiki/Object-oriented%5Fprogramming "Object-oriented programming") is written in a procedural style, such as by creating classes whose methods are overly long and messy, or forsaking object-oriented concepts like [polymorphism](https://en.wikipedia.org/wiki/Polymorphism%5F%28computer%5Fscience%29 "Polymorphism (computer science)"). The presence of this form of spaghetti code can significantly reduce the comprehensibility of a system.

## History

It is unclear when the phrase _spaghetti code_ was coined. Martin Hopkins made an early reference to spaghetti in this context in 1972, writing that the "principal motivation behind eliminating the goto statement is the hope that the resulting programs will not look like a bowl of spaghetti." In the 1978 book _A primer on disciplined programming using PL/I, PL/CS, and PL/CT_, [Richard Conway](https://en.wikipedia.org/wiki/Richard%5FW.%5FConway "Richard W. Conway") described programs that "have the same clean logical structure as a plate of spaghetti", a phrase repeated in the 1979 book _An Introduction to Programming_ he co-authored with [David Gries](https://en.wikipedia.org/wiki/David%5FGries "David Gries"). In the 1988 paper _A spiral model of software development and enhancement_, the term is used to describe the older practice of the _code and fix model_, which lacked planning and eventually led to the development of the [waterfall model](https://en.wikipedia.org/wiki/Waterfall%5Fmodel "Waterfall model"). In the 1979 book _Structured programming for the COBOL programmer_, author Paul Noll uses the phrases _spaghetti code_ and _rat's nest_ as synonyms to describe poorly structured source code.

In the _Ada – Europe '93_ conference, [Ada](https://en.wikipedia.org/wiki/Ada%5F%28programming%5Flanguage%29 "Ada (programming language)") was described as forcing the programmer to "produce understandable, instead of spaghetti code", because of its restrictive exception propagation mechanism.

In a 1980 publication by the [United States National Bureau of Standards](https://en.wikipedia.org/wiki/National%5FInstitute%5Fof%5FStandards%5Fand%5FTechnology "National Institute of Standards and Technology"), the phrase _spaghetti program_ was used to describe older programs having "fragmented and scattered files".

In a 1981 computer languages spoof in _The Michigan Technic_ titled "BASICally speaking...FORTRAN bytes!!", the author described [FORTRAN](https://en.wikipedia.org/wiki/FORTRAN "FORTRAN") stating that "it consists entirely of spaghetti code".

[Richard Hamming](https://en.wikipedia.org/wiki/Richard%5FHamming "Richard Hamming") described in his lectures the etymology of the term in the context of early programming in binary codes:

> If, in fixing up an error, you wanted to insert some omitted instructions then you took the immediately preceding instruction and replaced it by a transfer to some empty space. There you put in the instruction you just wrote over, added the instructions you wanted to insert, and then followed by a transfer back to the main program. Thus the program soon became a sequence of jumps of the control to strange places. When, as almost always happens, there were errors in the corrections you then used the same trick again, using some other available space. As a result _the control path of the program through storage soon took on the appearance of a can of spaghetti._ Why not simply insert them in the run of instructions? Because then you would have to go over the entire program and change all the addresses which referred to any of the moved instructions! Anything but that!

## Examples

### Simple

The following [BASIC](https://en.wikipedia.org/wiki/BASIC "BASIC") code, a program that prints 1 to 100, is a relatively simple example of code that can be more easily understood with structured control flow instead of using goto. The use of `GOTO` for looping and lack of indentation leads to less than clear logic flow.

1 i=0
2 i=i+1
3 PRINT i
4 IF i>=100 THEN GOTO 6
5 GOTO 2
6 END

The following code produces the same result, but uses a structured [loop statement](https://en.wikipedia.org/wiki/Loop%5F%28computing%29 "Loop (computing)") and indentation to improve readability.

1 FOR i=1 TO 100
2     PRINT i
3 NEXT i
4 END

### More representative

The following code implements a numeric [sorting algorithm](https://en.wikipedia.org/wiki/Sorting%5Falgorithm "Sorting algorithm"). The use of goto statements results in a spaghetti-like nature to the control flow.

  INPUT "How many numbers should be sorted? "; T
  DIM n(T)
  FOR i = 1 TO T
    PRINT "NUMBER:"; i
    INPUT n(i)
  NEXT i
  'Calculations:
  C = T
E180:
  C = INT(C / 2)
  IF C = 0 THEN GOTO C330
  D = T - C
  E = 1
I220:
  f = E
F230:
  g = f + C
  IF n(f) > n(g) THEN SWAP n(f), n(g)
  f = f - C
  IF f > 0 THEN GOTO F230
  E = E + 1
  IF E > D THEN GOTO E180
  GOTO I220
C330:
  PRINT "The sorted list is"
  FOR i = 1 TO T
    PRINT n(i)
  NEXT i

## Related

### Big ball of mud

A big ball of mud is a [software system](https://en.wikipedia.org/wiki/Software%5Fsystem "Software system") that lacks a perceivable architecture. Although undesirable from a software engineering point of view, such systems are common in practice due to business pressures, developer [turnover](https://en.wikipedia.org/wiki/Turnover%5F%28employment%29 "Turnover (employment)") and [software entropy](https://en.wikipedia.org/wiki/Software%5Fentropy "Software entropy"). The term was popularized by Brian Foote and Joseph Yoder although they credit Brian Marick for coining the term.

> A Big Ball of Mud is a haphazardly structured, sprawling, sloppy, duct-tape-and-baling-wire, spaghetti-code jungle. These systems show unmistakable signs of unregulated growth, and repeated, expedient repair. Information is shared promiscuously among distant elements of the system, often to the point where nearly all the important information becomes global or duplicated.
>
> The overall structure of the system may never have been well defined.
>
> If it was, it may have eroded beyond recognition. Programmers with a shred of architectural sensibility shun these quagmires. Only those who are unconcerned about architecture, and, perhaps, are comfortable with the inertia of the day-to-day chore of patching the holes in these failing dikes, are content to work on such systems.

— Brian Foote and Joseph Yoder, _Big Ball of Mud._ Fourth Conference on Patterns Languages of Programs (PLoP '97/EuroPLoP '97) Monticello, Illinois, September 1997

### Pasta-related

Inspired by the popularity of _spaghetti code_, other [pasta](https://en.wikipedia.org/wiki/Pasta "Pasta")\-oriented terms that describe the structural nature of code include:

Lasagna code

[Lasagna](https://en.wikipedia.org/wiki/Lasagna "Lasagna") code has [layers](https://en.wikipedia.org/wiki/Architectural%5Flayer "Architectural layer") that are so intertwined that making a change in one layer necessitates changing other layers too.

Ravioli code

[Ravioli](https://en.wikipedia.org/wiki/Ravioli "Ravioli") code comprises well-structured [classes](https://en.wikipedia.org/wiki/Class%5F%28programming%29 "Class (programming)") that are easy to understand in isolation but in combination result in less than clear system design.
