Lecture
Spaghetti code is a poorly designed, poorly structured, confusing and difficult to understand program, especially containing many GOTO operators (especially transitions back), exceptions and other structures that worsen the structuredness [1] . The most common anti-pattern programming.
The spaghetti code is named like this because the flow of the program is like a bowl of spaghetti, that is, tortuous and tangled. Sometimes called “kangaroo code” ( kangaroo code ) because of the many jump instructions.
Currently, the term applies not only to cases of GOTO abuse, but also to any “multiply connected” code, in which the same small fragment is executed in a large number of different situations and performs many different logical functions [1] .
Spaghetti code usually occurs:
Spaghetti code can be debugged and work correctly and with high performance, but it is extremely difficult to maintain and develop [1] . Editing spaghetti to add new functionality sometimes brings such a huge potential for introducing new errors that refactoring (the main cure for spaghetti) becomes inevitable.
Below is an example of a spaghetti code in BASIC that performs a simple action - printing numbers from 1 to 10 and their squares. Real examples of spaghetti code are much more complex and create more problems when accompanying programs.
10 i = 0 20 i = i + 1 30 IF i <= 10 THEN GOTO 70 40 IF i> 10 THEN GOTO 50 50 PRINT "Program complete." 60 END 70 PRINT i; square =; i * i 80 GOTO 20
The same code written in the structured programming style:
FOR i = 1 TO 10 PRINT i; square =; i * i NEXT i PRINT "Program complete."
The same functional style code using the iteration method written in Ruby:
(1..10) .each {| i | puts "# {i} \ t square = # {i ** 2}"} puts "Program ended."
By analogy with the "spaghetti code" programmers came up with a few more concepts, not yet generally accepted.
Comments
To leave a comment
Software and information systems development
Terms: Software and information systems development