Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Spaghetti code

From Wikipedia, the free encyclopedia
Software source code with poor structure
Description of image
Cooked spaghetti, characteristically tangled

Spaghetti code iscomputersource code that encodescontrol flow that is convoluted, and therefore, hard to understand.Control statements directprogramexecution in ways that instead of having a quality of structure, resembles cookedspaghetti, twisted and tangled.[1]The code tends to be hard tomaintain.

Since control flow logic encoded via thegoto statement tends to lead to convoluted control flow, use of goto is often associated with a classification as spaghetti code.[2] The practice ofstructured programming was envisioned to eliminate the need for and use of the goto statement as one way to avoid the production of spaghetti code.Ensuring the creation of high-qualitysoftware, instead of spaghetti code, often involves aspects such as using bettertools,training developers and improvingsoftware development processes.[3]

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

History

[edit]

It is unclear when the phrasespaghetti code was coined. Martin Hopkins made an early reference to spaghetti in this context in 1972, writing that the "principal motivation behind eliminating the goto statement is the hope that the resulting programs will not look like a bowl of spaghetti."[6] In the 1978 bookA 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",[7] a phrase repeated in the 1979 bookAn Introduction to Programming he co-authored withDavid Gries.[8] 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.[9] In the 1979 bookStructured programming for the COBOL programmer, author Paul Noll uses the phrasesspaghetti code andrat's nest as synonyms to describe poorly structured source code.[10]

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

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

In a 1981 computer languages spoof inThe Michigan Technic titled "BASICally speaking...FORTRAN bytes!!", the author describedFORTRAN 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 resultthe 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!

Examples

[edit]

Simple

[edit]

The followingBASIC code, a program that prints 1 to 100, is a relatively simple example of code that can be more easily understood with structured control flow instead of using goto. The use ofGOTO for looping and lack of indentation leads to less than clear logic flow.

1i=02i=i+13PRINTi4IFi>=100THENGOTO65GOTO26END

The following code produces the same result, but uses a structuredloop statement and indentation to improve readability.

1FORi=1TO1002PRINTi3NEXTi4END

More representative

[edit]

The following code implements a numericsorting algorithm. The use of goto statements results in a spaghetti-like nature to the control flow.

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

Related

[edit]

Big ball of mud

[edit]

A big ball of mud is asoftware system that lacks a perceivable architecture. Although undesirable from a software engineering point of view, such systems are common in practice due to business pressures, developerturnover andsoftware entropy. The term was popularized by Brian Foote and Joseph Yoder although they credit Brian Marick for coining the term.[15]

A Big Ball of Mud is a haphazardly structured, sprawling, sloppy, duct-tape-and-baling-wire, spaghetti-code jungle. These systems show unmistakable signs of unregulated growth, and repeated, expedient repair. Information is shared promiscuously among distant elements of the system, often to the point where nearly all the important information becomes global or duplicated.

The overall structure of the system may never have been well defined.

If it was, it may have eroded beyond recognition. Programmers with a shred of architectural sensibility shun these quagmires. Only those who are unconcerned about architecture, and, perhaps, are comfortable with the inertia of the day-to-day chore of patching the holes in these failing dikes, are content to work on such systems.

— Brian Foote and Joseph Yoder,Big Ball of Mud. Fourth Conference on Patterns Languages of Programs (PLoP '97/EuroPLoP '97) Monticello, Illinois, September 1997

Pasta-related

[edit]

Inspired by the popularity ofspaghetti code, otherpasta-oriented terms that describe the structural nature of code include:

Lasagna code
Lasagna code haslayers that are so intertwined that making a change in one layer necessitates changing other layers too.[16]
Ravioli code
Ravioli code comprises well-structuredclasses that are easy to understand in isolation but in combination result in less than clear system design.[17]

See also

[edit]

References

[edit]
  1. ^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. Retrieved2 January 2017.
  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 fromthe original(PDF) on 6 March 2018. Retrieved5 March 2018.
  3. ^Markus, Pizka (2004)."Straightening spaghetti-code with refactoring?"(PDF).Software Engineering Research and Practice:846–852. Archived fromthe original(PDF) on 5 March 2018. Retrieved5 March 2018.
  4. ^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.Bibcode:2010ITSEn..36...20M.CiteSeerX 10.1.1.156.1524.doi:10.1109/TSE.2009.50.ISSN 0098-5589.S2CID 14767901.
  5. ^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.
  6. ^Hopkins, M. E. (1972): A Case fo the GOTO. In: ACM '72: Proceedings of the ACM annual conference - Volume 2, August 1972, pp 787–790, p 59 DOI:https://dl.acm.org/doi/10.1145/800194.805860
  7. ^Conway, Richard (1978).A primer on disciplined programming using PL/I, PL/CS, and PL/CT. Winthrop Publishers.ISBN 978-0-87626-712-7.
  8. ^Conway, Richard; Gries, David (1979).An Introduction to Programming (3rd ed.). Little, Brown.ISBN 978-0-316-15414-7.
  9. ^Boehm, Barry W. (May 1988). "A spiral model of software development and enhancement".IEEE Computer.21 (2):61–72.Bibcode:1988Compr..21e..61B.doi:10.1109/2.59.S2CID 1781829.
  10. ^Noll, Paul (1977).Structured programming for the COBOL programmer: design, documentation, coding, testing. M. Murach & Associates.
  11. ^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.
  12. ^United States National Bureau of Standards (1980).ASTM special technical publication. United States Government Printing Office.
  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. ^Foote, Brian; Yoder, Joseph (26 June 1999)."Big Ball of Mud".laputan.org. Retrieved14 April 2019.
  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. Retrieved5 March 2018.
  17. ^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.

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Spaghetti_code&oldid=1332090817"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp