Spaghetti code: Difference between revisions
imported>Strait →Examples: cap |
imported>Jaeanthe →Simple: Fixed typo. |
||
| Line 1: | Line 1: | ||
{{short description|Software source code with poor structure}} | {{short description |Software source code with poor structure}} | ||
[[File:1816Filipino spaghetti 03.jpg|thumb|alt=Description of image |Cooked spaghetti, characteristically tangled]] | |||
'''Spaghetti code''' is [[computer]] [[source code]] that encodes [[control flow]] that is convoluted, and therefore, hard to understand. | |||
Control statements direct [[computer program |program]] [[Execution (computing)|execution]] in ways that instead of having a quality of structure, resembles cooked [[spaghetti]], twisted and tangled<!-- pages 235–236 -->.<ref>{{cite book|last1=Horstmann|first1=Cay|title=Java Concepts for AP Computer Science|date=2008|publisher=J. Wiley & Sons|location=Hoboken, NJ|isbn=978-0-470-18160-7|pages=235–236|edition=5th ed. [i.e. 2nd ed.].|chapter-url=http://horstmann.com/bigjava3.html|access-date=2 January 2017|language=en|chapter=Chapter 6 - Iteration}}</ref> | |||
The code tends to be hard to [[Software maintenance |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.<ref name="Cram5">{{cite journal|last1=Cram|first1=David|last2=Hedley|first2=Paul|title=Pronouns and procedural meaning: The relevance of spaghetti code and paranoid delusion|journal=Oxford University Working Papers in Linguistics, Philology and Phonetics|date=2005|volume=10|pages=187–210|url=http://mostlyharmless.org.uk/wp-content/uploads/2010/12/cramhedley-web.pdf|access-date=5 March 2018|archive-date=6 March 2018|archive-url=https://web.archive.org/web/20180306022905/http://mostlyharmless.org.uk/wp-content/uploads/2010/12/cramhedley-web.pdf|url-status=dead}}</ref> 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 [[software tool |tools]], [[training]] developers and improving [[software development process]]es.<ref name="Markus4">{{cite journal|last1=Markus|first1=Pizka|title=Straightening spaghetti-code with refactoring?|journal=Software Engineering Research and Practice|date=2004|pages=846–852|url=http://itestra.com/wp-content/uploads/2017/08/04_itestra_straightening_spaghetti_code_with_refactoring.pdf|access-date=5 March 2018|archive-date=5 March 2018|archive-url=https://web.archive.org/web/20180305202716/http://itestra.com/wp-content/uploads/2017/08/04_itestra_straightening_spaghetti_code_with_refactoring.pdf|url-status=dead}}</ref> | |||
Spaghetti code can also describe an [[anti-pattern]] in which [[Object-oriented programming|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 (computer science)|polymorphism]].<ref name="Moha10">{{cite journal|last1=Moha|first1=N.|last2=Gueheneuc|first2=Y. G.|last3=Duchien|first3=L.|last4=Meur|first4=A. F. Le|title=DECOR: A Method for the Specification and Detection of Code and Design Smells|journal=IEEE Transactions on Software Engineering|date=January 2010|volume=36|issue=1|pages=20–36|doi=10.1109/TSE.2009.50|issn=0098-5589|citeseerx=10.1.1.156.1524|s2cid=14767901}}</ref> The presence of this form of spaghetti code can significantly reduce the comprehensibility of a system.<ref name="Abbes11">{{cite book|last1=Abbes|first1=M.|last2=Khomh|first2=F.|last3=Gueheneuc|first3=Y. G.|last4=Antoniol|first4=G.|title=2011 15th European Conference on Software Maintenance and Reengineering |chapter=An Empirical Study of the Impact of Two Antipatterns, Blob and Spaghetti Code, on Program Comprehension |date=2011|pages=181–190|doi=10.1109/CSMR.2011.24|isbn=978-1-61284-259-2|citeseerx=10.1.1.294.1685|s2cid=14152638}}</ref> | Spaghetti code can also describe an [[anti-pattern]] in which [[Object-oriented programming|object-oriented code]] is written in a procedural style,{{Dubious|I don't think spaghetti code refers to procedural style in object-oriented code|date=September 2025}} such as by creating classes whose methods are overly long and messy, or forsaking object-oriented concepts like [[Polymorphism (computer science)|polymorphism]].<ref name="Moha10">{{cite journal|last1=Moha|first1=N.|last2=Gueheneuc|first2=Y. G.|last3=Duchien|first3=L.|last4=Meur|first4=A. F. Le|title=DECOR: A Method for the Specification and Detection of Code and Design Smells|journal=IEEE Transactions on Software Engineering|date=January 2010|volume=36|issue=1|pages=20–36|doi=10.1109/TSE.2009.50|bibcode=2010ITSEn..36...20M |issn=0098-5589|citeseerx=10.1.1.156.1524|s2cid=14767901}}</ref> The presence of this form of spaghetti code can significantly reduce the comprehensibility of a system.<ref name="Abbes11">{{cite book|last1=Abbes|first1=M.|last2=Khomh|first2=F.|last3=Gueheneuc|first3=Y. G.|last4=Antoniol|first4=G.|title=2011 15th European Conference on Software Maintenance and Reengineering |chapter=An Empirical Study of the Impact of Two Antipatterns, Blob and Spaghetti Code, on Program Comprehension |date=2011|pages=181–190|doi=10.1109/CSMR.2011.24|isbn=978-1-61284-259-2|citeseerx=10.1.1.294.1685|s2cid=14152638}}</ref> | ||
== History == | ==History== | ||
It is | It is unclear when the phrase ''spaghetti code'' was coined; however, a reference appeared in 1972: "The principal motivation behind eliminating the goto statement is the hope that the resulting programs will not look like a bowl of spaghetti." by Martin Hopkins.<ref>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</ref> In the 1978 book ''A primer on disciplined programming using PL/I, PL/CS, and PL/CT'', [[Richard W. Conway|Richard Conway]] described programs that "have the same clean logical structure as a plate of spaghetti",<ref>{{cite book|title=A primer on disciplined programming using PL/I, PL/CS, and PL/CT|last=Conway|first=Richard|publisher=Winthrop Publishers|year=1978|isbn=978-0-87626-712-7}}</ref><!-- page 186 --> a phrase repeated in the 1979 book ''An Introduction to Programming'' he co-authored with [[David Gries]].<ref>{{cite book|title=An Introduction to Programming|last1=Conway|first1=Richard|last2=Gries|first2=David|edition=3rd|publisher=Little, Brown|year=1979|isbn=978-0-316-15414-7}}</ref><!-- page 158 --> 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]].<ref>{{cite journal|journal=IEEE Computer|title=A spiral model of software development and enhancement|last=Boehm|first=Barry W.|volume=21|issue=2|date=May 1988|pages=61–72|doi=10.1109/2.59|bibcode=1988Compr..21e..61B |s2cid=1781829}}</ref><!-- page 63 --> 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.<ref>{{cite book|title=Structured programming for the COBOL programmer: design, documentation, coding, testing|last=Noll|first=Paul|publisher=M. Murach & Associates|year=1977}}</ref><!-- page 15 --> | ||
In the ''Ada – Europe '93'' conference, [[Ada (programming language)|Ada]] was described as forcing the programmer to "produce understandable, instead of spaghetti code", because of its restrictive exception propagation mechanism.<ref>{{cite conference|conference=Ada – Europe '93 (Proceedings)|book-title=Lecture Notes in Computer Science|title=Use and abuse of exceptions — 12 guidelines for proper exception handling|last=Schwille|first=Jürgen |series=Lecture Notes in Computer Science |volume=688|year=1993|publisher=Springer Berlin Heidelberg|pages=142–152|doi=10.1007/3-540-56802-6_12|isbn=978-3-540-56802-5 }}</ref> | In the ''Ada – Europe '93'' conference, [[Ada (programming language)|Ada]] was described as forcing the programmer to "produce understandable, instead of spaghetti code", because of its restrictive exception propagation mechanism.<ref>{{cite conference|conference=Ada – Europe '93 (Proceedings)|book-title=Lecture Notes in Computer Science|title=Use and abuse of exceptions — 12 guidelines for proper exception handling|last=Schwille|first=Jürgen |series=Lecture Notes in Computer Science |volume=688|year=1993|publisher=Springer Berlin Heidelberg|pages=142–152|doi=10.1007/3-540-56802-6_12|isbn=978-3-540-56802-5 }}</ref> | ||
In a 1980 publication by the [[National Institute of Standards and Technology|United States National Bureau of Standards]], the phrase ''spaghetti program'' was used to describe older programs having "fragmented and scattered files".<ref>{{cite book|title=ASTM special technical publication|issue=500–565|author=United States National Bureau of Standards|publisher=United States Government Printing Office|year=1980}}</ref><!-- page 15 --> | |||
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".<ref>{{cite journal|journal=The Michigan Technic|title=BASICally speaking...FORTRAN bytes!!|author=MTSBS{{clarify|date=April 2015}}| | 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".<ref>{{cite journal|journal=The Michigan Technic|title=BASICally speaking...FORTRAN bytes!!|author=MTSBS{{clarify|date=April 2015}}| | ||
| Line 24: | Line 27: | ||
}} | }} | ||
== | ==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 {{code |GOTO}} for looping and lack of indentation leads to less than clear logic flow. | ||
<syntaxhighlight lang="basic"> | <syntaxhighlight lang="basic"> | ||
1 i=0 | 1 i=0 | ||
2 i=i+1 | 2 i=i+1 | ||
3 PRINT | 3 PRINT i | ||
4 IF i>=100 THEN GOTO 6 | 4 IF i>=100 THEN GOTO 6 | ||
5 GOTO 2 | 5 GOTO 2 | ||
6 | 6 END | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The following code produces the same result, but uses a structured [[Loop (computing)|loop statement]] and indentation to improve readability. | |||
<syntaxhighlight lang="basic"> | <syntaxhighlight lang="basic"> | ||
1 FOR i=1 TO 100 | 1 FOR i=1 TO 100 | ||
2 PRINT | 2 PRINT i | ||
3 NEXT i | 3 NEXT i | ||
4 | 4 END | ||
</syntaxhighlight> | </syntaxhighlight> | ||
The | ===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. | |||
<syntaxhighlight lang="basic"> | <syntaxhighlight lang="basic"> | ||
INPUT "How many numbers should be sorted? "; T | INPUT "How many numbers should be sorted? "; T | ||
| Line 88: | Line 82: | ||
NEXT i | NEXT i | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==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 (employment)|turnover]] and [[software entropy]]. The term was popularized by Brian Foote and Joseph Yoder although they credit Brian Marick for coining the term.<ref>{{cite web |last1=Foote |first1=Brian |last2=Yoder |first2=Joseph |url=http://www.laputan.org/mud/mud.html#BigBallOfMud |title=Big Ball of Mud |website=laputan.org |date=26 June 1999 |accessdate=14 April 2019}}</ref> | |||
{{bquote| | |||
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 code{{anchor |lasagna-code}}: [[Lasagna]] code has [[Architectural layer |layers]] that are so intertwined that making a change in one layer necessitates changing other layers too.<ref name="Latchezar18">{{cite journal|last1=Tomov|first1=Latchezar|last2=Ivanova|first2=Valentina|title=Teaching Good Practices In Software Engineering by Counterexamples|journal=Computer Science and Education in Computer Science|date=October 2014|issue=1|pages=397–405|url=https://www.researchgate.net/publication/301298530|access-date=5 March 2018}}</ref> | |||
; Ravioli code{{anchor |ravioli-code}}: [[Ravioli]] code comprises well-structured [[Class (programming)|classes]] that are easy to understand in isolation but in combination result in less than clear system design.<ref name="Troyer91">{{cite conference|last1=De Troyer|first1=O.|title=The OO-binary relationship model : A truly object oriented conceptual model|conference=Advanced Information Systems Engineering|volume=498|date=13 May 1991|pages=561–578|doi=10.1007/3-540-54059-8_104|language=en|series=Notes on Numerical Fluid Mechanics and Multidisciplinary Design|isbn=978-3-319-98176-5|s2cid=10894568 |editor-last=Andersen |editor-first=Rudolf|editor2-last=Bubenko |editor2-first=Janis A. |editor3-last=Sølvberg |editor3-first=Arne|url=https://pure.uvt.nl/ws/files/5292185/DTO5613576.pdf}}</ref> | |||
== See also == | == See also == | ||
{{Portal|Computer programming|Engineering}} | {{Portal|Computer programming|Engineering}} | ||
* | * ''{{Annotated link |The Elements of Programming Style}}'' | ||
* | * {{Annotated link |International Obfuscated C Code Contest}} | ||
* | * {{Annotated link |Technical debt}} | ||
== References == | == References == | ||
Latest revision as of 02:51, 18 October 2025
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.[1] 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.[2] 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.[3]
Spaghetti code can also describe an anti-pattern in which object-oriented code is written in a procedural style,Script error: No such module "Unsubst". such as by creating classes whose methods are overly long and messy, or forsaking object-oriented concepts like polymorphism.[4] The presence of this form of spaghetti code can significantly reduce the comprehensibility of a system.[5]
History
It is unclear when the phrase spaghetti code was coined; however, a reference appeared in 1972: "The principal motivation behind eliminating the goto statement is the hope that the resulting programs will not look like a bowl of spaghetti." by Martin Hopkins.[6] 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",[7] a phrase repeated in the 1979 book An Introduction to Programming he co-authored with David Gries.[8] 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.[9] 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.[10]
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.[11]
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".[12]
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".[13]
Richard Hamming described in his lectures[14] the etymology of the term in the context of early programming in binary codes:
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 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 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. 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 mudScript error: No such module "anchor".
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.[15]
Inspired by the popularity of spaghetti code, other pasta-oriented terms that describe the structural nature of code include:
- Lasagna codeScript error: No such module "anchor".
- Lasagna code has layers that are so intertwined that making a change in one layer necessitates changing other layers too.[16]
- Ravioli codeScript error: No such module "anchor".
- Ravioli code comprises well-structured classes that are easy to understand in isolation but in combination result in less than clear system design.[17]
See also
Script error: No such module "Portal".
References
External links
- Go To Statement Considered Harmful. The classic repudiation of spaghetti code by Edsger Dijkstra
- We don't know where to GOTO if we don't know where we've COME FROM by R. Lawrence Clark from DATAMATION, December, 1973 Template:Webarchive
- Refactoring Java spaghetti code into Java bento code separating out a bowl full of code from one class into seven classes
- Objects and Frameworks – Taking a Step Back by Brian Rinaldi
- Programming Pasta - Spaghetti, Lasagna, Ravioli and Macaroni Code Template:Webarchive
- Pasta Theory of Programming
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "Citation/CS1".
- ↑ Script error: No such module "Citation/CS1".
- ↑ Script error: No such module "Citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ 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
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "Citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "Citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "Citation/CS1".
- ↑ Script error: No such module "citation/CS1".