Spaghetti code explained

Spaghetti code is a pejorative phrase for difficult-to-maintain and unstructured computer source code. Code being developed with poor structure can be due to any of several factors, such as volatile project requirements, lack of programming style rules, and software engineers with insufficient ability or experience.[1]

Meaning

Code that overuses GOTO statements rather than structured programming constructs, resulting in convoluted and unmaintainable programs, is often called spaghetti code.[2] Such code has a complex and tangled control structure, resulting in a program flow that is conceptually like a bowl of spaghetti, twisted and tangled.[3]

In a 1980 publication by the United States National Bureau of Standards, the phrase spaghetti program was used to describe older programs having "fragmented and scattered files".[4]

Spaghetti code can also describe an anti-pattern in which object-oriented code is written in a procedural style, such as by creating classes whose methods are overly long and messy, or forsaking object-oriented concepts like polymorphism.[5] The presence of this form of spaghetti code can significantly reduce the comprehensibility of a system.[6]

History

It is not clear when the phrase spaghetti code came into common usage; however, several references appeared in 1977 including Macaroni is Better Than Spaghetti by Guy Steele.[7] In the 1978 book A primer on disciplined programming using PL/I, PL/CS, and PL/CT, Richard Conway described programs that "have the same clean logical structure as a plate of spaghetti",[8] a phrase repeated in the 1979 book An Introduction to Programming he co-authored with David Gries.[9] In the 1988 paper A spiral model of software development and enhancement, the term is used to describe the older practice of the code and fix model, which lacked planning and eventually led to the development of the waterfall model.[10] In the 1979 book Structured programming for the COBOL programmer, author Paul Noll uses the phrases spaghetti code and rat's nest as synonyms to describe poorly structured source code.[11]

In the Ada – Europe '93 conference, Ada was described as forcing the programmer to "produce understandable, instead of spaghetti code", because of its restrictive exception propagation mechanism.[12]

In a 1981 computer languages spoof in The Michigan Technic titled "BASICally speaking...FORTRAN bytes!!", the author described FORTRAN stating that "it consists entirely of spaghetti code".[13]

Richard Hamming described in his lectures[14] the etymology of the term in the context of early programming in binary codes:

Related phrases

Ravioli code

Ravioli code is a term specific to object-oriented programming. It describes code that comprises well-structured classes that are easy to understand in isolation, but difficult to understand as a whole.[15]

Lasagna code

See also: Architectural layer and Layer (object-oriented design). Lasagna code refers to code whose layers are so complicated and intertwined that making a change in one layer would necessitate changes in all other layers.[16]

Examples

Here follows what would be considered a trivial example of spaghetti code in BASIC. 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's [[Goto|GOTO]] statements create a reliance on line 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.

1 i=02 i=i+13 PRINT i;"squared=";i*i4 IF i>=100 THEN GOTO 65 GOTO 26 PRINT "Program Completed."7 END

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

1 FOR i=1 TO 1002 PRINT i;"squared=";i*i3 NEXT i4 PRINT "Program Completed."5 END

The program jumps from one area to another, but this jumping is formal and more easily predictable, because for loops and functions provide 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.

Here is another example of Spaghetti code with embedded GOTO statements. INPUT "How many numbers should be sorted? "; T DIM n(T) FOR i = 1 TO T PRINT "NUMBER:"; i INPUT n(i) NEXT i 'Calculations: C = TE180: C = INT(C / 2) IF C = 0 THEN GOTO C330 D = T - C E = 1I220: f = EF230: g = f + C IF n(f) > n(g) THEN SWAP n(f), n(g) f = f - C IF f > 0 THEN GOTO F230 E = E + 1 IF E > D THEN GOTO E180 GOTO I220C330: PRINT "The sorted list is" FOR i = 1 TO T PRINT n(i) NEXT i

See also

External links

Notes and References

  1. Markus. Pizka. Straightening spaghetti-code with refactoring?. Software Engineering Research and Practice. 2004. 846–852. 5 March 2018. 5 March 2018. https://web.archive.org/web/20180305202716/http://itestra.com/wp-content/uploads/2017/08/04_itestra_straightening_spaghetti_code_with_refactoring.pdf. dead.
  2. Cram. David. Hedley. Paul. Pronouns and procedural meaning: The relevance of spaghetti code and paranoid delusion. Oxford University Working Papers in Linguistics, Philology and Phonetics. 2005. 10. 187–210. 5 March 2018. 6 March 2018. https://web.archive.org/web/20180306022905/http://mostlyharmless.org.uk/wp-content/uploads/2010/12/cramhedley-web.pdf. dead.
  3. Book: Horstmann. Cay. Java Concepts for AP Computer Science. 2008. J. Wiley & Sons. Hoboken, NJ. 978-0-470-18160-7. 235–236. 5th ed. [i.e. 2nd ed.].. http://horstmann.com/bigjava3.html. 2 January 2017. en. Chapter 6 - Iteration.
  4. Book: ASTM special technical publication. 500–565. United States National Bureau of Standards. United States Government Printing Office. 1980.
  5. Moha. N.. Gueheneuc. Y. G.. Duchien. L.. Meur. A. F. Le. DECOR: A Method for the Specification and Detection of Code and Design Smells. IEEE Transactions on Software Engineering. January 2010. 36. 1. 20–36. 10.1109/TSE.2009.50. 0098-5589. 10.1.1.156.1524. 14767901.
  6. Book: Abbes. M.. Khomh. F.. Gueheneuc. Y. G.. Antoniol. G.. 2011 15th European Conference on Software Maintenance and Reengineering . An Empirical Study of the Impact of Two Antipatterns, Blob and Spaghetti Code, on Program Comprehension . 2011. 181–190. 10.1109/CSMR.2011.24. 978-1-61284-259-2. 10.1.1.294.1685. 14152638.
  7. Guy Lewis Steele. 1977. Macaroni is better than spaghetti. In Proceedings of the 1977 symposium on Artificial intelligence and programming languages. Association for Computing Machinery, New York, NY, USA, 60–66. DOI:https://doi.org/10.1145/800228.806933
  8. Book: Conway, Richard. A primer on disciplined programming using PL/I, PL/CS, and PL/CT. Winthrop Publishers. 1978. 978-0-87626-712-7.
  9. Book: An Introduction to Programming. Conway. Richard. Gries. David. 3rd. Little, Brown. 1979. 978-0-316-15414-7.
  10. IEEE Computer. A spiral model of software development and enhancement. Boehm. Barry W.. 21. 2. May 1988. 61–72. 10.1109/2.59. 1781829.
  11. Book: Noll, Paul. Structured programming for the COBOL programmer: design, documentation, coding, testing. M. Murach & Associates. 1977.
  12. Ada – Europe '93 (Proceedings). Lecture Notes in Computer Science. Use and abuse of exceptions — 12 guidelines for proper exception handling. Schwille. Jürgen . Lecture Notes in Computer Science . 688. 1993. Springer Berlin Heidelberg. 142–152. 10.1007/3-540-56802-6_12. 978-3-540-56802-5 .
  13. The Michigan Technic. BASICally speaking...FORTRAN bytes!!. MTSBS. 99. 4. March–April 1981.
  14. Book: Hamming . Richard . The Art of Doing Science and Engineering . 1996 . Taylor & Francis . 9056995006.
  15. De Troyer. O.. The OO-binary relationship model : A truly object oriented conceptual model. Advanced Information Systems Engineering. 498. 13 May 1991. 561–578. 10.1007/3-540-54059-8_104. en. Notes on Numerical Fluid Mechanics and Multidisciplinary Design. 978-3-319-98176-5. 10894568 . Andersen . Rudolf. Bubenko . Janis A. . Sølvberg . Arne.
  16. Tomov. Latchezar. Ivanova. Valentina. Teaching Good Practices In Software Engineering by Counterexamples. Computer Science and Education in Computer Science. October 2014. 1. 397–405. 5 March 2018.