Spaghetti code

Spaghetti codeis apejorativephrase for difficult-to-maintainand unstructuredcomputersource code. Code being developed with poor structure can be due to any of several factors, such as volatile project requirements, lack ofprogramming stylerules, andsoftware engineerswith insufficient ability or experience.
Code that overusesGOTOstatements rather thanstructured programmingconstructs, resulting in convoluted and unmaintainable programs, is often called spaghetti code.Such code has a complex and tangledcontrol structure, resulting in a program flow that is conceptually like abowl of spaghetti, twisted and tangled.
In a 1980 publication by theUnited States National Bureau of Standards, the phrasespaghetti programwas used to describe older programs having "fragmented and scattered files".
Spaghetti code can also describe ananti-patternin whichobject-oriented codeis written in a procedural style, such as by creating classes whose methods are overly long and messy, or forsaking object-oriented concepts likepolymorphism.The presence of this form of spaghetti code can significantly reduce the comprehensibility of a system.
It is not clear when the phrase spaghetti code came into common usage; however, several references appeared in 1977 includingMacaroni is Better Than SpaghettibyGuy Steele.In the 1978 bookA primer on disciplined programming using PL/I, PL/CS, and PL/CT,Richard Conwaydescribed programs that "have the same clean logical structure as a plate of spaghetti",a phrase repeated in the 1979 bookAn Introduction to Programminghe co-authored withDavid Gries.In the 1988 paperA spiral model of software development and enhancement, the term is used to describe the older practice of thecode and fix model, which lacked planning and eventually led to the development of thewaterfall model.In the 1979 bookStructured programming for the COBOL programmer, author Paul Noll uses the phrasesspaghetti codeandrat's nestas synonyms to describe poorly structured source code.
In theAda – Europe '93conference,Adawas described as forcing the programmer to "produce understandable, instead of spaghetti code", because of its restrictive exception propagation mechanism.
In a 1981 computer languages spoof inThe Michigan Technictitled "BASICally speaking...FORTRAN bytes!!", the author describedFORTRANstating that "it consists entirely of spaghetti code".
Richard Hammingdescribed in his lecturesthe etymology of the term in the context of early programming in binary codes:
Raviolicode is a term specific toobject-oriented programming. It describes code that comprises well-structuredclassesthat are easy to understand in isolation, but difficult to understand as a whole.
Lasagnacode refers to code whose layers are so complicated and intertwined that making a change in one layer would necessitate changes in all other layers.
Here follows what would be considered a trivial example of spaghetti code inBASIC. The program prints each of the numbers 1 to 100 to the screen along with its square. Indentation is not used to differentiate the various actions performed by the code, and the program'sGOTOstatements create a reliance online numbers. The flow of execution from one area to another is harder to predict. Real-world occurrences of spaghetti code are more complex and can add greatly to a program's maintenance costs.
Here is the same code written in astructured programmingstyle:
The program jumps from one area to another, but this jumping is formal and more easily predictable, becausefor loopsandfunctionsprovideflow controlwhereas thegotostatement 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.
Here is another example of Spaghetti code with embedded GOTO statements.