Programming Proverbs 7: Avoid unnecessary GOTO's

Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

Programming Proverbs 7: Avoid unnecessary GOTO's

  • Comments 1

This was quite the controversy when it all started. Today some people go through a whole programming course without even learning that the language they are using even has a GOTO statement. An unconditionally GOTO statement barely exists in most programs today. It wasn't that way 40 years ago though.

Edsger W. Dijkstra really brought a lot of clout to the discussion with his classic paper "Go To Statement Considered Harmful." Dijkstra  actually argued that the statement should be removed for modern programming languages. Every language design group since then has had that as part of there design discussion.

There isn't a lot of reason for unconditional GOTO statements these days. We've got great ways to organize code using loops, tight little module, class methods and many more. In fact today I'd say you'd be hard pressed to define a section of code that requires a GOTO statement for efficient code.

There used to be one time when a GOTO did (more or less) serve a good purpose. Older versions of BASIC used an ON ERROR GOTO statement for error handling. Generally the error handling code was placed at the end of the module. The last line before that code would be a GOTO that sent the code to the final END statement. Each section of error handler would include a GOTO at the end to also send execution to the final END statement. The idea was to have a single point of exit for any routine. That's still a good idea but requiring brute force methods to get there are not always a good idea.

Today a lot of teachers debate the idea of teaching their students about the GOTO statement. I pretty much avoided it most of the time. Sometimes I talked about it early in the course. I think it was valuable to discuss the ways GOTO was used and what problems it cost. Hiding things seems like a bad idea What do you think?

This is the seventh of a series of posts based on the book Programming Proverbs by Henry Ledgard. The index for the series is an earlier post and discussion of the list as a whole is taking place in the comments there. Comments on this "proverb" are of course very welcome here.

Technorati tags: