|The Goto, Spaghetti and the Velociraptor
|Written by Mike James
|Friday, 16 July 2021
Page 2 of 2
The state of spaghetti
Today most languages use block structure but some have slipped the Goto back into the list of control statements - why I have no idea.
You can prove that you never need the Goto and its use is nearly always detrimental to program structure - but it's there if you really need it.
It's there as a sort of comfort blanket or perhaps for days when you want to reenact scenes from old programming practices or simply to cook up a bowl of spaghetti code to frighten the beginners.
However the Goto isn't the only bad statement prowling around.
Since the early days of structured programming language designers have tended to add structures that they think might make their languages more useful. You can now generally jump out of loops and conditionals and we have loop constructs with exit points anywhere you care to place them.
These are very capable of being misused to create spaghetti - but few are warned of the possibility.
Object-oriented programming also has a lot to answer for.
Done properly it's great. But the need to write methods that do something coherent in terms of the object mean that methods get bigger and bigger. With structured modular programming if a procedure got bigger than ten lines of code you considered splitting it into two.
In object-oriented code you start to write an Update method and it starts to get big. What do you do split it into an Update_first and an Update_second?
You could create some private helper methods but these don't really fit with the object-oriented way of doing things. The trouble is that methods don't naturally break down in a hierarchy of procedures that have any real meaning in what the object is or does - they are simply messy internal working.
As a result object-oriented programmers don't think size matters. They often write big methods with the result that their flow of control graphs are big and messy - not quite spaghetti but well on the way to linguine.
What can you do to keep your code simple and avoid being eaten by a velociraptor?
All you really have to do is never use the Goto and aim for a simple flow of control graph.
Of course all rules, these and many that you no doubt can think up, are not absolute. We still have a freedom to express an algorithm as we please and the guiding principle is that the expression should indeed be pleasing.
The next time you feel like being adventurous with your code - listen for the breath of the velociraptor.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Friday, 16 July 2021 )