Spaghetti code
This article relies largely or entirely on a single source. (June 2009) |
![](http://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Spaghetti.jpg/175px-Spaghetti.jpg)
Spaghetti code is a pejorative term for source code that 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.
Examples
Below is 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 used to differentiate the various actions performed by the code, 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 = 0
20 i = i + 1
30 PRINT i; " squared = "; i * i
40 IF i >= 10 THEN GOTO 60
50 GOTO 20
60 PRINT "Program Completed."
70 END
Here is the same code written in a structured programming style:
10 FOR i = 1 TO 10
20 PRINT i; " squared = "; i * i
30 NEXT i
40 PRINT "Program Completed."
50 END
The 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 exist, but are often poorly understood by inexperienced programmers. 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 mechanisms
which 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 ad hoc global variables and the action at a distance 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.
Other related terms
The term "spaghetti code" has inspired the coinage of other terms that similarly compare program structure to styles of pasta.
Ravioli code
![](http://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Ravioli.jpg/175px-Ravioli.jpg)
Ravioli code is a type of computer program structure, characterized by an excessive number of very small and (ideally) loosely coupled software components. The term stems from the analogy of ravioli (small pasta pouches containing cheese, meat, or vegetables) to modules (which ideally are encapsulated, consisting of both code and data). While generally desirable from a coupling and cohesion perspective, overzealous separation and encapsulation of code can bloat call stacks and make navigation through the code for maintenance purposes more difficult.
Lasagna code
![](http://upload.wikimedia.org/wikipedia/commons/thumb/6/69/Lasagna_NIH.jpg/175px-Lasagna_NIH.jpg)
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 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.[1]
A similar layering may be seen in communication stacks, where a protocol (such as the OSI model) is divided into layers (in this case 7), with each layer performing a limited and well defined function and communicating with other layers using specific and standardized methods. Such a design eases the evolutionary improvement of the entire stack through layer-specific improvements.
Again, while loosely coupled layering is generally desirable in a program's architecture because it makes objects at each layer more interchangeable with existing or possible future implementations, other types of changes to the code will actually increase in complexity as more layers are added and so an extensively layered architecture can be seen as an anti-pattern as well. Adding a new field to a UI view, for example, requires changing every object at every layer in the architecture that is required to have knowledge about this new field (generally the view itself, any underlying controller/presenter class, data transfer objects, SOA layers, data access objects or mappings, and the database schema itself). A quote usually attributed either to David Wheeler or Butler Lampson reads, "There is no problem in computer science that cannot be solved by adding another layer of indirection, except having too many layers of indirection".
Spaghetti with meatballs
The term "spaghetti with meatballs" is a pejorative term used in computer science to describe loosely constructed object-oriented programming (OOP) that remains dependent on procedural code. It may be the result of a system whose development has included a long life cycle, language constraints, micro-optimization theatre, or a lack of coherent coding standards.
In some languages, OOP features are available only in later specifications. Notable examples of this include Visual Basic, and PHP. Other languages, like C, rely on function pointers to simulate OOP — still requiring the underlying procedural code to which they point.
Using OOP does not necessarily prevent a class's code from becoming spaghetti-like. In this parlance, "spaghetti" describes twisted, tangled and unstructured code, while "meatballs" denotes the use of class structures (i.e. objects).
See also
- International Obfuscated C Code Contest A competition to produce pleasingly obscure C code.
References
This article is based on material taken from the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.
- ^ Celko, Joe (January 1997). "The Future of SQL Programming". DBMS Online. Retrieved 2008-09-10.
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
- 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