goto loops
-
Strange - when I post a comment along those lines, I get downvoted, but when you do it gets upvoted. Makes me think that some people here go more for the person than the message! Isn't the first time I have noticed that, though...
-
Strange - when I post a comment along those lines, I get downvoted, but when you do it gets upvoted. Makes me think that some people here go more for the person than the message! Isn't the first time I have noticed that, though...
I got 1-voted, and probably from the same person that 1-voted you. I edited it because I thought that's why I was 1-voted - I had neglected to mention the one valid
goto
everyone uses. Of course, the 1-voters can alsogoto
hell for all I care..45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I got 1-voted, and probably from the same person that 1-voted you. I edited it because I thought that's why I was 1-voted - I had neglected to mention the one valid
goto
everyone uses. Of course, the 1-voters can alsogoto
hell for all I care..45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I got 1-voted, and probably from the same person that 1-voted you. I edited it because I thought that's why I was 1-voted - I had neglected to mention the one valid
goto
everyone uses. Of course, the 1-voters can alsogoto
hell for all I care..45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Wasn't me even though I don't agree with you. But since you are so dead set I'm wrong, I challenge you: Show me ONE piece of code where a GoTo has a meaningful use!
I think the nested 'for' loop example is meaningful enough:
for (int i = 0; i < I_BOUND; i++) { for (int j = 0; j < J_BOUND; j++) { if (MyTestingFunction(i, j, k)) { goto LoopExit; } } } LoopExit:
Steve McConnell gives a few other examples here[^]. To be clear, I'm not saying there aren't other ways of doing this (as there clearly are), and I don't believe there are any situations where using a goto provides funtionality above and beyond other native constructs (though am willing to be proved wrong!). I just don't agree that you can categorically state that use of goto is *always* wrong.Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.
-
I think the nested 'for' loop example is meaningful enough:
for (int i = 0; i < I_BOUND; i++) { for (int j = 0; j < J_BOUND; j++) { if (MyTestingFunction(i, j, k)) { goto LoopExit; } } } LoopExit:
Steve McConnell gives a few other examples here[^]. To be clear, I'm not saying there aren't other ways of doing this (as there clearly are), and I don't believe there are any situations where using a goto provides funtionality above and beyond other native constructs (though am willing to be proved wrong!). I just don't agree that you can categorically state that use of goto is *always* wrong.Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.
It's probably easier, but I still don't think it's a good use in this case. Sort of like exiting a room through the window. I'm not an expert on this, but can you be sure that jumping out of a nested loop like that doesn't leave something not cleaned up properly, stack e.g.?
-
Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...
Never ever use goto!! You have been warned.[^] :)
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
-
It's probably easier, but I still don't think it's a good use in this case. Sort of like exiting a room through the window. I'm not an expert on this, but can you be sure that jumping out of a nested loop like that doesn't leave something not cleaned up properly, stack e.g.?
It depends on the context - if
MyTestingFunction()
is expensive to run then an intelligent way to exit the loop early is a pretty good reason, especially if the bounds of the data structure being traversed are very large. The variables in the loops are scoped by the braces so by the time the exit label is hit, the variables are out of scope and the GC can do its thing. Assuming thatMyTestingFunction()
is stateless and isn't dealing with unmanaged resources, I don't believe there's a memory leak issue here.Sarchasm : The gulf between the author of sarcastic wit and the person who doesn't get it.
-
Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...
I have used
goto
in C# when breaking out of nested loops, and rarely inswitch
blocks to avoid copying one case to the end of some other case (copypasta is worse thangoto
IMO). And I will hear no "nested loops should be refactored anyway" - it was performance code and performance code is not meant to look nice. In assembly I use the equivalent ofgoto
all the time. There is no alternative. -
What do you not understand about "Please do not post programming questions here"?
▬▬▬▬▬▬▬▬▬▬▬▬
It isn't a programming question. He's just asking for opinions about usage of a statement (warning: related to programming). Ya know? Other than important things like football, bullshit politics, stiupid dradunk bdahbble, cricket, etc., people should be let free to discuss something related to programming now and then.
It's time for a new sig. Seriously.
-
pompeyboy3 wrote:
to greater effect than other soloutions although I must confess I can see better ways to code some of the examples
to greater effect than the other solutions he provided, but not greater than real good solutions. As you started to point out, you can write his example in perfectly good code without requiring to the goto instruction. IMHO, this blog entry is rather poor.
There are always other solutions. The blog was pointing out the in rare circumstances goto is the best solution....if you define best as also the simplest solution. And yes, goto can be abused just like any other element in any language. I've seen multiple inheritance abused far more than goto's.
Steve Wellens
-
Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...
-
I have used
goto
in C# when breaking out of nested loops, and rarely inswitch
blocks to avoid copying one case to the end of some other case (copypasta is worse thangoto
IMO). And I will hear no "nested loops should be refactored anyway" - it was performance code and performance code is not meant to look nice. In assembly I use the equivalent ofgoto
all the time. There is no alternative.At the risk of being 1-voted to oblivion, I agree with you. If a GOTO will improve the performance OR readability of code, it should be used. I recently used a GOTO: 0. A 3D grid of objects, with coordinates expressed in 2 angular and 1 linear dimension. 1. Millions of these points in a DB, with random coordinates and local density. 2. Require to display one of these points in a 2D view, from a different origin, within the context of no less than, say, 50 surrounding points. 3. The DB contains only the raw data to compute the 3D coordinates. You can build the SQL to get a best bet of the points to analyze, but you have only a vague idea of how many of these will fit your parameters. The maths is complex, first to get the true coordinates, then to transpose to the observer position, and then to see if you have enough to give the context. If too many, fine, its dense. If too few, increase the radius of the search area by a percentage based on the shortfall. The kicker is, the original point must be in the centre of the search area, so any limit on the number of points in a loop may reach the limit in a small segment of the required circle, displacing the master point. I can assure anyone who questions this that a simple IF (not enough points) Calculate required increase to value GOTO startOfTheWholeShebangWithNewValue at the end of the code is both faster and more readable than any alternative. I am not going to post the code, and anyone familiar with coordinate transformations will know why. ;)
-
It's probably easier, but I still don't think it's a good use in this case. Sort of like exiting a room through the window. I'm not an expert on this, but can you be sure that jumping out of a nested loop like that doesn't leave something not cleaned up properly, stack e.g.?
Johnny J. wrote:
I'm not an expert on this, but can you be sure that jumping out of a nested loop like that doesn't leave something not cleaned up properly, stack e.g.?
Yes, you can be 100% sure, as long as you stay in the function, which every reasonable compiler enforces. The only way to mess this up is inline assembly to change the stack pointer, or inline assembly to force a cross-function jump to a function which expects a different amount of bytes for locals on the stack.
-
Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...
Those who know when to use a goto do not tell, those who tell do not know. :) I've used them before, in probably 3 places out of millions of lines of code. In those 3 places it was entirely appropriate and called for in my opinion and I didn't even bother to find any other way to do it. It's a feature of the language and when it's right to use it you just know. Avoiding using it because some people still harbor a hatred for old style basic programming is about as silly as anything else in this world.
Yesterday they said today was tomorrow but today they know better. - Poul Anderson
-
Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...
Yes, you can make spaghetti code with
goto
's. However, you can just as easily make spaghetti code with degenerate loops, loops with insane stop criteria and weird booleans which you are forced to use as replacement. Also, just because some theoreticians said "goto is bad because it messes with the provability of the code" doesn't meangoto
is actually bad. Who in their right mind actually proves that their code is correct? That's an insane amount of work, where you could just do some tests and "be reasonably sure" that it's fine. Chances are low that you're writing Mission Control code for an expensive space program. In other news, Knuth does not think thatgoto
is evil, and wrote the paper "Structured Programming with Goto Statements" (use google). -
It isn't a programming question. He's just asking for opinions about usage of a statement (warning: related to programming). Ya know? Other than important things like football, bullshit politics, stiupid dradunk bdahbble, cricket, etc., people should be let free to discuss something related to programming now and then.
It's time for a new sig. Seriously.
Why hate football posts? At least you are a sport man right?
-
At the risk of being 1-voted to oblivion, I agree with you. If a GOTO will improve the performance OR readability of code, it should be used. I recently used a GOTO: 0. A 3D grid of objects, with coordinates expressed in 2 angular and 1 linear dimension. 1. Millions of these points in a DB, with random coordinates and local density. 2. Require to display one of these points in a 2D view, from a different origin, within the context of no less than, say, 50 surrounding points. 3. The DB contains only the raw data to compute the 3D coordinates. You can build the SQL to get a best bet of the points to analyze, but you have only a vague idea of how many of these will fit your parameters. The maths is complex, first to get the true coordinates, then to transpose to the observer position, and then to see if you have enough to give the context. If too many, fine, its dense. If too few, increase the radius of the search area by a percentage based on the shortfall. The kicker is, the original point must be in the centre of the search area, so any limit on the number of points in a loop may reach the limit in a small segment of the required circle, displacing the master point. I can assure anyone who questions this that a simple IF (not enough points) Calculate required increase to value GOTO startOfTheWholeShebangWithNewValue at the end of the code is both faster and more readable than any alternative. I am not going to post the code, and anyone familiar with coordinate transformations will know why. ;)
Chris C-B wrote:
At the risk of being 1-voted to oblivion, I agree with you. If a GOTO will improve the performance OR readability of code, it should be used
If the statement is still there, ergo there must be a reason. "Don't use
goto
" is a good, quick rule for the newbie. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Do you use goto in C# ? Where do you find it usefull or not usefull ? i had never used goto in C# but now when i just saw that a collegue uses this very often, I start wondering.. :-? tell me...
Lucian-aSterX wrote:
Do you use goto in C# ?
I don't use C# any more, but my attitude about goto is simple: I have never used it before in C-like languages (assembly and Fortran are a different story) but if I ever find a situation when it is useful I'll use it without any second thoughts.
-
No serious programmer uses a
goto
. EDIT (After the 1 vote)---------------- Okay - maybe we all do - when wegoto
the bathroom....45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001modified on Monday, July 5, 2010 10:22 AM
Real Programmers aren't afraid to use GOTOs[^] Yet another proof that owning a gun doesn't make you a real man.