What Bugs Do ‘Goto’ Statements Cause? Historical Examples

bad codebugcoding-stylegotohistory

I understand that save for breaking out of loops nested in loops; the goto statement is evaded and reviled as a bug prone style of programming, to never be used.

XKCD
Alt Text: "Neal Stephenson thinks it's cute to name his labels 'dengo' "
See the original comic at: http://xkcd.com/292/

Because I learned this early; I don't really have any insight or experience on what types of bugs goto actually leads to. So what are we talking about here:

  • Instability?
  • Unmaintainable or unreadable code?
  • Security vulnerabilities?
  • Something else entirely?

What kind of bugs do "goto" statements actually lead to? Are there any historically significant examples?

Best Answer

Here's a way to look at it I have not seen in the other answers yet.

It is about scope. One of the main pillars of good programming practice is keeping your scope tight. You need tight scope because you lack the mental ability to oversee and understand more than just a few logical steps. So you create small blocks (each of which becomes "one thing") and then build with those to create bigger blocks which become one thing, and so on. This keeps things manageable and comprehensible.

A goto effectively inflates the scope of your logic to the entire program. This is sure to defeat your brain for any but the smallest programs spanning only a few lines.

So you will not be able to tell if you made a mistake anymore because there is just too much to take in and check for your poor little head. This is the real problem, bugs are just a likely result.

Related Topic