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
Real Programmers aren't afraid to use GOTOs[^] Yet another proof that owning a gun doesn't make you a real man.
-
Why hate football posts? At least you are a sport man right?
I never said I hate football - I just don't care about it enough to hate it. And at the same time I don't mind people discussing about it. All I'm asking for is a little space for people to discuss about something that's completely valid and on topic. That's programming.
It's time for a new sig. Seriously.
-
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).harold aptroot wrote:
provability of the code
:laugh: There's a great Joel 's article on the subject. Talk at Yale: Part 1 of 3[^] And when all was said and done, she got to the end of the proof, and somehow was getting exactly the opposite result of the one that made sense, until that same graduate student pointed out where, 63 steps earlier, some bit had been accidentally flipped due to a little bit of dirt on the board, and all was well.
-
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 used one a couple of weeks ago. I could have used some other mechanism but it was the perfect tool for that particular job, anything else would have added unecessary complexity and would probably have been optimised away into the same IL anyway. It's all part of being a programmer in my opinion - selecting the right tool [keyword]. Having said that, it's the first time I've used it in years!
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
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
John C wrote:
Those who know when to use a goto do not tell, those who tell do not know
A bit like teenage sex?
-
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 am not religious about not using goto, I'd use it if it were ever truly useful, above and beyond the spagetti effect it creates. I've never once used it in C, C++, or C#.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Funny how no-one even the comments could come up with a really good alternative (I agree for simplicity, the && is approach is best). How would I do it?
bool Do(params Func<bool>[] args)
{
foreach (var a in args)
if (!a())
return false;
return true;
}
...if (Do(Step1, Step2, Step3, Step4, Step5, Step6, Step7))
Console.WriteLine("Brillant!");xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Editionleppie wrote:
if (Do(Step1, Step2, Step3, Step4, Step5, Step6, Step7)) Console.WriteLine("Brillant!");
And how do you put a breakpoint on Step3? If you can't put a breakpoint on a line of code, you lose maintainability.
Steve Wellens
-
leppie wrote:
if (Do(Step1, Step2, Step3, Step4, Step5, Step6, Step7)) Console.WriteLine("Brillant!");
And how do you put a breakpoint on Step3? If you can't put a breakpoint on a line of code, you lose maintainability.
Steve Wellens
Steve Wellens wrote:
And how do you put a breakpoint on Step3?
Either in Step3, or in the Do method...
Steve Wellens wrote:
If you can't put a breakpoint on a line of code, you lose maintainability.
BS!
xacc.ide
IronScheme - 1.0 RC 1 - out now!
((λ (x) `(,x ',x)) '(λ (x) `(,x ',x))) The Scheme Programming Language – Fourth Edition -
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...
Despite all preaching against them, gotos are nothing bad or evil. They just should be used sparingly and with consideration. When you have some deeply nested (spaghetti) code, a goto may be the only way to get out without making the whole thing even more complicated. Indeed you might also see a break as a goto with an implicit destination. Refactoring the code into separate methods may be a far better choice most of the time, but sometimes you need such a spaghetti monster and then gotos may be needed. But if somebody needs them regularily, that person should work a little on the coding style.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
-
OK - to clarify. What you have asked here is more of a programming philosophy question than a programming question. Others ask these in the Lounge and get away with them so I don't see why you shouldn't. I have never encountered a situation in .NET where a goto makes my job easier. Generally, a well structured application with small, self-contained methods should have no need for a goto. When I see them, it's generally an indication that somebody has landed themselves in an architectural muddle and they can't see the way out of the morass. This is generally when they have loops nested within loops nested within loops. The way I tend to look at this, if you've got yourself this deep into loopfuggery, it's an indication that you are using the wrong loops.
"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.
If you look at oldfashioned code you will see that it generally had a considerably smaller memory footprint and executed faster (in comparison). Good design obviously costs memory and some efficiency. With today's best practices one would not rally get far on a 8 bit CPU and 4k RAM. I don't rerally like the 'we have plenty of everything' attitude, however, only small and selected regions of the code need such a performance treatment and should then be well commented.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
-
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).I second that. Most of the people that preach that goto is evil are the same ones over engineering their code into a mess no human can understand. Some professor told them that goto is bad so it has to be true. Now they have religion and everything is black and white. It's funny that multiple inheritance can be way worse but doesn't get anywhere near the bad press that goto does. I had an engineer tell me recently assembly language was unnecessary. Obviously he has never too done much embedded work on a little 8051. He also told me doing CORBA over a CANBus for a UI was practical. And of course he has given me the goto speech in the past. Should I believe him? Bottom line - it's more about the programmer than the language constructs.
-
:-D This is a discussion and I'm not claiming a try-finally block would be a better solution.
Pete
-
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...
While I understand that sometimes a goto statement can solve a specific issue, I do not use it since the last time I programmed in GWBasic on my Sinclair 1000 computer (yes I am that old but still kicking). I believe there are always other better solutions than a GOTO and since I do not write OS code or device drivers, I do not need the extra performance that it can (sometimes) provide. IMHO, I believe that using GOTO in business/web application is an easy way out of a problem and its use should be discouraged. Train your brain into finding better solutions. Its much more fun.
Pierre Boucher 'Bien souvent on se rend coupable en négligeant d'agir, et non pas seulement en agissant.' - Marc Aurèle, empereur et philosophe romain.
-
Despite all preaching against them, gotos are nothing bad or evil. They just should be used sparingly and with consideration. When you have some deeply nested (spaghetti) code, a goto may be the only way to get out without making the whole thing even more complicated. Indeed you might also see a break as a goto with an implicit destination. Refactoring the code into separate methods may be a far better choice most of the time, but sometimes you need such a spaghetti monster and then gotos may be needed. But if somebody needs them regularily, that person should work a little on the coding style.
A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.
I completely agree with you. Sometimes the use of GOTO in the first place might be one of the cause of the spaghetti code.
Pierre Boucher 'Bien souvent on se rend coupable en négligeant d'agir, et non pas seulement en agissant.' - Marc Aurèle, empereur et philosophe romain.
-
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 second that. Most of the people that preach that goto is evil are the same ones over engineering their code into a mess no human can understand. Some professor told them that goto is bad so it has to be true. Now they have religion and everything is black and white. It's funny that multiple inheritance can be way worse but doesn't get anywhere near the bad press that goto does. I had an engineer tell me recently assembly language was unnecessary. Obviously he has never too done much embedded work on a little 8051. He also told me doing CORBA over a CANBus for a UI was practical. And of course he has given me the goto speech in the past. Should I believe him? Bottom line - it's more about the programmer than the language constructs.
Reminds me somehow of the people who keep insisting that "you can not beat the compiler" even though I keep proving them wrong :) They have a point that compiler could be smart, but in practice most aren't. Especially the ones for embedded systems.. they are godaweful, and IME most of the time you just can't afford to waste any performance/storage on crappy compiler output, they are scarce enough already.
-
In my Comp Sci 201 class - object-oriented programming, first semester of college, we were given an F for ever using a goto statement.
-
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...
Do you like spaghetti? If you use goto expect to get spaghetti code out of it. Following one line of code and having to jump to another point is not fun. When you are doing procedural code (like Qbasic) it was needed but now that you have functions and such it is a bad idea.
-
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...
When all your .Net code is compiled to assembly code, all for's, while's, switches and if's will eventually become goto's (actually conditional "jumps"). Aside from that, the use of goto's just tells me bad design and coding, I've been programming for a while and never used goto's (despite the temptation in some situations). No situation really need goto's, they just need better design.
-
I read that as "he has a .45 inch reach" and felt sorry for him for a moment.