Imagine a bowl of cooked spaghetti and you manage to take a few of its strands on a fork. Unfortunately, few fall back to bowl. Now, trying to search for which is the longest string or the start point of it. Quite a task. Isn’t it? Analogically, this is what happens when we have a spaghetti code.
What is spaghetti code?
Spaghetti code, as the name says it is an unstructured, complex and tangled nature of source code which is troublesome to maintain and comprehend for fellow programmers.
How does it get introduced?
Not implying the design patterns expected for a programming language per se.
The code starts simple but as and when new features get appended for the sake of scalability not understanding the existing code can introduce redundant code and looping/jumping of program from one to other, back and forth. Also if multiple developers work simultaneously problem can be worse.
Overuse of workarounds to serve the purpose of functionality for time being and never refactoring it.
Is spaghetti code avoidable?
Yes. It is definitely avoidable if as a code developer we use more readable variables and provide comments to make it comprehensible.
Before beginning writing new feature understand what the existing code does and is it possible to tweak it a little bit to improvise new feature.
If there is a reusable code block its a good practice to isolate it in a function construct and make a call from other code block on need basis.