Spaghetti code
|
Spaghetti-from-above.jpg
Spaghetti code is a pejorative term for a computer program code with 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.
Spaghetti code is an example of an anti-pattern.
Contents |
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 dim i 20 i = 0 30 i = i + 1 40 if i <> 10 then goto 90 50 if i = 10 then goto 70 60 goto 30 70 print "Program Completed." 80 end 90 print i & " squared = " & i * i 100 goto 30
Here is the same code written in a procedural style:
dim i for i = 1 to 10 print i & " squared = " & square(i) next print "Program Completed." function square(i) square = i * i end function
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 where 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 language
The many forms of assembly language (and also the underlying machine code) insist upon spaghetti code. This is because these are low level programming languages where structured control flow statements such as for loops and while loop are prohibited by their nature.
While many assembly languages have a function stack, and apparent function calls, these are just a thin wrapper for the goto statement. They store the original location of the program counter in order to return to it later.
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.
See also
References
- This article was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.
External links
- Go To Statement Considered Harmful (http://www.acm.org/classics/oct95/). The classic repudiation of spaghetti code by Edsger Dijkstra.
- The Daily WTF - Curious Perversions In Information Technology (http://thedailywtf.com/)
- 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://www.fortran.com/fortran/come_from.html)de:Spagetticode