Saturday, September 19, 2009

Goto Shmoto

C++ was my first programming language. When I was going through school I can remember clearly the day that the keyword "goto" came up. The teacher said something along the lines of, "goto will jump to another section of code...just don't use it." At the time I had enough information I was trying to retain so that sounded good to me, just don't use it. My career continued, I never used goto, but it would come up from time to time. While working at my job, I came across code that used it. I just assumed no one told these programmers not to use it, but couldn't help but wonder, "How did goto get such a bad rap?"

I mean it is pretty simple to understand why. If the code you're trying to walk through is jumping all over the place it can make it a bit more cryptic. When you are reading you literature of choice the author doesn't (usually) have a "goto page12Paragraph3." Some may say that would get confusing.

Still though, why was it created in the first place? Almost all programming languages have some form of a goto present in their make-up. Yet a lot of the coding standards I've seen specifically say to not use goto. There had to be a good way to use it. I could not help but think that people just did not know how to use it properly.

So I became fed up with this lately and had to do some research. It seems my first thoughts were not too far off. This topic has been controversial for 30+ years. The first pieces of literature on the this topic are from two greats, Donald Knuth and Edsger Dijkstra.

The first paper I read was this: "Go To Considered Harmful". This was written by Mr. Dijkstra. In short it explains that the programs he saw that chose to use more structured ways of handling logic over gotos were of higher quality. Cool, so this problem was solved 40 years ago...well in a way. This is controversial so there must be another side to this story.

The second paper I read was from Mr. Knuth, "Structured Programming with go to Statements". From the title you can guess that this is about using goto for structured programming. A lot of people thought from the title that it was going to be pro goto, but it was something else. From reading the text you find that he agrees a lot with the movement away from goto in favour of more structured programing. It is a pretty lengthy paper that goes over hard examples of when goto can be more elegant/optimal and when it can be obscure. He also stresses that the real problem with bad code is with its structure and that removing gotos does not solve the problem completely. Code that does not use gotos can be just as bad as those that do use them if they are structured obscurely.

From this research I have come to the conclusion that I will continue to not use it in favour of other language tools and paradigms at my disposal. Things like event systems, exception protocol, and standard loops provide a more organized version of gotos. The effect of the goto is the basis of a lot of operations and code structure that we have today and for that I have a new respect for the keyword. I now think that it could be a very useful tool for making optimizations, but I won't stop from questioning if adding in a goto will really help the performance in the end.

Oh yeah if you want a sweet shirt : Knuth Shirt

No comments:

Post a Comment