Programming Pasta - Spaghetti, Lasagna, Ravioli and Macaroni Code
It is vital that a program be properly structured and that it is logical. A beginner may not care much about the structure of the program but as one progresses towards complex programs he may find that the program may be a compiled by different people and without a logical structure, understanding the program would turn into a nightmare. Based on such an assumption, it has inspired the usage of terms for computer program structures that have some similarity to different styles of food.
Spaghetti CodeImage Courtesy: joostdevblog.blogspot.com
As quite evident from the name, it refers to the source code whose control flow is entangled and intertwined like spaghetti. Every line of code might be referencing almost any variable anywhere and maintaining the code becomes all but impossible, because the risk of introducing bugs elsewhere is too great. This term was mainly defined in context of the Goto statement since problems were faced while discerning source code using Goto statements in which the control could bounce about a program due to which no one could guarantee about the state of the program while it executed a specific piece of code. Goto statements were the leading cause of spaghetti code at one time, but since their fall object oriented programming has taken its place. Though designed to prevent spaghetti code, object oriented programming allows an object to mix code and data freely and conform to any number of interfaces, while a class can be subclassed freely about the program. Object oriented programming can be very effective if used with proper discipline but some programmers don’t handle it well which leads to spaghetti code. The solution to the spaghetti code is use of an aggressive and proactive refactoring effort focused on keeping programs small, effective, clean in source code, and mainly, coherent.
Lasagna CodeImage Courtesy: http://joostdevblog.blogspot.com
Lasagna code can be simply defined as the code that has too many layers. A technical explanation of lasagna code is that it is a code where a lot of really small classes are present instead of a small range of larger classes which would have been more understandable. Programmers new to object oriented programming tend to create a lot of small classes without keeping in mind that there is an ideal class size as well. In general classes above 500 lines are almost always too long, causing spaghetti within the class, while classes beneath 50 lines are quite often too small and thus part of lasagna. Lasagna code, a term coined in 1982, is generally seen in client-server applications where well-defined interfaces between client and server exist. This type of code is used to describe the software that has understandable and layered structure but though layered, it is still not easy to modify since a change in one layer is not so easy in practice. Adding a new field will only add complexity since it will require every object at every layer in the architecture to have the knowledge of the new field. The network protocol layers are an example of lasagna code.
Ravioli CodeImage Courtesy: wordpress.com
The ideal software structure is one having components that are small and loosely coupled; this ideal structure is called ravioli code. Ravioli are basically little dough wraps with good stuff in them so in a ravioli code, each of the components, or objects, is a package containing some meat or other nourishment for the system. Any component can be modified or replaced without significantly affecting other components in a ravioli code. In a ravioli code, there are thousands of little classes and it is very difficult to find where everything happens from. While this type of coding is quite attractive from a perspective of coupling and cohesion but separation and encapsulation of the code can expand call stacks which in turn create a problem for navigation through the code for maintenance purposes.
Macaroni code is an analogy of the macaronic language and this code is defined as the code that uses a mixture of computer languages in a single document. A program code is usually written so that other programmers can have a look plus various programming languages have the functionality of working in mixed environment in a single document. A good know-how of most of the programming languages is necessary to understand the macaroni code.