|
Here's what "Writing Solid Code" (p. xxii) has to say on the subject of the
goto statement:
---[excerpt begins]--- And here's what "Code Complete" (p. 349) has to say on the subject:
---[excerpt begins]---
A primary feature of most goto discussions is a shallow approach to the
question. The arguer on the "gotos are evil" side usually presents a
trivial code fragment that uses gotos and then shows how easy it is to
rewrite the fragment without gotos. This proves mainly that it's easy to
write trivial code without gotos.
|
Indeed it always surprises me how quickly people are willing to regurgitate the age old argument against the use of goto. In my early days of programmer, I started as a BASIC spaghetti code programmer that over used goto's as a matter of course. Then I went to university where I was told never to use goto (or suffer the wrath of the TA's grading penalties.) Indeed I trained myself to get out of using goto's and indeed, I've never come to an algorithm that I couldn't some implement without gotos.
But then I got a job. On that job I had to really brush up my assembly skills as that was this language I was to use day-in, day-out. I have since merged my skills at high level and low level programming, and when I hear this debate it is enough to make me cringe. All the so called problems that goto's have are really just limitations of the coder's skill level, not anything intrinsic about the use of goto itself.
Some points that I think should be made: Over-use or capricious use of goto can definately lead to hard to follow and maintain "spaghetti code". Using high level flow control mechanisms helps the compiler and helps you see structure in your code. On the other hand, well placed sparse use of goto is usually clear. Examples:
On the plus side to minimizing the use of goto's it can help a beginner who has been raised on BASIC (like I was) break their bad habits. The biggest thing that a beginner can expect to learn from doing this is learning to program at a high level of abstraction that is different from what they are used to. Getting used to other ways of programming will help the beginner expand their breadth of programming paradigms. But as mentioned in "Writing Solid Code" above you don't want to get into a mode where you are blindly following rules that have nothing to do with writing clean maintainable code.
Update: I recently looked at the book "Compilers Principles, Techniques, and Tools". In it, it describes the intermediate languages that programs are compiled to in which typically all branches are transformed to goto and if ... goto constructions. The idea being that optimizations are done after the program has been transformed to a loopless goto riddled intermediate program. That seals it for me -- I say use goto whenever it makes sense.