Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Spaghetti code
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == Here follows what would be considered a trivial example of spaghetti code in [[BASIC programming language|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 <code>[[Goto|GOTO]]</code> statements create a reliance on [[line number]]s. 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. <syntaxhighlight lang="basic"> 1 i=0 2 i=i+1 3 PRINT i;"squared=";i*i 4 IF i>=100 THEN GOTO 6 5 GOTO 2 6 PRINT "Program Completed." 7 END </syntaxhighlight> Here is the same code written in a [[structured programming]] style: <syntaxhighlight lang="basic"> 1 FOR i=1 TO 100 2 PRINT i;"squared=";i*i 3 NEXT i 4 PRINT "Program Completed." 5 END </syntaxhighlight> The program jumps from one area to another, but this jumping is formal and more easily predictable, because [[for loop]]s and [[Subroutine|functions]] provide [[control flow|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. <syntaxhighlight lang="basic"> 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 = T E180: C = INT(C / 2) IF C = 0 THEN GOTO C330 D = T - C E = 1 I220: f = E F230: 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 I220 C330: PRINT "The sorted list is" FOR i = 1 TO T PRINT n(i) NEXT i </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)