Jump to content

Spaghetti code: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 1: Line 1:
'''Spaghetti code''' is a pejorative term for a [[computer]] [[program]] code with a complex and tangled [[control structure]], especially one using many [[GOTO]]s, [[exception]]s, or other "unstructured" [[branch]]ing constructs.
'''Spaghetti code''' is a pejorative term for a [[computer]] [[program]] code with a complex and tangled [[control structure]], especially one using many [[GOTO]]s, [[exception]]s, threads, or other "unstructured" [[branch]]ing 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 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.

Revision as of 23:16, 26 October 2004

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.

Here's an actual example of spaghetti code taken from a production codebase:

 if (aAguId == null || new Integer("0").equals(aAguId))
     ...

The author could have just written:

 if (aAguId == null || 0 == aAguId.intValue())
     ...

which not only does the same thing, it is easier to read, uses less memory, and requires fewer instructions. True spaghetti code, of course, can get much more convoluted, but this simple example displays the fundamental spaghetti principles: make your code more convoluted and complex than it needs to be and make people think hard to understand it.

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.