There are few programmers who haven’t heard of spaghetti code. But few truly understand it. There are often first year students who ask why they shouldn’t use goto statements in their code. It’s hard for them to understand partially because they have little context. Spaghetti code is intrinsically linked with the use of goto statements. When Fortran first appeared, the goto statement was one of the languages core control structures. It provided jumps from one part of the program to another part – often in an extremely unstructured manner. Languages that followed like Pascal, C, Ada, all had goto statements.
So adding one goto statement to a program may not seem like a big deal. But it is, and students don’t truly understand this. Until of course they have re-engineered a “small” vintage Fortran program. Then they get a true feeling for how messy unstructured programs are – pure spaghetti. Consider the amount of “jumps” in the following small legacy Fortran program from 1966.
A full description of spaghetti code with examples, can be found here.