Why goto statement should be avoided




















If you don't explain it, others will get frustrated trying to figure it out. And no one will want to read your code. No one will want to play your weird game with all of the house rules. We don't care if you think your game is good, it takes too long to learn to play, and no one else understands it. The bottom line is that if you use goto statements, real programmers, skilled and practiced professionals, who are worthwhile to associate with, will not want to read your code.

And many of them will not want to associate with you because of that, at least professionally. If you insist on writing bad code, there are plenty of ways to keep doing it without goto ;. Last edited on Feb 11, at am UTC. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article. A better solution: Why not just move the looping code into its own subroutine? You may need to pass a handful of arguments, such as the contents of the loop, but it makes the code simpler.

Now, to exit the loops, simply return from the subroutine. A common use of GOTO is to perform a "common exit," or in other words always call a certain section of code before returning from a routine. Perhaps this is done to reset a variable or to close any file units opened in the routine. Maybe it does some other kind of cleanup before returning from the routine. An option for how to not only perform common cleanup after exiting, but to guarantee that the cleanup will always be done is to create an "in-scope" object, which is cleaned up by IDL's automatic garbage collection.

Basically, the idea is to put any cleanup code in a temporary object's ::Cleanup method. When exiting the routine, the object falls out of scope, and once the object falls out of scope, IDL will automatically destroy, or "garbage collect," the object.

When doing so, the Cleanup method automatically gets called. I wrote an IDL Data Point blog post last year describing the in-scope object, which can be found here. I hadn't thought of that. Actually, with the goto he used to goto a subroutine of some sort , raptors should attack him. There are more implications, and you are right to mention it. Show 2 more comments. Did you google the issue? The founder of the anti-goto movement is Edsger Dijskstra with his legendary "Goto Considered Harmful" To get you started you can goto ha ha!

John Smith John Smith Ironically, he actually tolerated far more GOTO than programmers today. Ole V. Nikolai Fetissov Nikolai Fetissov Apparently the author of the question is unaware why "goto" is available in C, too.

And I still don't get what you are so upset about. Hey, thanks, I didn't know there was russian mob around here : — Nikolai Fetissov. The Overflow Blog. Podcast Explaining the semiconductor shortage, and how it might end. Does ES6 make JavaScript frameworks obsolete? Featured on Meta. Now live: A fully responsive profile. Linked See more linked questions. Related Hot Network Questions. Stack Overflow works best with JavaScript enabled.



0コメント

  • 1000 / 1000