ArticleslgStudy

computer science

Spaghetti code

Spaghetti code is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Spaghetti code rather than just read about it. In short: Spaghetti code is computer source code with convoluted control flow, with statements directing program execution without structure, instead resembling cooked spaghetti, twisted and tangled. The code tends to be hard to understand or maintain.

Spaghetti code — main illustration
Spaghetti code — illustration

Key takeaways

  • Spaghetti code belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Spaghetti code to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Spaghetti code from memory before moving on to harder problems.

Reference excerpt

Spaghetti code is computer source code with convoluted control flow, with statements directing program execution without structure, instead resembling cooked spaghetti, twisted and tangled. The code tends to be hard to understand or 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". 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 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.

The following code produces the same result, but uses a structured loop statement and indentation to improve readability.

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.

Related

Big ball of 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.

Pasta-related Inspired by the popularity of spaghetti code, other pasta-oriented terms that describe the structural nature of code include:

Lasagna code Lasagna code has layers that are so intertwined that making a change in one layer necessitates changing other layers too. Ravioli code Ravioli code comprises well-structured classes that are easy to understand in isolation but in combination result in less than clear system design.

See also

Code smell – Characteristic of source code that hints at a quality problem The Elements of Programming Style – 1978 book by Brian W. Kernighan and P. J. Plauger International Obfuscated C Code Contest – Contest to produce obscure computer code Software brittleness – Description of how difficult software is to modify Software maintenance – Modification of software after delivery SQALE Technical debt – Cause of higher cost of maintaining a lower quality system

References

… excerpt ends here. Continue reading the full article.

Illustrations

Spaghetti code: Cooked spaghetti, characteristically tangled
Cooked spaghetti, characteristically tangled

Worked examples

Example 1 — a first encounter with Spaghetti code

Start with the simplest possible case. Write down what Spaghetti code claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Spaghetti code before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Spaghetti code ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Spaghetti code

In research
Spaghetti code appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Spaghetti code in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Spaghetti code is common in secondary-school and first-year university syllabi. It links to neighbouring topics Anti-patterns, Metaphors referring to spaghetti, Pejorative terms related to technology, so understanding it makes those chapters shorter.
In everyday life
Look for Spaghetti code outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Spaghetti code” →

How to study Spaghetti code in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Spaghetti code means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Spaghetti code out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Spaghetti code in simple terms?

Spaghetti code is computer source code with convoluted control flow, with statements directing program execution without structure, instead resembling cooked spaghetti, twisted and tangled. The code tends to be hard to understand or maintain.

Why does Spaghetti code matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Spaghetti code?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Spaghetti code.

Tags

  • Anti-patterns
  • Metaphors referring to spaghetti
  • Pejorative terms related to technology
  • Software engineering folklore

Keep exploring