What is “spaghetti” code?
YouTube Viewers YouTube Viewers
15.1K subscribers
7,600 views
0

 Published On Nov 12, 2020

We all know how things are when you enjoy your spaghetti too much: slurping all without any consideration getting that sauce everywhere: cheeks, eyes, ears, your momma’s favourite tablecloth. Eating in a clean way without cutting it in pieces is way too hard to make it worthwhile. Same goes with the programming: when working with really messy code that isn’t following any good practices well often say it’s spaghetti code. But what is spaghetti code anyway?

#programming #tech #softwaredevelopment #cleancode #quickanswer

Implementing Effective Code Reviews
UK: https://amzn.to/355xOTW US: https://amzn.to/3dAgr1q

This term draws its meaning back from olden days when things weren’t as refined and settled as they’re now. Fathers of programming had to invent many concepts themselves, and until they did - many things were written without them. One such concept is structured programming invented by dutch computer scientist Edsger Wybe Dijkstra. Oh this guy. In 1968 he wrote an open letter “Go To Statement Considered Harmful” to Communications of the Association for Computing Machinery. Wow that’s a mouthful.
This letter caused serious debate in the community. You see - GOTO is the programming instruction that makes the program jump, or continue execution from a different place in the code. It is similar to the behaviour that happens when an IF statement’s condition is not met. Program just skips execution of these few lines that are between curly braces. GOTO is an unconditional change of execution flow from one portion of code to another.
Such jumps are imperative for compiled machine code, as processors only support limited logic, and we need to optimise for speed there, but no programmer is expected to think and design like a CPU. Almost no languages of higher level support goto today, and those which do - discourage its use.
Despite not using GOTO, modern code can still be considered a spaghetti code. Giuliana Carullo outlines following examples in her book (see the description):
Do not design your solution in advance, that is, no architecture, nothing.
Use a bunch of global variables.
Use an object-oriented (OO) language, but dismiss all the benefits (inheritance, polymorphism, etc.).
Forget that someone told you that design patterns exist. They are your enemy.
Write big classes with tons of different responsibilities.
Write highly coupled components.
Don’t think about APIs and just pick and choose your preferred names.
If parallelization is required, just add a bunch of threads when you think it might somehow work.
Don’t consider code reusability; someone else needs to reinvent the wheel in case needed.
It doesn’t take a genius to understand that this is a great spaghetti recipe. I call it: fire and forget - once you wrote this code forget about changing it in the future.
I recently had a pleasure of refactoring such code from its original form - I even found some goto’s in it, as C++ still supports them. It is less than 5000 lines of code, but I’m 9 months into this process and I still haven’t completed the damn refactoring. Majority of time was spent on writing unit tests, as there were none. Man - let me tell you: spaghetti code is a nightmare to maintain! Subscribe, and I’ll see you in the next one, cheers!

show more

Share/Embed