In yesterday’s post I named the “GoTo” statement as basically a bad thing. To my surprise this started a short Twitter discussion and also came up in the comments. So I did a bit more research. I found this fairly balanced article called “Using gotos” by Steve McConnell. And Mark Guzdial twittered a link to an older post he wrote titled “Plea to Language Designers: Bring Back GoTo!” Wikipedia also covers the issue fairly well with a bunch of external links. So clearly there are multiple sides in this issue. I made the comment in Twitter that this issue is far from settled science.
By settled science I mean that there is still a lot of discussion going on. Arguably there are cases where GoTo statements have a place. One of the things that Mark Guzdial pointed out in twitter “When non-programmers define processes, they use goto's. GoTo is natural.” There is a lot of truth there. Of course one could argue either for or against what is “natural” and people do. Another thing to concider is how an understanding of GoTo helps with understanding other concepts. Ian Bogost (@ibogost)Twittered “GOTO made understanding machine branches easy.” Let’s face it most assembly languages could not function without the equivalent of a GoTo statement!
Most programming languages do include a GoTo statement of some form. Why? Well the discussion comes up every time and people ultimately decide to include it and yet at the same time hope people will not use it. Even the venerable Programming Proverb 7 says “Avoid unnecessary GOTO's” and not avoid all use of the GOTO. I suspect that this is a debate that will go on for ever. Not that there is anything wrong with that. I think I learn something new every time it comes up.
I don't have a problem with the Goto statement. Never have. It is the label that I don't like. If you are reading through a program to try and find out what it does and you come to a label this instantly destroys any continuity and context you have built up. You have to allow for the fact that there are now many more possible paths to that location. The orignal Basic, with a number for every line, was a nightmare in this respect.
In my opinion the only reason for a goto is when you need to get out of Dodge fast, because everything around you is falling apart. In the days before exceptions (which are really gotos with a nice suit on) I used to allow myself a couple of "get out of jail free" labels that I would put into a system so that if everything went wrong I could always jump to them and get things back to a known good state. setjmp in C was even better, because that used to put the stack back too.
For normal, run of the mill, programming I don't see any need to use a goto. But if the langauge doesn't support exceptions you have to treat them as a necessary evil for when stuff goes bad.