Spaghetti code

Spaghetti code

Spaghetti code is a pejorative term for source code which has a complex and tangled control structure, especially one using many GOTOs, exceptions, threads, or other "unstructured" branching constructs. It is named such because program flow tends to look like a bowl of spaghetti, i.e. twisted and tangled. Spaghetti code can be caused by several factors, including inexperienced programmers and a complex program which has been continuously modified over a long life cycle. Structured programming greatly decreased the incidence of spaghetti code, and is widely regarded as one of the most important advances in programming history.

Examples

Below is an example of what would be considered a trivial example of spaghetti code in BASIC. The program prints the numbers 1 to 10 to the screen along with their square. Notice that indentation is not needed and that the program's GOTO statements create a reliance on line numbers. Also observe the unpredictable way the flow of execution jumps from one area to another. Real-world occurrences of spaghetti code are more complex and can add greatly to a program's maintenance costs.10 i = 020 i = i + 130 PRINT i; " squared = "; i * i40 IF i < 10 THEN GOTO 2050 PRINT "Program Completed."60 ENDHere is the same code written in a structured programming style:FOR i = 1 TO 10 PRINT i; " squared = "; i * iNEXT iPRINT "Program Completed."ENDThe program jumps from one area to another but this jumping is predictable and formal. This is because using for loops and functions are standard ways of providing 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.

Assembly and script languages

When using the many forms of assembly language (and also the underlying machine code) the danger of writing spaghetti code is especially great.This is because they are low-level programming languages where equivalents for structured control flow statements such as for loops and while loops seldom exist.Many scripting languages have the same deficiencies: this applies to the batch scripting language of DOS and DCL on VMS.

Nonetheless, adopting the same discipline as in structured programming can greatly improve the readability and maintainability of such code.This may take the form of conventions limiting the use of goto to correspond to the standard structures, or use of a set of assembler macros for if and loop constructs.Most assembly languages also provide a function stack, and function call mechanismswhich can be used to gain the advantages of procedural programming. Macros can again be used to support a standardized form of parameter passing, to avoid the accumulate and fire anti-pattern.

Programs written in higher-level languages with high-level constructs such as "for loops" (as in the second example above) are often compiled into assembly or machine code. When this process occurs, the high-level constructs are translated into low-level "spaghetti code" which may resemble the first example above in terms of control flow. But because compilers must be faithful to high-level constructs in the source code, the problems that plague relatively unstructured languages like BASIC do not haunt higher-level languages. It does, however, mean that debugging even mildly optimized code with a source-level debugger can be surprisingly confusing.

Ravioli code

Ravioli code is a type of computer program structure, characterized by a number of small and (ideally) loosely-coupled software components. The term is in comparison with spaghetti code, comparing program structure to pasta; with ravioli (small pasta pouches containing cheese, meat, or vegetables) being analogous to objects (which ideally are encapsulated modules consisting of both code and data).

Lasagna code

Lasagna code is a type of program structure, characterized by several well-defined and separable "layers", where each layer of code accesses services in the layers below through well-defined interfaces. The term is in comparison with spaghetti code, comparing program structure to pasta. The analogy stems from the layered structure of a plate of lasagna, where different ingredients (meat, sauce, vegetables, or cheese) are each separated by strips of pasta.

One common instance of lasagna code occurs at the interface between different subsystems, such as between web application code, business logic, and a relational database. Another common programming technique, alternate hard and soft layers (use of different programming languages at different levels of the program architecture), tends to produce lasagna code. In general, client-server applications are frequently lasagna code, with well-defined interfaces between client and server.

Lasagna code generally enforces encapsulation between the different "layers", as the subsystems in question may have no means of communication other than through a well-defined mechanism, such as Structured Query Language, a foreign function interface, or Remote Procedure Call. However, individual layers in the system may be highly unstructured or disorganized.

The term was coined by database guru Joe Celko in 1982. [cite web
url=http://www.dbmsmag.com/9701d06.html
title=The Future of SQL Programming
last=Celko
first=Joe
publisher=DBMS Online
date=January 1997
accessdate=2008-09-10
]

ee also

* Structured programming Coding without goto, e.g. using only loop, sequence and alternate structures.
* International Obfuscated C Code Contest A competition to produce pleasingly obscure C code.
* Spaghetti with Meatballs (programming) Twisted, tangled and unstructured code (spaghetti), with objects (meatballs) thrown in for good measure.

References

External links

* [http://www.acm.org/classics/oct95/ Go To Statement Considered Harmful] . The classic repudiation of spaghetti code by Edsger Dijkstra.
* [http://www.fortran.com/fortran/come_from.html "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]
* [http://yost.com/computers/java/java-spaghetti/ Refactoring Java spaghetti code into Java bento code] separating out a bowl full of code from one class into seven classes


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • spaghetti code — noun Unstructured or poorly structured program source code; especially code with many GOTO statements (or their equivalent). See Also: lasagna code, ravioli code …   Wiktionary

  • Spaghetti (disambiguation) — Spaghetti may refer to:* Spaghetti, a long thin form of pasta * Spaghetti (film), a 1916 film starring Oliver Hardy * Spaghetti tree, a famous BBC 1957 parody stating spaghetti grew on trees * Spaghetti squash * Spaghetti code, a term used to… …   Wikipedia

  • Spaghetti — is a long, thin, cylindrical pasta of Italian origin. [spaghetti. Dictionary.com. Dictionary.com Unabridged (v 1.1). Random House, Inc. http://dictionary.reference.com/browse/spaghetti (accessed: June 03, 2008).] A variety of pasta dishes are… …   Wikipedia

  • Spaghetti with meatballs (programming) — In computer science, spaghetti with meatballs is a pejorative term used to describe a loosely constructed Object oriented system. Much like traditional spaghetti code, the term spaghetti with meatballs is a description of poor coding, usually by… …   Wikipedia

  • spaghetti — ● n. péj. ►ARGOT Dans l expression code spaghetti , code à la structure complexe et entremêlées, due à une trop grande utilisation de goto et d exceptions. Le code kangourou existe aussi, car il contient beaucoup de sauts. Le développement al… …   Dictionnaire d'informatique francophone

  • Code Impénétrable — Le code impénétrable d un programme informatique est un code dont la compréhension est très difficile pour un humain tout en restant parfaitement compilable par un ordinateur. Appelé aussi assombrissement ou obfuscation, cette technique de… …   Wikipédia en Français

  • Code impenetrable — Code impénétrable Le code impénétrable d un programme informatique est un code dont la compréhension est très difficile pour un humain tout en restant parfaitement compilable par un ordinateur. Appelé aussi assombrissement ou obfuscation, cette… …   Wikipédia en Français

  • Code impénetrable — Code impénétrable Le code impénétrable d un programme informatique est un code dont la compréhension est très difficile pour un humain tout en restant parfaitement compilable par un ordinateur. Appelé aussi assombrissement ou obfuscation, cette… …   Wikipédia en Français

  • Code Red (DVD Company) — Code Red Type DVD Distribution Company Industry Home video Products DVD Website …   Wikipedia

  • Code spaghetti — Programmation spaghetti La programmation spaghetti ou code spaghetti sont des termes péjoratifs utilisés en programmation. Ils qualifient des programmes dont le code n est pas clair et qui font un usage excessif de sauts inconditionnels (voir… …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”