Spaghetti code is a
pejorative
A pejorative or slur is a word or grammatical form expressing a negative or a disrespectful connotation, a low opinion, or a lack of respect toward someone or something. It is also used to express criticism, hostility, or disregard. Sometimes, a ...
phrase for unstructured and difficult-to-
maintain source code
In computing, source code, or simply code, is any collection of code, with or without comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the wo ...
. Spaghetti code can be caused by several factors, such as volatile
project
A project is any undertaking, carried out individually or collaboratively and possibly involving research or design, that is carefully planned to achieve a particular goal.
An alternative view sees a project managerially as a sequence of even ...
requirements, lack of
programming style
Programming style, also known as code style, is a set of rules or guidelines used when writing the source code for a computer program. It is often claimed that following a particular programming style will help programmers read and understand sour ...
rules, and
software engineer
Software engineering is a systematic engineering approach to software development.
A software engineer is a person who applies the principles of software engineering to design, develop, maintain, test, and evaluate computer software. The term ''p ...
s with insufficient ability or experience.
Meaning
Code that overuses
GOTO
GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function ca ...
statements rather than
structured programming
Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection ( if/then/else) and repetition ( ...
constructs, resulting in convoluted and unmaintainable programs, is often called spaghetti code.
Such code has a complex and tangled
control structure
In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an ''imp ...
, resulting in a program flow that is conceptually like a
bowl of spaghetti, twisted and tangled.
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".
Spaghetti code can also describe an
anti-pattern
An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
in which
object-oriented code
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
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 not clear when the phrase spaghetti code came into common usage; however, several references appeared in 1977 including ''Macaroni is Better Than Spaghetti'' by
Guy Steele
Guy Lewis Steele Jr. (; born October 2, 1954) is an American computer scientist who has played an important role in designing and documenting several computer programming languages and technical standards.
Biography
Steele was born in Missouri ...
. In the 1978 book ''A primer on disciplined programming using PL/I, PL/CS, and PL/CT'',
Richard Conway used the term to describe types of 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
David Gries (born April 26, 1939 in Flushing, Queens, New York) is an American computer scientist at Cornell University, United States mainly known for his books ''The Science of Programming'' (1981) and ''A Logical Approach to Discrete Math'' ( ...
. 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
The waterfall model is a breakdown of project activities into linear sequential phases, meaning they are passed down onto each other, where each phase depends on the deliverables of the previous one and corresponds to a specialization of tasks. ...
. 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
Ada may refer to:
Places
Africa
* Ada Foah, a town in Ghana
* Ada (Ghana parliament constituency)
* Ada, Osun, a town in Nigeria
Asia
* Ada, Urmia, a village in West Azerbaijan Province, Iran
* Ada, Karaman, a village in Karaman Province, ...
was described as forcing the programmer to "produce understandable, instead of spaghetti code", because of its restrictive exception propagation mechanism.
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".
Richard Hamming
Richard Wesley Hamming (February 11, 1915 – January 7, 1998) was an American mathematician whose work had many implications for computer engineering and telecommunications. His contributions include the Hamming code (which makes use of a Ha ...
described in his lectures
the etymology of the term in the context of early programming in binary codes:
Related phrases
Ravioli code
Ravioli
Ravioli (; singular: ''raviolo'', ) are a type of pasta comprising a filling enveloped in thin pasta dough. Usually served in broth or with a sauce, they originated as a traditional food in Italian cuisine. Ravioli are commonly square, though o ...
code is a term specific to
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
. It describes code that comprises well-structured
classes that are easy to understand in isolation, but difficult to understand as a whole.
Lasagna code
Lasagna
Lasagna (, also , also known as lasagne, ) is a type of pasta, possibly one of the oldest types, made of very wide, flat sheets. Either term can also refer to an Italian dish made of stacked layers of lasagna alternating with fillings such as ...
code refers to code whose layers are so complicated and intertwined that making a change in one layer would necessitate changes in all other layers.
Examples
Here follows what would be considered a trivial example of spaghetti code in
BASIC
BASIC (Beginners' All-purpose Symbolic Instruction Code) is a family of general-purpose, high-level programming languages designed for ease of use. The original version was created by John G. Kemeny and Thomas E. Kurtz at Dartmouth College ...
. The program prints each of the numbers 1 to 100 to the screen along with its square. Indentation is not used to differentiate the various actions performed by the code, and the program's
GOTO
GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function ca ...
statements create a reliance on
line number
In computing, a line number is a method used to specify a particular sequence of characters in a text file. The most common method of assigning numbers to lines is to assign every line a unique number, starting at 1 for the first line, and increm ...
s. The flow of execution from one area to another is harder to predict. Real-world occurrences of spaghetti code are more complex and can add greatly to a program's maintenance costs.
1 i=0;
2 i=i+1;
3 PRINT i; "squared=";i*i;
4 IF i>=100 THEN GOTO 6;
5 GOTO 2;
6 PRINT "Program Completed.";
7 END
Here is the same code written in a
structured programming
Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection ( if/then/else) and repetition ( ...
style:
1 FOR i=1 TO 100
2 PRINT i;"squared=";i*i
3 NEXT i
4 PRINT "Program Completed."
5 END
The program jumps from one area to another, but this jumping is formal and more easily predictable, because
for loop
In computer science a for-loop or for loop is a control flow statement for specifying iteration. Specifically, a for loop functions by running a section of code repeatedly until a certain condition has been satisfied.
For-loops have two part ...
s and
functions provide
flow control whereas the ''goto'' statement encourages arbitrary flow control. Though this example is small, real world programs are composed of many lines of code and are difficult to maintain when written in a spaghetti code fashion.
Here is another example of Spaghetti code with embedded GOTO statements.
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
See also
*
Big ball of mud
An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
, a piece of software with no perceivable architecture
*
International Obfuscated C Code Contest
The International Obfuscated C Code Contest (abbreviated IOCCC) is a computer programming contest for the most creatively obfuscated C code. Held annually, it is described as "celebrating 'ssyntactical opaqueness". The winning code for the 27t ...
, a competition to produce pleasingly obscure C code
*
Technical debt
In software development, technical debt (also known as design debt or code debt) is the implied cost of additional rework caused by choosing an easy (limited) solution now instead of using a better approach that would take longer.
Analogous with ...
* ''
The Elements of Programming Style
''The Elements of Programming Style'', by Brian W. Kernighan and P. J. Plauger, is a study of programming style, advocating the notion that computer programs should be written not only to satisfy the compiler or personal programming "style", bu ...
''
*
Write-only language
In computer humor, a write-only language is a pejorative term for a programming language alleged to have syntax or semantics sufficiently dense and bizarre that any routine of significant size is too difficult to understand by other programmers an ...
, a language with such bizarre syntax that resulting code is incomprehensible
References
External links
Go To Statement Considered Harmful The classic repudiation of spaghetti code by
Edsger Dijkstra
Edsger Wybe Dijkstra ( ; ; 11 May 1930 – 6 August 2002) was a Dutch computer scientist, programmer, software engineer, systems scientist, and science essayist. He received the 1972 Turing Award for fundamental contributions to developing progra ...
''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{{Webarchive, url=https://web.archive.org/web/20180716171336/http://www.fortran.com/fortran/come_from.html , date=2018-07-16
Refactoring Java spaghetti code into Java bento codeseparating out a bowl full of code from one class into seven classes
Objects and Frameworks – Taking a Step Backby Brian Rinaldi
Programming Pasta - Spaghetti, Lasagna, Ravioli and Macaroni CodePasta Theory of ProgrammingSpaghetti Code: Detangling Life and Work with Programmer Wisdom (English Edition)
Anti-patterns
Articles with example BASIC code
Software engineering folklore
Pejorative terms related to technology
Metaphors referring to spaghetti