The EVIL goto...

Started by
27 comments, last by Voodoo4 23 years, 8 months ago
On the web there are many resources on c and c++ and many good tutorials. In these tutorials you get good reference on c's basic statements like if,do-while,switch,etc. But when it comes to the goto statement everyone is treating it as the spawn of evil,the satan's child! This is also happening in books of serious authors and it is really sad. Everyone wants to make clear that "a good programmer should never use the goto!" . Well ok,i agree.You can write your code again without the use of goto,but since it exists why should you ignore it? Why then the compiler authors don't exclude it from their c version? The goto statement is very usefull when you have to deal with deep nested loops.Goto-Jumping may save you from lots of typing. Saying that goto should be eliminated i believe is narrow minded thinking.If there's something to eliminate that is unreasonable use of it. If you can avoid it then avoid it. I have never understood this "gotoless" programming frenzy really.They say it makes the code a mess. Well if you're not a neat programmer i think this isn't goto's fault! Voodoo4 Edited by - Voodoo4 on 7/23/00 1:16:13 PM Edited by - Voodoo4 on 7/23/00 1:16:57 PM
Here these words vilifiers and pretenders, please let me die in solitude...
Advertisement
Its very simple, goto is evil.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
I''ve never used goto before (but I haven''t coded much yet ) but I don''t look at it as an evil statement. Offcourse, (almost?) everything can be done without goto (and also with goto ). I''ve seen enough code using goto where it was the best way to use goto, so yes, goto has a good use. I will use it when I feel I need to use it.

-------------------------
-Programmers don't byte, they nibble a bit.
Unknown Person
-------------------------
--------------------------Programmers don't byte, they nibble a bit. Unknown Person-------------------------

The reason goto was included is for those situations in which you absolutely have to have it, and since C is a systems programming language, there are instances when writing say an OS kernel in which goto has its uses.


goto is not evil in and of itself, but as a programmer who spent a few years(in 80''s) writing BASIC code, I will say that goto''s produce code which is unreadable after a certain size. Gosub(from BASIC) isn''t much better. Functions accomplish many of the same purposes, but encourage reuse and reduce causes for errors(like inadvertently changing a variable used by the code the goto''d the current code). if''s/ do''s/ while''s/ for''s/ function''s/ and switch''s will accomplish everything goto does but will make your life easier in 3 months when you come back to look at it. This is experience talking, not computer science, and just be glad you don''t have to use line numbers(one renum and you''re spending HOURS figuring out where sections of code are).

Well, in my experience I''ve only ever used goto once (ever since I stopped programming in BASIC on my TI-99-4A back when I was 7). It was during the ECOO team programming competition 2 years ago. It was with 3 minutes left in the competition that we realized a bug in our code. We needed an exit condition in the middle of our 15 nested for loops. Now, the only reason that 15 nested for loops even cropped up is because we were programming under a deadline, and a very tight one at that. The solution sucked, but it worked. So anyways, as opposed to coding in 15 extra conditions, we used Pascal''s goto statement. It got the job done.

My CS prof hit me when I told him.

Essentially, people are so opposed to goto because there really is no reason for it to be used. You can do anything you want with other constructs, particularily functions. If you have a bunch of nested loops or something, look for a cleaner way to cut out of them. You''ll love yourself later.
==============="Or a pointed stick!"
The goto statement is not evil.

It has the potential to be abused much more than other statements; by inexperienced programmers and those who come from BASIC (or similar) backgrounds, goto does get abused.

But the occassional and proper use of goto can make code readable and easy to maintain over using other sorts of control structures.

However, I find the need to use goto is very infrequent. Also keep in mind that while every ANSI C conforming compiler supports it, not all support it well. Because of the bad rap it has gotten and the infrequency of its use, various compilers will either botch the generated code or optimize it poorly. That in itself may be a reason to avoid or minimize using goto .

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
quote:Original post by mossmoss

...those who come from BASIC (or similar) backgrounds, goto does get abused.


yep, that''s the case. We don''t want to show that we started with *laaaaame* basic

pi~


btw what''s a blue programmer?

Jan PieczkowskiBrainwave Studios
It is my understanging that the use of goto can be bad in some situations due to undefined behavior. (goto into an if, for loop, or something else)
Because it can be bad and isn''t absolutely necessary to programming, most people thing it is a bad idea.
"goto is EVIL!!!" they say. Has anybody noticed that exception handling (try/catch/throw statements)--and ''throw'' in particular--are really stylized goto statements? Stick a label where the catch() is, change ''throw'' to goto and it works exactly the same.

So next time someone tells you that goto is Evil(TM), ask them if they approve of exception handling. If they say yes, just sit back and smile.

Oh, and if that isn''t convincing enough, answer this: what is the one statement in C or any other language that compiles to a *SINGLE* instruction on just about every chip known to mankind? Call it JMP, BRA, or whatever, it''s still GOTO to me

--Capt. Mathew Neville
Well, exception handling does a LOT more than goto, but point is well taken. =)

IMHO, goto is one of those tools that is more liable to do damage than good. New programmers should stay away from goto while they get experience. Then once you have a good understanding of real world programming, then goto can be used effectively.

I once was talking to someone who had a masters in music. He said that when getting your bachelors (sp?), you learn all the rules to making music. When getting your masters, you learn how to break those rules. I think of goto the same way, you must first learn the rules of structured programming before you can really understand when to use goto.

Tim

This topic is closed to new replies.

Advertisement