Yes Sir, that's what I'm gonna say! |
Few years later, at the high school, my computer science teacher told me that goto is evil but luckily its use can be always avoided (see the Structured Program Theorem). One of the reasons for its bad reputation is that usually the code that uses goto is less readable and the program flow is difficult to follow. In two words: spaghetti code.
Only a couple of years ago I realized that this is not true. Goto is not evil by itself and in some (many) situations its use can produce a code that is more readable, optimized, and with small memory footprint.
Do you want some examples? How do you exit from multiple nested loops? Without using goto you should have at least a variable and an if for every loop that encloses loops and then you should have a bunch of breaks. And what is a break? A masked goto.
And what about multiple exit conditions that need to free allocated memory? Without using goto, you should duplicate the cleanup code with the risk to forget something every time you need to modify the code in the future.
These are only a couple of examples about the usefulness of the goto statement. If you are not fully convinced, have a look to this thread.
Post a Comment