Original Post
Well, the title says it all I guess :)
in some languages you would be highly restricted if there weren't any "goto"s
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 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.
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;Because when he wrote that, this is what code looked like.
start:
if (i == 100) goto end;
i++;
goto start;
end:
;
And since people got creative, it went further:int i = 0;Oh, right. They didn't have functions either, so p was return address. So the above code would be used like this:
int j = 1;
start:
i++;
if (j %2&& j%3 == 0) goto phase 3
goto phase3;
phase2:
j++;
goto start:
phase3:
goto p;0x221: p = 0x223;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.
0x222: goto start:
0x223:
// and later
0x300: p = 0x302;
0x301: goto start:
0x302:
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
[quote name='zacaj' timestamp='1312665780' post='4845550']
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
switch(var)
{
case a:
case b:
case c:
some code...
}Isn't that more or less what exceptions are for?
int test (int i)
{
if(i)
goto ERROR;
return 1;
ERROR:
return 0;
}
int test (int i)
{
if(i)
throw(0);
return 1;
}
Actually in C#, I believe it is valid to use goto statements when dealing with switch statements that share the same code. In C# you cannot do
switch(var)
{
case a:
case b:
case c:
some code...
}
// you can do this
switch (x) {
case 1:
case 2:
case 3:
// stuff
break;
}
// but not this
switch(x) {
case 1:
//stuff
case 2:
// more stuff
case 3:
// even more stuff
break;
}
[quote name='ProgrammerZ' timestamp='1312669641' post='4845586']Isn't that more or less what exceptions are for?
int test (int i)
{
if(i)
goto ERROR;
return 1;
ERROR:
return 0;
}
int test (int i)
{
if(i)
throw(0);
return 1;
}
If you used a goto in production code I would fire you on the spot.
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.
[/quote]
Sorry, I should've clarified. I work currently in C# house. If you're using that language (as opposed to C/basic/etc where options are lessened) and still manage to mangle your design sufficiently that goto is your best option... and then proceed to use goto anyway instead of cleaning up your design; then you've shown yourself as someone not to trust in the codebase.
The same thing would happen if you made a 2000 line method by hand or a class with 100 public parameters. They're not dogma, they're definitive signals that either you don't care or you're a bad programmer.
for (int i=0; i<n; ++i) {
switch (some_array) {
case 0:
//...
break;
case 1:
if (some_condition())
goto DONE; // Break out of the loop, in a situation where `break' would just break out of the switch block.
break;
//...
}
}
DONE:
//...
I guess I am a heretic, but I use goto in other situations that haven't been mentioned here. The most common is something like this:
for (int i=0; i<n; ++i) {
switch (some_array) {
case 0:
//...
break;
case 1:
if (some_condition())
i=n;// DONE!!!
break;
//...
}
}
//...
This topic has been locked by a moderator. New replies are not allowed.
GameDev.net uses cookies to ensure you have the best experience on our platform. Learn more