goto loops
-
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
-
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
John Simmons / outlaw programmer wrote:
No serious programmer uses a goto.
The whimsical ones do though :-D
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
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!
Now your just setting him up to post code in the lounge and getting him flamed for doing so. I'm on to you ;P
-
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...
You earn your upvote rights by scaring people. ;)
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
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!
10 Print "This Code is useless."
20 Goto 10"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
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...
Well if you must know I up voted him for the remark about the 1 vote and the goto the bathroom part. Could be that my sense of humor is a bit off today but I thought it was funny so...
-
10 Print "This Code is useless."
20 Goto 10"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
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. ;)