Jump to content

Spaghetti code

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 24.163.85.3 (talk) at 21:45, 30 May 2007. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A plate of spaghetti looks twisted and tangled, which is where the name for spaghetti code comes from.
Cooked spaghetti with a tomato sauce to make the spaghetti more palatable in the same way that a programmer may wish to cover his code with the sauce, although perhaps this is taking the analogy too far.
Spaghetti during cooking. Before a programmer is complete the code may be clear and easy to understand as is this uncooked spaghetti, but by the time he is finished, oh boy, you just wait. It's a mess.
Spaghetti served with crayfish. This is clearly taking the analogy far beyond any reasonable bounds.

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. Also called kangaroo code because such code has so many jumps in it.

It is also used as in a pejorative sense to imply that a given work is difficult for the user of the term to follow. There are no known objective consensus metrics to measure "Spaghetti-ness". What may be easy for one person to follow may not be easy for another.

Spaghetti code is an example of an anti-pattern.

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 = 0
20 i = i + 1
30 print i; " squared = "; i * i
40 if i < 10 then goto 20
50 print "Program Completed."
60 end

Here is the same code written in a structured programming style:

for i = 1 to 10
    print i; " squared = "; i * i
next i
print "Program Completed."

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 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 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 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 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.

See also

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.