Fog Creek Software
Discussion Board




GOTO's Considered Harmful

The entire "GOTO statement considered harmful" is quite a myth. Granted, Dijkstra wrote an article with this title, and many people agreed with him. But, on the other hand, some people have demonstrated that it is possible to do structured programming with goto statements, and sometimes even better with them than without them.

For example, Don Knuth wrote an article "Structured Programming Using Goto Statements" (rumouredly sub-titled "'Goto Statement Considered Harmful' Considered Harmful") in which he deomnstrated exactly that. And I take this view as well.

Don't get me wrong, I don't advocate a goto-spaghetti, like I once saw in old BASIC programs. But goto's have their use.

When programming in C, I find myself using goto's whenever I feel the need to. This occurs less in Perl where I have other powerful constructs.

Shlomi Fish
Wednesday, October 15, 2003

Off course, GOTO is inocent victim... Dijkstra wrote his article in 196x, when predominant comercial languages were FORTRAN and COBOL.

At that time, programmers used GOTO to simulate various control constructs we now take for granted (while, do...while), which tended to go out of control -- programmers used GOTO to reuse piece of code without resorting to functions/procedures.

Today, I program in C++ and I am using goto only to simulate finally construct...

Srdjan
Wednesday, October 15, 2003

"Today, I program in C++ and I am using goto only to simulate finally construct... "

I've never thought of using a goto that way before.  However, it seems like a good idea.

What is the scope of goto labels?  Do they have procedure scope?  (ie can you name all labels "finally:")

Almost Anonymous
Wednesday, October 15, 2003

The whole 'finally' concept in C++ is useless and actually is harmful. You should use destructors to properly release the resources.

Passater
Wednesday, October 15, 2003

The idea of not using GOTO is like not using global variables, it seems to me. It's easier to stay out of the habit, rather than decide in each case whether it's ok or not.

The Real PC
Wednesday, October 15, 2003

I've be coding in C and C++ for 20+ years. I've never once even considered using a goto. Just never saw the need....

sgf
Wednesday, October 15, 2003

ALmost,

They have the scope of the block they appear in.

For a finally clause in C, I now use the do { } while(false) construct.

Dennis Atkins
Wednesday, October 15, 2003

Oops, it's procedure not block. Sorry.

Dennis Atkins
Wednesday, October 15, 2003

Global variables are a good thing when properly used.
Module (static globals of file scope) variables are a good thing even more often.

Dennis Atkins
Wednesday, October 15, 2003

I went with the do while false construct for a while.
But it breaks horribly with actual nested while loops, etc.

Generally, one goto is OK. That is, there is one label you're allowed to use:

exit: (or end or finally or returnpoint or whatever else you want to call it)

which is the finally construct, you then have code like
if (S_OK != SomeCOMCall()) goto exit;

Every once in a blue moon a slightly different goto can be useful.

mb
Wednesday, October 15, 2003

Soo.. whats worse, goto exit or multiple return points in a method :-)

Anu
Wednesday, October 15, 2003

In fact, Dijkstra's original title for the paper was "A case against the goto statement". The title "Goto considered harmful" was substituted by the editor, Niklaus Wirth:

http://www.cs.utexas.edu/users/EWD/ewd13xx/EWD1308.pdf

I vaguely recall from the essay by Knuth some quoted remarks by Dijkstra to the effect that his postition on the goto statement was not as extreme as people thought, and that he did not violently disagree with Knuth on the matter. I don't have the paper to hand and would have to check.

as
Wednesday, October 15, 2003


Hell, ANY keyword in ANY language can be considered harmful if used improperly. And that includes BASIC's Rem keyword.

anon
Thursday, October 16, 2003

I have seen a quote from Dijkstra about BASIC which I keep with me all the time.

Using BASIC damages the mind, therefore its teaching should be a criminal offence.

I have not used goto's for a great many years. But I do agree that goto's in most modern languages aren't as nasty since they have limited scope, usually to the current function.

The problem was with older languages, for example old BASIC dialects (no block structure, line numbers not labels, etc) where the control structures were lacking and the scope of goto's was the whole source file. Under these circumstances it was difficult not to use them because of the brokeness of the language.

It was in these former times that Dijkstra published his famous paper about goto's, the idea of structured programming was still a novelty and code from those days was much more spaghetti-like than would be acceptable today.

whattimeisiteccles
Thursday, October 16, 2003

whattimeisiteccles writes, "It was in these former times that Dijkstra published his famous paper about goto's, the idea of structured programming was still a novelty and code from those days was much more spaghetti-like than would be acceptable today."

Excellent point - one must consider the circumstances surrounding the writting to understand its intended meaning.

bpd
Thursday, October 16, 2003

Re: BASIC. BASIC was my first language. However, the books from which I learned it taught me to use it with structured programming in mind. They taught me to think
in loops, conditionals, routines, etc.

Afterwards, the transition to C was quite easy in this sense. I do met, however, of someone who first learned BASIC and Assembler, and said they were the "natural way a human thinks like". He also said, that it took him a lot of time to get used to Pascal.

Like I said, I've seen some goto-spagheti code and it was not a pretty sight.

Shlomi Fish
Friday, October 17, 2003

Back in my C days, I used an occasional forward goto, which isn't nearly so harmful as a backwards goto. They helped me avoid multiple nested braces, which are also considered harmful.

Now, having read Martin Fowler's Refactoring book, it's clear that writing smaller functions or methods is a better way of avoiding multiple nested braces. Of course, in Java gotos aren't available and I don't miss them.

Julian
Saturday, October 18, 2003

*  Recent Topics

*  Fog Creek Home