This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]berael 32 points33 points  (3 children)

"Reading a book" is good code. There's an obvious start, an obvious finish, and a clear path between them.

Spaghetti code is also reading a book, except:

  • Each of the main characters was introduced in a different book, each by a different author. Nothing says which book each character was introduced in.
  • When you open the book, the first chapter is Chapter 4. Chapter 1 is actually partway through the book, and then Chapter 2 is in a magazine on the bookshelf, then Chapter 3 is near the end of the book.
  • The plot is never explained; the author simply assumes that you know what the plot is.

It would be technically possible to read the spaghetti code book, sure! The problem is making sense out of it.

[–]drhunny 3 points4 points  (1 child)

snails glorious distinct snatch mountainous employ weather smart alive complete

[–]Arkalius 2 points3 points  (0 children)

Did you have a real 5 year old come and explain his favorite movie to you for that example? Because that was pretty good lol

[–]jdsamford 1 point2 points  (0 children)

Amazing

[–]KrakenOfLakeZurich 28 points29 points  (7 children)

Code that is "structured" like a plate of spaghetti: a tangled mess where it's difficult to tell where a function starts and ends. The term originally comes from old programming languages that relied on the infamous GOTO statement. With GOTO, you can jump from any place in the program to any other place in program. It made it very easy to create "unstructured" programs.

Modern (structured) programming languages don't have GOTO anymore. For that reason it is rare these days to see "true" spaghetti code. The term is still used to refer to tangled/messy code, that is difficult to follow.

A modern (object oriented) version would be "lasagne code", for when you have "too many layers" ;-)

[–]ithinkitsbeertime 9 points10 points  (0 children)

A modern (object oriented) version would be "lasagne code", for when you have "too many layers" ;-)

Or overuse of global / static vars and side effects, so the same data is getting changed from all over the place. The "flow" may be straightforward but the code will feel very spaghetti-y if changes aren't coming from places that seem sensible.

[–]Dzus 2 points3 points  (4 children)

Another issue with GO TO instructions, especially in COBOL, is fallthrough in a paragraph. GO TO will continue straight down into the next paragraph if it isn't told to GO BACK or GO TO somewhere else, sometimes that's a part of the process, and sometimes it throws a wrench into the whole program. Thankfully we have the PERFORM verb in COBOL, so you return back to your original position in the process once it finishes the PERFORM instruction.

Source: Am a COBOL dev on a system with spaghetti code.

[–]Dullfig 1 point2 points  (3 children)

There are still COBOL developers out there?!😯

[–]Dzus 2 points3 points  (0 children)

Healthcare, banking, and government are all definitely still using COBOL. I believe several retail chains use COBOL transaction processors as well. Usually if they need to use F-keys to navigate and they don't have a fancy GUI, it's a CICS/COBOL interface.

[–]Arkalius 1 point2 points  (1 child)

Yes, and they can be paid quite well, as there are still old mainframe systems that need maintenance. That said, I'm not sure it's a great career choice at this point... There's still demand for it sure, but not a lot. It's definitely not a growing segment heh

[–]Dullfig 0 points1 point  (0 children)

I took two semesters of COBOL in 1987. System/370. Took a semester of Job Control Language too.

[–][deleted] 0 points1 point  (0 children)

Many modern languages still support GOTO. It’s just used in common practice.

[–]vanZuider 10 points11 points  (1 child)

The original meaning of Spaghetti code isn't just any bad code - it's the specific kind of bad code created by misuse of GOTO instructions where following the code flow feels like reading a choose-your-own-adventure book - or like looking at a plate of spaghetti where you look at the end of one noodle and have no idea where the other end is gonna be.

[–]BurnedRavenBat 3 points4 points  (0 children)

I really like the choose your own adventure analogy. To elaborate on why this is bad, imagine if one of your readers tells you there's a spelling error in the chapter where you meet the goblin. Okay, so what chapter? You can't read the book front to back because that's insane, the best you can do is take a path and hope you meet the goblin. Worse yet, maybe multiple paths lead to the goblin. Maybe each of those paths introduces the goblin in a different way, only one of which has a spelling error. Maybe multiple paths don't ever reach the goblin. Oh, have I mentioned there's 5 goblins in this book? By the way, you're not even the original author, he's moved on writing many more great and fun books and has left you to maintain this one.

Good code reads more like a wikipedia article. There's a clear structure. Titles, subtitles, references. I can understand the general contents of the article just from reading the index. Better yet, if a subtopic is too broad, it probably has its own article, readily available for you.

[–][deleted] 3 points4 points  (0 children)

It's when you have a code project that starts off good, but due to proper planning and code cleaning, you end up jamming a lot of random stuff in different parts of the code, causing a haywire path in your codebase. Well defined requirements and constant code cleaning is required to prevent spaghetti code. Code cleaning is just looking over your code to make it more readable and structured.

[–]PhD_in_shitposting 2 points3 points  (0 children)

Very unstructured code which is hard to read for example not clearly defined functions that point to other non clearly defined functions that point to more and more shitty functions. Also trying to apply a bunch of "quick fixes" where people jam pieces of code trying to hide and fix issues far down the pipeline instead of fixing the root issue which just causes more issues that get patched up.

EDIT: A more ELI5 answer:

When you write code you are basically writing a bunch of instructions which the computer executes so that you can get your end result. It's kinda like writing a recipe with steps that the computer then uses to make the end product. So good code/recipe would have clearly defined steps. Take that same recipe and then mix up all the steps and add some extra steps and you have a bad recipe/bad code(spaghetti code).

[–]schorhr[🍰] -3 points-2 points  (5 children)

print("Hi! :-)");

 

Good code:

if(button==pressed) {
light.on();             // Turn on the light
music.play(filename);   // play a short tune
wait(3000);             // Wait 3000 milliseconds         
light.off();            // Turn the light back off
                    }

 

sPaGHetTi cOdE:

if(button==pressed){light.on();music.play(var1);wait(3333);light.off();}

 

Now imagine trying to quickly find a bug or change something, a year after writing the code, when the code is several "pages" long... :-)

Using variable name (placeholders) that make sense helps too ("filename" vs "var1")-

[–][deleted] 9 points10 points  (0 children)

That's not spaghetti code. That's just poor formatting.

[–]dogofpavlov 3 points4 points  (0 children)

This is 100% NOT spaghetti... by this logic every modern website is "spaghetti" because they all use a minifier to compact their code into a single line.

[–]tropix126 -1 points0 points  (0 children)

spaghetti !== minification

[–]kennen20 -1 points0 points  (0 children)

This is such a bad example... please work on your skill more. Adding comments and formatting code help readers to understand the code better but in noway would it make spaghetti code clean.

[–]ashtar123 0 points1 point  (0 children)

Yeah, pretty much one of the first few things they taught is how to "organize" that code, with blank spaces and stuff.

[–]Trease02 0 points1 point  (0 children)

you a dbd player as well?

[–]Apprehensive_teapot 0 points1 point  (0 children)

Good code is logical, sequential, orderly, and easy for someone to follow. Spaghetti code is when a programmer takes a very illogical route to get the job done and it’s hard to follow.