Spaghetti code is a pejorative phrase for source code that has a complex and tangled control structure, especially one using many GOTO statements, exceptions, threads, or other “unstructured” branching constructs. It is named such because program flow is conceptually like a bowl of spaghetti, i.e. twisted and tangled. Spaghetti code can be caused by several factors, such as continuous modifications by several people over a long life cycle. Structured programming greatly decreases the incidence of spaghetti code.
That’s cool and all but what the hell is Lasagna code?
Well Lasagna Code is any program structure characterized by several well-defined and separable layers, where each layer of code accesses services in the layers below through well-defined interfaces. The analogy stems from the layered structure of lasagna, where different ingredients (for example, meat, sauce, vegetables, or cheese) are each separated by strips of pasta.
Look at how beautiful Lasagna looks. Structured, well defined, everything enclosed to it’s own layer. No part is trying to grasp or access items outside it’s own layer. A perfect example of well structured design.
No wonder Garfield loves Lasagna. It’s a work of art. Taste wise I prefer barbecued meat but to the eye, Lasagna looks AWESOME.
So, back to our point. What are the pitfalls of Lasagna code?
Lasagna code offers proper decoupling of your code, it divides properly into layers of scope, it can help with training newcomers as they get trained in the scope of each layer separately and get to know the system step by step, so what might be the pitfalls of Lasagna code?
Many people use the term Lasagna Code as derogative, trying to explain but code structure. Well Lasagna code is not a bad structure. Look a it. It smoothly separates your code in layers, each layer exposes an interface to communicate with, and each class can be easily swapped or refactored as long as it implements that interface. Just like Lasagna. Organized and tasteful.
But not all lasagna tastes great, lasagna might suffer from one great flaw that an experienced chef will never do, but a young cook will.
Too much Pasta
Get it? No? OK.
When the ratio of pasta to meat leans over to the pasta side, then Lasagna tastes awful. When it’s more pasta than meat it stops being Lasagna and starts being plain pasta.
So just like Lasagna might suffer from too much pasta, Lasagna code might suffer from too much layering of the system. It’s easy to get distracted, enjoying all this layering, decoupling and compartmentalization of your code that you end up with too many layers, or too much pasta.
So even though many use the term as derogative, to me Lasagna code is good. As long as the ratio of pasta to meat is the proper one. What is proper? Well that relies deeply in you and your system.