goto loops
-
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.
-
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 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...
Plenty of programming books I've read have always stated that it is best to avoid the GoTo statement because it increases the chances of producing 'spagetti' code. Having said that, I first cut my programming teeth on assembly language; and you just couldn't avoid the use of GoTo (Jmp) statement in assembly.
I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image. Stephen Hawking
-
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...
Agreed that goto's should be avoided, and I dislike that they are not navigable in C#. However contrary to popular belief there are situations in which you are required to start coding before you actually know what the system architecture should look like. AN EXAMPLE: in situations where you have VERY complicated logic, say 100 's of variable combinations where each combination requires a slightly different approach yet they all implement the same code, you could permute out all the variants, and make a block for each (Terror to debug) if you categorized something wrong, or you can work your categorization into the code and call each functional block only once... this latter method, works very well in pure research of physical simulations, where you want to have a better understanding of the underlying properties (you can extract the characteristics of the system from your code)... a very specific example.. but... This is how your encouraged to solve problems in many graduate physics Biology and chemistry courses that focus on devising rules for observable properties, or devising qualitative groupings, of things which don't behave well with normal statistics... (categorical Bayesian data regression in degenerate systems is not very fun!)
I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...
-
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:
i just saw that a collegue uses
How old is your colleague? When I first learned to program, a "GOTO/JUMP" was the only way to break a loop before completion or to branch (i.e. early Fortran, original BASIC & 6502 assembly, etc). And I still tend to think in DO/FOR loops and GOTO line number. But I try to avoid using line numbers these days. We didn't have these modern languages like C and still used punched cards and paper tape (I am must be seriously old!).
Melting Away www.deals-house.com www.innovative--concepts.com
-
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 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...
It seems to me, the GOTO statement goes back to the original Basic Programming of the early seventies, where it had really valid uses. Because resources, especially memory, was so scarce, you used less precious bytes when you used a GOTO instead of a FOR loop. Nowadays, it's more used for getting out of some sticky situations such as multiple nested loops or to get around such things. It's much more elegant and sound to set a boolean flag and just check it at every step out of the structure. What do you guys think?
-
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 just got through reading all the comments (as of the time I'm posting). Here's my 2 cents. I skipped formal religious training (ie degree in computer science from university -- taught by monks who are constrained to teach the latest C.S. fad or risk losing their jobs) and instead "squandered" my time at university studying math and science (my parents were so ashamed :(( ). Anyway, because of my secular education, I tend to be mostly agnostic when it comes to religious wars on subjects like: - use of goto - use of macros - procedural versus Object Oriented, versus Aspect Oriented programming - which language (Python, Java, C#, etc.) the angels in heaven speak - which language (Cobol, Fortran, Algol, etc.) the demons in hell speak. I once heard a man say to his deeply religous mother, "But mom, even Jesus drank wine", to which she responded "Yes, but I'd have thought better of Him if He hadn't!" Perhaps there are just times when the angels, in order to be understood by us mere mortals, stoop to vile "hell-speak" and use goto. At which point, we would have thought better of them if they hadn't... ;)
David --------- Empirical studies indicate that 20% of the people drink 80% of the beer. With C++ developers, the rule is that 80% of the developers understand at most 20% of the language. It is not the same 20% for different people, so don't count on them to understand each other's code. http://yosefk.com/c++fqa/picture.html#fqa-6.6 ---------
-
Lucian-aSterX wrote:
i just saw that a collegue uses
How old is your colleague? When I first learned to program, a "GOTO/JUMP" was the only way to break a loop before completion or to branch (i.e. early Fortran, original BASIC & 6502 assembly, etc). And I still tend to think in DO/FOR loops and GOTO line number. But I try to avoid using line numbers these days. We didn't have these modern languages like C and still used punched cards and paper tape (I am must be seriously old!).
Melting Away www.deals-house.com www.innovative--concepts.com
Snowman58 wrote:
How old is your colleague?
My collegue is about ~35 years old... but i don't think tha using goto has a lot to do with age, because i remember when i was in college (~2 years ago) the programming teacher (C#) told us about goto loops and teached us how to use this. I have to admit that he wasn't so good at programming, anyway... this may be the reason...