To goto or not to goto?

Started by
92 comments, last by godplusplus 12 years, 7 months ago
Well, the title says it all I guess :)
Advertisement
I find them useful to do cleanup in functions that can fail, so I dont have to free all the resources everywhere, I can just goto the end, and put all the frees there
I never use goto statements. It's generally unsafe to change the flow of an application in this way. Almost in every situation there are more suitable techniques that are safer, and which won't create spaghetti.
I think you need to define the context of this question; in some languages you would be highly restricted if there weren't any "goto"s. Making some assumptions, I think Dijkstra's opinion on this matter might be an interesting read ("Edgar Dijkstra: Go To Statement Considered Harmful" http://wwwens.uqac.c...jkstra_Goto.pdf)

I'm curious about the intent behind the poll in this thread; are you meaning to form an opinion based on the general consensus?
I have about 0...2 gotos per project. I have tiny to small projects. Very-very sometimes, they are simpler and neater than avoiding them at all costs.
I've used one goto in the say... 18 years I've been programming. It was a throwaway test app that I had to trigger cleanup code before exiting the function and had done it so slapdash that a finally block wouldn't deal with it; and it amused me to write such horrible code.

If you used a goto in production code I would fire you on the spot.
I use [font="Courier New"]goto[/font]. Quite a bit.

I use it exclusively in object-oriented C programming, because C lacks useful constructs such as RAII and exceptions. It is better to have a forward-jumping goto into a sequence of rollback operations as you initialize members on your constructor code than any other way of handling failures. The use of [font="Courier New"]goto[/font] gives clarity and structure where no other construct can.

For those of you who are too young to understand what Dijkstra wrote, his poins was that alternatives to [font="Courier New"]goto[/font] should be emphasized and chosen in many situations. At the time, such as when I was a student, much code was written in non-structured langauges like FORTRAN IV or BASIC, and the use of [font="Courier New"]goto[/font] was required by the language itself. Most programs were spaghetti code. Dijkstra was involved in designing a new language, one that could be used to teach and to reason about computing -- he was not advocating a dogmatic methodology for future generations.

When a dogmatic anti-goto zealot tells you never to use that control construct, stop and ask for a clarification on the reasons why. If he or she can not tell you any reason other than the ipso dixit of "Dr. Dijkstra wrote a paper saying it was harmful," ignore them and any other advice they give.

Stephen M. Webb
Professional Free Software Developer

in some languages you would be highly restricted if there weren't any "goto"s

I see your point.

I'm curious about the intent behind the poll in this thread; are you meaning to form an opinion based on the general consensus?

The reason I made this poll is because i personally use them exactly this way:

I find them useful to do cleanup in functions that can fail, so I dont have to free all the resources everywhere, I can just goto the end, and put all the frees there

... and I got curious about whether people in general avoid them at all cost, or if there were indeed others who think they are the right solution in some (special) situations.

I never use goto statements. It's generally unsafe to change the flow of an application in this way. Almost in every situation there are more suitable techniques that are safer, and which won't create spaghetti.


Which is a pointless statement. Unless one writes function bodies which are hundreds of lines long, which is spaghetti code anyway, then goto in any today's language isn't capable of creating such mess anymore.

THIS is spaghetti code referred to by the article and something that isn't realistically possible today. Any compiled code generated by compilers or VMs today looks exactly the same, there is just million times more of it. It wasn't sloppy programming - there just aren't any functions, labels, namespaces, classes or anything. Hence "structured programming" was considered an advancement.

If you used a goto in production code I would fire you on the spot.[/quote]

Which is fine, since I don't want to belong to religious organizations based around dogmas conjured on word-of-mouth lore.

I can't imagine what would happen if someone dared overload an operator.

----
Djikstra has the misfortune of being misquoted ever since he wrote the article. But the context in which was written was different and reasons were long lost by many generations that came in between. He proposed that goto functionality be replaced with higher semantic constructs, such as for and while loops. And later, functions... The horror....

So in that context the question would be: do you ever use goto instead of a for loop?int i = 0;
start:
if (i == 100) goto end;
i++;
goto start;
end:
;
Because when he wrote that, this is what code looked like.

And since people got creative, it went further:int i = 0;
int j = 1;
start:
i++;
if (j %2&& j%3 == 0) goto phase 3
goto phase3;
phase2:
j++;
goto start:
phase3:
goto p;
Oh, right. They didn't have functions either, so p was return address. So the above code would be used like this:0x221: p = 0x223;
0x222: goto start:
0x223:
// and later
0x300: p = 0x302;
0x301: goto start:
0x302:
Pray that you counted the bytes in advance correctly, since inserting a line and running out of space might mean renumber all jumps in code below.

BASIC was really great high level language which had gosub that allowed prameterless function calling as well as looping constructs, thereby eliminating the need for such code. But as gurus would say, a lot of optimization opportunities were lost due to these high-level languages (true to an extent).

BASIC still had numbering problem, so you'd start writing the program with each line as multiple of 10. When new stuff was inserted, you'd stuff it in between, maybe as 15 or 25. And if you ran out and had to renumber, gosubs were suddenly broken since they relied on line numbers. So functions were usually put at some high number, such as 2000, 2200, 2500, leaving enough room in between.

In C, goto can be used idiomatically as error handler.
In C++ there is not much use that cannot typically be replaced using either break or continue, both available in Java as well. It doesn't cause problems with auto-allocation either, compared to setjmp/longjmp which is more commonly found in C.
Java does define goto keyword but it's not implemented.
PHP recently added it, most other languages do not have it.

I find them useful to do cleanup in functions that can fail, so I dont have to free all the resources everywhere, I can just goto the end, and put all the frees there


Isn't that more or less what exceptions are for?

This topic is closed to new replies.

Advertisement