Spaghetti code

Last updated

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

Contents

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:

If, in fixing up an error, you wanted to insert some omitted instructions then you took the immediately preceding instruction and replaced it by a transfer to some empty space. There you put in the instruction you just wrote over, added the instructions you wanted to insert, and then followed by a transfer back to the main program. Thus the program soon became a sequence of jumps of the control to strange places. When, as almost always happens, there were errors in the corrections you then used the same trick again, using some other available space. As a result the control path of the program through storage soon took on the appearance of a can of spaghetti. Why not simply insert them in the run of instructions? Because then you would have to go over the entire program and change all the addresses which referred to any of the moved instructions! Anything but that!

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

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 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.

1i=02i=i+13PRINTi;"squared=";i*i4IFi>=100THENGOTO65GOTO26PRINT"Program Completed."7END

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

1FORi=1TO1002PRINTi;"squared=";i*i3NEXTi4PRINT"Program Completed."5END

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? ";TDIMn(T)FORi=1TOTPRINT"NUMBER:";iINPUTn(i)NEXTi'Calculations:C=TE180:C=INT(C/2)IFC=0THENGOTOC330D=T-CE=1I220:f=EF230:g=f+CIFn(f)>n(g)THENSWAPn(f),n(g)f=f-CIFf>0THENGOTOF230E=E+1IFE>DTHENGOTOE180GOTOI220C330:PRINT"The sorted list is"FORi=1TOTPRINTn(i)NEXTi

See also

Related Research Articles

<span class="mw-page-title-main">BASIC</span> Family of programming languages

BASIC is a family of general-purpose, high-level programming languages designed for ease of use. The original version was created by John G. Kemeny and Thomas E. Kurtz at Dartmouth College in 1963. They wanted to enable students in non-scientific fields to use computers. At the time, nearly all computers required writing custom software, which only scientists and mathematicians tended to learn.

Computer programming or coding is the composition of sequences of instructions, called programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of procedures, by writing code in one or more programming languages. Programmers typically use high-level programming languages that are more easily intelligible to humans than machine code, which is directly executed by the central processing unit. Proficient programming usually requires expertise in several different subjects, including knowledge of the application domain, details of programming languages and generic code libraries, specialized algorithms, and formal logic.

<span class="mw-page-title-main">Computer program</span> Instructions to be executed by a computer

A computer program is a sequence or set of instructions in a programming language for a computer to execute. It is one component of software, which also includes documentation and other intangible components.

<span class="mw-page-title-main">Fortran</span> General-purpose programming language

Fortran is a third generation, compiled, imperative programming language that is especially suited to numeric computation and scientific computing.

PL/I is a procedural, imperative computer programming language initially developed by IBM. It is designed for scientific, engineering, business and system programming. It has been in continuous use by academic, commercial and industrial organizations since it was introduced in the 1960s.

Procedural programming is a programming paradigm, classified as imperative programming, that involves implementing the behavior of a computer program as procedures that call each other. The resulting program is a series of steps that forms a hierarchy of calls to its constituent procedures.

Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection (if/then/else) and repetition, block structures, and subroutines.

In computer science, control flow is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an imperative programming language from a declarative programming language.

A programming paradigm is a relatively high-level way to structure and conceptualize the implementation of a computer program. A programming language can be classified as supporting one or more paradigms.

In computer programming, a block or code block or block of code is a lexical structure of source code which is grouped together. Blocks consist of one or more declarations and statements. A programming language that permits the creation of blocks, including blocks nested within other blocks, is called a block-structured programming language. Blocks are fundamental to structured programming, where control structures are formed from blocks.

Ratfor is a programming language implemented as a preprocessor for Fortran 66. It provides modern control structures, unavailable in Fortran 66, to replace GOTOs and statement numbers.

<span class="mw-page-title-main">Conditional (computer programming)</span> Control flow statement that executes code according to some condition(s)

In computer science, conditionals are programming language commands for handling decisions. Specifically, conditionals perform different computations or actions depending on whether a programmer-defined Boolean condition evaluates to true or false. In terms of control flow, the decision is always achieved by selectively altering the control flow based on some condition . Although dynamic dispatch is not usually classified as a conditional construct, it is another way to select between alternatives at runtime. Conditional statements are the checkpoints in the programme that determines behaviour according to situation.

In computing, a line number is a method used to specify a particular sequence of characters in a text file. The most common method of assigning numbers to lines is to assign every line a unique number, starting at 1 for the first line, and incrementing by 1 for each successive line.

In computer programming, COMEFROM is an obscure control flow structure used in some programming languages, originally as a joke. COMEFROM is the inverse of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement.

S-algol is a computer programming language derivative of ALGOL 60 developed at the University of St Andrews in 1979 by Ron Morrison and Tony Davie. The language is a modification of ALGOL to contain orthogonal data types that Morrison created for his PhD thesis. Morrison would go on to become professor at the university and head of the department of computer science. The S-algol language was used for teaching at the university at an undergraduate level until 1999. It was also the language taught for several years in the 1980s at a local school in St. Andrews, Madras College. The computer science text Recursive Descent Compiling describes a recursive descent compiler for S-algol, implemented in S-algol.

In computer programming, a branch table or jump table is a method of transferring program control (branching) to another part of a program using a table of branch or jump instructions. It is a form of multiway branch. The branch table construction is commonly used when programming in assembly language but may also be generated by compilers, especially when implementing optimized switch statements whose values are densely packed together.

Non-structured programming is the historically earliest programming paradigm capable of creating Turing-complete algorithms. It is often contrasted with the structured programming paradigm, in particular with the use of unstructured control flow using goto statements or equivalent. The distinction was particularly stressed by the publication of the influential "Go To Statement Considered Harmful" open letter in 1968 by Dutch computer scientist Edsger W. Dijkstra, who coined the term "structured programming".

<span class="mw-page-title-main">History of compiler construction</span>

In computing, a compiler is a computer program that transforms source code written in a programming language or computer language, into another computer language. The most common reason for transforming source code is to create an executable program.

<span class="mw-page-title-main">Goto</span> One-way control statement in computer programming

Goto is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control. The jumped-to locations are usually identified using labels, though some languages use line numbers. At the machine code level, a goto is a form of branch or jump statement, in some cases combined with a stack adjustment. Many languages support the goto statement, and many do not.

In computer programming, a function, procedure, method, or routine is a callable unit of software logic that has a well-defined interface and behavior and can be invoked multiple times.

References

  1. Markus, Pizka (2004). "Straightening spaghetti-code with refactoring?" (PDF). Software Engineering Research and Practice: 846–852. Archived from the original (PDF) on 5 March 2018. Retrieved 5 March 2018.
  2. Cram, David; Hedley, Paul (2005). "Pronouns and procedural meaning: The relevance of spaghetti code and paranoid delusion" (PDF). Oxford University Working Papers in Linguistics, Philology and Phonetics. 10: 187–210. Archived from the original (PDF) on 6 March 2018. Retrieved 5 March 2018.
  3. Horstmann, Cay (2008). "Chapter 6 - Iteration". Java Concepts for AP Computer Science (5th ed. [i.e. 2nd ed.]. ed.). Hoboken, NJ: J. Wiley & Sons. pp. 235–236. ISBN   978-0-470-18160-7 . Retrieved 2 January 2017.
  4. United States National Bureau of Standards (1980). ASTM special technical publication. United States Government Printing Office.
  5. Moha, N.; Gueheneuc, Y. G.; Duchien, L.; Meur, A. F. Le (January 2010). "DECOR: A Method for the Specification and Detection of Code and Design Smells". IEEE Transactions on Software Engineering. 36 (1): 20–36. CiteSeerX   10.1.1.156.1524 . doi:10.1109/TSE.2009.50. ISSN   0098-5589. S2CID   14767901.
  6. Abbes, M.; Khomh, F.; Gueheneuc, Y. G.; Antoniol, G. (2011). "An Empirical Study of the Impact of Two Antipatterns, Blob and Spaghetti Code, on Program Comprehension". 2011 15th European Conference on Software Maintenance and Reengineering. pp. 181–190. CiteSeerX   10.1.1.294.1685 . doi:10.1109/CSMR.2011.24. ISBN   978-1-61284-259-2. S2CID   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. Conway, Richard (1978). A primer on disciplined programming using PL/I, PL/CS, and PL/CT. Winthrop Publishers. ISBN   978-0-87626-712-7.
  9. Conway, Richard; Gries, David (1979). An Introduction to Programming (3rd ed.). Little, Brown. ISBN   978-0-316-15414-7.
  10. Boehm, Barry W. (May 1988). "A spiral model of software development and enhancement". IEEE Computer. 21 (2): 61–72. doi:10.1109/2.59. S2CID   1781829.
  11. Noll, Paul (1977). Structured programming for the COBOL programmer: design, documentation, coding, testing. M. Murach & Associates.
  12. Schwille, Jürgen (1993). "Use and abuse of exceptions — 12 guidelines for proper exception handling". Lecture Notes in Computer Science. Ada – Europe '93 (Proceedings). Lecture Notes in Computer Science. Vol. 688. Springer Berlin Heidelberg. pp. 142–152. doi:10.1007/3-540-56802-6_12. ISBN   978-3-540-56802-5.
  13. MTSBS[ clarification needed ] (March–April 1981). "BASICally speaking...FORTRAN bytes!!". The Michigan Technic. 99 (4).{{cite journal}}: CS1 maint: multiple names: authors list (link) CS1 maint: numeric names: authors list (link)
  14. Hamming, Richard (1996). The Art of Doing Science and Engineering. Taylor & Francis. ISBN   9056995006.
  15. De Troyer, O. (13 May 1991). Andersen, Rudolf; Bubenko, Janis A.; Sølvberg, Arne (eds.). The OO-binary relationship model : A truly object oriented conceptual model (PDF). Advanced Information Systems Engineering. Notes on Numerical Fluid Mechanics and Multidisciplinary Design. Vol. 498. pp. 561–578. doi:10.1007/3-540-54059-8_104. ISBN   978-3-319-98176-5. S2CID   10894568.
  16. Tomov, Latchezar; Ivanova, Valentina (October 2014). "Teaching Good Practices In Software Engineering by Counterexamples". Computer Science and Education in Computer Science (1): 397–405. Retrieved 5 March 2018.