You may be wondering what I am talking about. You have probably seen a loaf of bread, sliced up and packaged by the marketplace – neatly arranged in a plastic enclosure of some sort. Then you have probably seen a plate of spaghetti, tangled and restless on your fine china. Spaghetti Code is an old term that refers to a computer program – whether it’s a script or full-blown utility – that runs the same way it’s written: from top to bottom, serially, with little to no repositioning or “jumping around” involved.

THE PROCESS

Software developers and engineers are more than just programmers. The creation of a program involves a fair amount of forethought. Typically, program creation can be broken down into four steps:

  • Planning
  • Analysis
  • Design
  • Implementation

The planning phase occurs when the need for a program’s creation is realized. Why build the system? Is it feasible? This phase is typically where a proposal, project plan, and official “feasibility study” are created. Often, requirements are determined as part of this initial information gathering phase. This helps avoid the potential for “scope creep” (where a semi-finished product gets requests for additional features, which were not in the original plan.)

The analysis phase is where the functionality of the system is clarified. What will the system do? What will the User Experience be like? This is typically a highly technical phase, where requirements, models, and diagrams are drafted. A Design Document is started, which (for an official project) is typically around 200 pages and includes an overview, design description (with the previously mentioned diagrams), and a test plan with the results. Often there will be several appendices to the document, which include definitions of all entities described in the diagrams, complete documentation (like a user manual), implementation details, and the entire source code.

The design phase is where it all comes together. The user interface is finalized, database schemas are created, and the system is architected. Details such as the physical architecture (will it be client-based? client-server? distributed?) are hashed out and the source code is written.

The implementation phase is where the completed product is heavily tested, documented, and – as the name implies – implemented for use in the intended production environment.

This series of steps is known as the waterfall method of development. There are other methods which are common these days, such as agile (essentially, build the boat while you’re on the water using it), parallel (where the “design” phase is broken down into several sub-phases of “design” and “implement”), and phased (where all four steps are repeated in sequence).

How does this relate to spaghetti code and the overall structure of a program?

THE SOLUTION

Modern programs are typically not written in a single file intended to run from top to bottom. In the past, this was often the case with popular languages such as BASIC and even Assembly to an extent. Most modern programs are composed of multiple files, representing different regions or functions of the program.   Often, these files are then placed within a separate folder structure hierarchy, for better organization. Note that a modern script, a light program intended to automate a simple task, may have only a single file. However, it is usually broken up into functions, which are blocks of code that can be called from anywhere.

This is where “spaghetti code” comes into play.

Pretend you had a program where you needed to get the current hour of the day – in five different places. Would you really want to copy and paste the required three lines of code, in five different places? What if you had to update part of the logic because you realized it didn’t work properly? Take a look at this code:

Don’t worry too much about the syntax right now – the “int” in front of currentHourOfDay simply means this function will return an int. Focus on the fact that you can reuse the above block of code by simply calling currentHourOfDay() in place of a number.

You could create a new variable and set it equal to the current hour:

You could use the current hour of the day as a conditional logic structure:

Or you could simply have the program do something continuously until 7:00 hits.

The old method, which would be to repeatedly copy and paste the lines inside of the currentHourOfDay function throughout the program, would lead to what’s known as spaghetti code: one long, unorganized serial program, which runs from top to bottom and is essentially a set of sequential commands. Spaghetti code is typically looked down upon by programmers, due to its inflexibility (and often, lack of documentation). Really long programs and scripts, written in spaghetti code fashion, often appear convoluted to anyone that tries to read or maintain it.

Remember that, as a programmer, you are rarely the only person who will end up seeing your code. Comments and good structural organization not only help you remember what you were thinking, but help newcomers understand your code and co-workers maintain your code.

Share

Comments are closed.

Post Navigation