Spaghetti code

Software source code with poor structure
title: "Spaghetti code" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["anti-patterns", "articles-with-example-basic-code", "software-engineering-folklore", "pejorative-terms-related-to-technology", "metaphors-referring-to-spaghetti"] description: "Software source code with poor structure" topic_path: "technology/software-engineering" source: "https://en.wikipedia.org/wiki/Spaghetti_code" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary Software source code with poor structure ::
::figure[src="https://upload.wikimedia.org/wikipedia/commons/d/dc/1816Filipino_spaghetti_03.jpg" caption="Cooked spaghetti, characteristically tangled" alt="Description of image"] ::
Spaghetti code is computer source code that encodes control flow that is convoluted, and therefore, hard to understand. Control statements direct program execution in ways that instead of having a quality of structure, resembles cooked spaghetti, twisted and tangled. The code tends to be hard to maintain.
Since control flow logic encoded via the 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 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, instead of spaghetti code, often involves aspects such as using better tools, training developers and improving software development processes.
Spaghetti code can also describe an anti-pattern in which object-oriented code 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. 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 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. 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. 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 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, 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 stating that "it consists entirely of spaghetti code".{{cite journal|journal=The Michigan Technic|title=BASICally speaking...FORTRAN bytes!!|author=MTSBS| volume=99|issue=4|date=March–April 1981}}
Richard Hamming described in his lectures the etymology of the term in the context of early programming in binary codes:
::quote
::
|text=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 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 for looping and lack of indentation leads to less than clear logic flow.
::code[lang=basic] 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 and indentation to improve readability.
::code[lang=basic] 1 FOR i=1 TO 100 2 PRINT i 3 NEXT i 4 END ::
More representative
The following code implements a numeric sorting algorithm. The use of goto statements results in a spaghetti-like nature to the control flow.
::code[lang=basic] 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{{anchor |big-ball-o-mud}}
A big ball of mud is a 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 and 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-oriented terms that describe the structural nature of code include:
; Lasagna codelasagna-code: Lasagna code has layers that are so intertwined that making a change in one layer necessitates changing other layers too.
; Ravioli coderavioli-code: Ravioli code comprises well-structured classes that are easy to understand in isolation but in combination result in less than clear system design.
References
References
- (2008). "Java Concepts for AP Computer Science". J. Wiley & Sons.
- (2005). "Pronouns and procedural meaning: The relevance of spaghetti code and paranoid delusion". Oxford University Working Papers in Linguistics, Philology and Phonetics.
- (2004). "Straightening spaghetti-code with refactoring?". Software Engineering Research and Practice.
- (January 2010). "DECOR: A Method for the Specification and Detection of Code and Design Smells". IEEE Transactions on Software Engineering.
- (2011). "2011 15th European Conference on Software Maintenance and Reengineering".
- Hopkins, M. E. (1972): A Case fo the GOTO. In: ACM '72: Proceedings of the ACM annual conference - Volume 2, August 1972, pp 787–790, p 59 DOI:https://dl.acm.org/doi/10.1145/800194.805860
- Conway, Richard. (1978). "A primer on disciplined programming using PL/I, PL/CS, and PL/CT". Winthrop Publishers.
- (1979). "An Introduction to Programming". Little, Brown.
- Boehm, Barry W.. (May 1988). "A spiral model of software development and enhancement". IEEE Computer.
- Noll, Paul. (1977). "Structured programming for the COBOL programmer: design, documentation, coding, testing". M. Murach & Associates.
- Schwille, Jürgen. (1993). "Use and abuse of exceptions — 12 guidelines for proper exception handling". Springer Berlin Heidelberg.
- United States National Bureau of Standards. (1980). "ASTM special technical publication". United States Government Printing Office.
- (1996). "The Art of Doing Science and Engineering". Taylor & Francis.
- (26 June 1999). "Big Ball of Mud".
- (October 2014). "Teaching Good Practices In Software Engineering by Counterexamples". Computer Science and Education in Computer Science.
- (13 May 1991). "The OO-binary relationship model : A truly object oriented conceptual model".
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::