Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Spaghetti code
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Spaghetti code
(section)
Add topic