Directory

Encyclopedia

NodeWorks
                              ENCYCLOPEDIA

Link Checker

Home
Encyclopedia : S : SP : SPA :

Spaghetti code

 

Spaghetti code

looks twisted and tangled, which is where the name for spaghetti code comes from.

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.

Examples


Below is an example of what would be considered spaghetti code in BASIC. The program prints the numbers 0 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.

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 * 2 100 goto 30 Here is the same code written in a procedural style:

dim i for i = 0 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 predicable 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

  • Structured programming

    References

    External links

  • Go To Statement Considered Harmful. The classic repudiation of spaghetti code by Edsger Dijkstra.
  • The Daily WTF - Curious Perversions In Information Technology



  • NodeWorks boosts web surfing!
    Page Returned in 0.884 seconds - HTML Compressed 68.4%

    This article is from Wikipedia. All text is available
    under the terms of the GNU Free Documentation License.
     GNU Free Documentation License
    © 2008 Chamas Enterprises Inc.