Works with

Salesforce logo
Search
Close this search box.

Spaghetti Code: a Love-Hate Story

Ah spaghetti, bloody delicious when covered in marinara, pesto, or creamy pasta sauce – awful when it has anything to do with my lines of code. This article is dedicated to my (love?) hate relationship with spaghetti code.

I hope by the end of the article you’ll hate spaghetti code as much as I do, and we can mutually take solace in our shared experience. If not that, then at least I hope you learned something, or perhaps even had a laugh!

In this Spaghetti Code Love-Hate Story...

So let’s begin, what is Spaghetti code 🙄?

It is what happens when you write code that doesn’t follow sound software development practices. By the end of this article, I hope that it will be clear what spaghetti code is, or at least what it looks like (and what not to do).

The formal meaning of spaghetti code is what happens when you have what is called deep or wide dependencies. By this what I mean is that your code depends on too many, or too few parts of your application to function correctly.

But there are many different ideas of what constitutes spaghetti code. I think my favorite definition comes from Alex Martelli, “Spaghetti code is what happens when you don’t obey the rules of proper top-down decomposition”.

For those unfamiliar with the term ‘top-down’, let me quote Wikipedia on the matter: “Top-down design, also known as hierarchical design, starts by defining high-level modules and successively lower-level submodules until individual routines are defined.”

This means that an ideal piece of spaghetti will go from being what is currently written, to what the code should do, by following these steps:

1) Identify what this piece of spaghetti does.

2) If what it currently does isn’t what it should be doing, re-write it so that it does what it needs to do (this stage can be broken down into what you need your code to do).

3) Write the code.

4) Repeat until all spaghetti has been replaced with a non-spaghetti code.

However, more often than not, people simply skip over these 3 and 4 and go straight to 2 or 1 (usually 1), and then start coding away like mad scientists without any regard for clean code practices A few months later they end up with what is commonly referred to as spaghetti code.

How to identify spaghetti code?

Firstly, if your code looks like a hard-to-follow hot mess, that’s probably your first hint. For more harder to identify spaghetti codes, here are some other clues:

  • If editing your code, or de-bugging causes immense frustration – chances are, it is spaghetti code.
  • If no developer wants to work with you on a particular project or wants to review your PR request – chances are, it is spaghetti code
  • If your manager randomly has outbursts of anger at your – chances are, you write a lot of spaghetti code.

Don’t worry if any of those examples apply to you, what matters is what you think your spaghetti code looks like – it’s likely that what you think looks bad now will actually look a whole lot worse after reading this article.

So what does spaghetti code look like?

This depends on what language you’re writing in and the framework/libs that are being used. Here, I’ll show some spaghetti code written in JavaScript using jQuery as an example:

function KFCSecretMenu() { //constructor

this.getKrushers = function() {

} } $(document).ready(function(){ //event listener for DOM Ready $(‘.welcome’).click(function (){ //event listener for click on element with class name of welcome $(‘.menu-item’).addClass(‘active’); //class to be added if the submenu is opened.

$(‘.menu-item’).find(‘.description’).slideDown(); //sliding down effect for description text }) }); function MenuItemClicked (e) { var itemID = $(e.target).parent().attr(‘id’); //gets ID of parent menu item var description = $(e.target).next().text() KFCSecretMenu.getKrushers({‘itemID’:itemID,’description’:description}); //calling function below }

Following? Nope, it’s spaghetti alright! 🕵️

We’ll look at what makes this piece of spaghetti code so bad, what makes it spaghettier than others, and what you can do to fix it. So what makes spaghetti code so bad? I think this is the most useful list of what makes spaghetti code what it is: Duplicated or overly complex functions, methods and variables Hard-to-follow and/or non-indented structures Hard-to-follow and/or long functions Hard-to-read white space Hard to follow logic Lack of comments Unusual coding styles (trying too hard) Good variable names.

There’s also some other things that will make your code a hot mess, but don’t necessarily count as spaghetti code: Inefficient algorithms Large file size Poorly designed software Lack of tests Spaghetti code looks different in every language, framework, and libs used – what might be spaghetti in one might not be spaghetti in another. That’s what makes it hard to define what spaghetti code looks like, what you need to do is think back on what would have been spaghetti code for you a few years ago – what were the biggest issues with your code? You probably made some of the mistakes from the list above, so start comparing what was bad about those past pieces of code and try keeping that list in mind while reading this article.

So, you have identified your spaghetti code, how to deal with it?

Some important things to note before we actually start discussing how to turn our piece of spaghetti… erm I mean code into what it should be:

Note 1: There are hundreds (maybe thousands) of ways out there on how NOT to write your code. I’m trying to keep this as simple as possible, the context of what I’m writing here is for those with a very basic knowledge on how to write code. You would actually be surprised what you can do by simply adding some white space and comments!

Note 2: It’s essential that you understand what you’re doing before trying to fix spaghetti code yourself – otherwise you’ll just make it worse! If not, read this article again until you do – then come back here.

Note 3: We are looking into what makes your spaghetti messy right now, what we’re not however looking into is what could have caused it in the first place (and therefore should cause us to think twice before opening up our editor).

What are the benefits of Spaghetti code?

Nothing. Next point!

How to prevent future spaghetti code?

Besides moving to Italy and having a very angry Italian man hit you over the head with a bunch of raw spaghetti every time you write a line of unclean code, you could try these things…

  • A good way to avoid spaghetti code is to keep your functions and methods short. That means that a function should only do what it is supposed to, there’s no need for a function that does 10 different things or has 100 lines – remove what’s not necessary!
  • Another tip to avoid spaghetti code is to keep what’s been done from being changed – what I mean by that is that you should try to not have deep nesting of loops within other loops, or in other words: don’t nest what doesn’t need to be nested!
  • Don’t forget about code formatting either, white space and indentation can do miracles for spaghetti code! The more good looking the code looks, the easier it will be to read.
  • Retros – always review what went well so you can track this, and implement it next time!
  • Alternatively, you could improve your coding
  • A final tip when planning what you want your spaghetti code to look like, try drawing graphs out of what you want it to do instead of writing your code directly in your editor (that means look at what each function needs to do before actually implementing them). If this goes wrong, take a step back and start over.

If you follow these simple rules, I can almost guarantee that what you’re writing will not turn into spaghetti – but what do I know? Maybe what your aunt used to call spaghetti is actually spaghetti code in disguise.

Final thoughts about spaghetti code

You could also try to embrace your spaghetti, love it as the only source of joy in your life! If that doesn’t work for you, there’s this excellent GIF that simulates the alternative.

Dev meme about tasks and meetings engineers sprint backlog review

If all else fails, you could just improve your coding ability. But seriously, that should be the last, last, last, lasssst thing you should do!

TL;DV FOR SALES
Meet your AI Sales Coach
Our AI tracks playbook adoption and measures how well objections are handled, giving managers coaching insights to improve rep performance. It summarizes countless meetings and highlights key discussions. It’s great for reps too – automating meeting prep, CRM updates and follow-up emails

Define your winning Playbook per Deal Stage, and let our AI handle the analysis.
Monitor how well your sales team follows it and where new hires have opportunities to improve their win rates.

Understand how your team handles common customer objections across hundreds of calls.
Spot where they excel and where they can improve to win more deals!

Organize clips and reels of what great sales calls look like according to your own playbooks.

Improve overall team efficiency and ensure each rep can save over 4 hours each week.

  • Joins your meetings, even if you don’t.
  • Records and transcribes in +30 languages.
  • Takes notes in your preferred structure.
  • Summarize one or multiple meetings.
  • Auto updates your favourite tools (Slack, Notion, CRM, etc).
  • Provides coaching insights on your meeting style.
Running late to your next meeting?
Send tl;dv instead and get blown away!