Why is goto evil




















Exiting nested loops If you're in a nested loop and need to break out of all loops, a goto can make this much cleaner and simpler than break statements and if-checks. Low-level performance improvements This is only valid in perf-critical code, but goto statements execute very quickly and can give you a boost when moving through a function.

Improve this answer. Community Bot 1. The first problem is solved very neatly by finally blocks in modern languages, and the second is solved by labelled break s. If you're stuck with C however, goto is pretty much the only way of solving these problems elegantly. Very good points. I like how Java allows breaking out of multiple levels of loops — Casebash.

Chinmay - finally blocks only apply to 1 modern languages that have them and b you can tolerate the overhead exception handling does have an overhead. That is, using finally is only valid under those conditions.

There are very rare, yet valid situations where a goto is the way to go. Okay people Oh that's right just the keyword. MatthewWhited: Scoping. If you enter scope at arbitrary point it is not clear to human what constructors should be called the problem exists in C as well.

Show 12 more comments. In C, for example, I believe there are three basic scenarios where a goto is appropriate. Breaking out of a nested loop. This would be unnecessary if the language had a labeled break statement. Bailing out of a stretch of code typically a function body in case of an error or other unexpected event. This would be unnecessary if the language had exceptions.

Implementing an explicit finite state machine. In this case and, I think, only in this case a goto corresponds directly to a concept in the problem domain, transitioning from one state to a specified other state, where the current state is represented by which block of code is currently executing. Keith Thompson Keith Thompson 6, 2 2 gold badges 27 27 silver badges 34 34 bronze badges. This is actually the best answer so far — rather surprising, for a question that is one and a half years old.

In switch-statement state machine, every write to the control value is really a goto. SSSM's are often good structures to use, since they separate out the SM state from the execution construct, but they don't really eliminate "gotos". Thank you for this. The "structured program theorem" amuses me, since it tends to clearly demonstrate the point that using structured programming statements to emulate a goto is much less readable than just using a goto!

Show 1 more comment. Timwi Timwi 4, 27 27 silver badges 37 37 bronze badges. Your point is that C 's goto is not evil? If so, that is the case for every routinely used modern language with a goto construct, not only C Add a comment. Chinmay Kanchi Chinmay Kanchi 6, 2 2 gold badges 37 37 silver badges 50 50 bronze badges. When your loops are nested several levels deep, your problem is that you failed to architect your code to avoid the multinested loops. Procedure calls are NOT the enemy. Read the "Lambda: The Ultimate Using a goto to break out of loops nested several levels deep is putting a band-aid on an open multiple fracture: it may be simple but it isn't the right answer.

And of course, there is never the odd case when deeply nested loops are called for? Being a good programmer isn't just about following the rules, but also about knowing when to break them. Strohm Never say never. If you use terms like "never" in programming , you've clearly not dealt with corner cases, optimization, resource contraints, etc.

In deeply nested loops, goto is very much indeed the most cleanest, elegant way to exit the loops. Doesn't matter how much you've refactored your code. Strohm: Second most missed feature on switching from VB. NET to C : the Do loop. Nested loops happen all the time. Strohm The only reason why you say "goto is never an elegant answer" is because you made your mind up that goto is never an elegant answer, and therefore any solution using goto cannot be an elegant answer.

The most of the discouragement comes form a sort of "religion" that has been created aroud God Djikstra that was compelling in the early '60s about it's indiscriminate power to: jump anywhere into whatever block of code function not executed from the begining loops not executed from the beginning skipped variable initialization jump away from whatever block of code without any possible cleanup.

In particular the first main point above is anymore permitted and the second is cleaned if you goto out of a block the stack is unwinded properly and all proper destructors called You can refer this answer to have an idea of how even code not using goto can be unreadable. But the problem is not for. It's me. Emilio Garavaglia Emilio Garavaglia 4, 1 1 gold badge 20 20 silver badges 23 23 bronze badges.

I would prefer "things like break etc are restricted goto", or something like that. The main problem with goto is that it is usually too powerful. To the extent that the current goto is less powerful than Dijkstra's, your answer is fine with me.

MuhammadAlkarouri: I totally agree. You just found a better wording to express exactly my concept. My point is that those restriction can sometimes not be applicable and the kind of restriction you need is not in the language. Then a more "powerful" thing, less specialized, is what makes you able to workaround. The answer to this is most pithily stated in the old "Lay's Potato Chips" slogan: "Bet you can't eat just one".

In your example, you have two "interlocking" whatever that means, and I'm betting it isn't pretty loops, and the goto gave you a cheap out. What about the maintenance guy, who has to modify that code after you got hit by a bus? Is the goto going to make his life easier or harder? Do those two loops need rethinking? I think this whole issue has been a case of barking up the wrong tree. Loren Pechtel Loren Pechtel 3, 23 23 silver badges 18 18 bronze badges.

Alas, most languages forbid jumping into a loop. Because control flows which fit structured programming patterns are easier to reason about than those which don't, one should attempt to use such patterns when possible; if code does fit such patterns, one shouldn't shout that it doesn't.

On the other hand, in cases where code really doesn't fit such patterns, shouting about it may be better than pretending the code fits when it really doesn't.

According to your logic, you shouldn't retrieve it, because chasing after your schedule wasn't on your schedule. Graham Borland Graham Borland 5 5 silver badges 7 7 bronze badges. Correct, but one of the arguments against goto s is that they thwart compiler optimizations. I don't see anything wrong with the question. This blog on the topic is worth a read: blog.

The answer with same graphic on another post. Add a comment. Active Oldest Votes. Improve this answer. Byron Whitlock Byron Whitlock Jumping out of nested loops are the only instance I have used goto statements. Even then, I refactor my code to simply return early when possible. I know this is an old post, but I thought I'd add one more use for goto.

In SQR goto is commonly used as a 'continue' in a loop. Many database languages don't have continues in the language so they put a goto at the end of the loop and call it if they need to 'continue'. More accurate to say because they can easily lead to spaghetti code if not used wisely. Sometimes it is actually the cleanest, DRYest way of doing things, especially in languages that don't focus on structured programming, as I've just discovered when trying to make a loop continue in T-SQL.

A return can be used instead of goto to jump out of nested loops. For example, extract the nested loops into a new function and return from there when the condition that triggers the goto is satisfied.

I only see goto being used only to get an extra bit of performance — mljrg. Show 8 more comments. What if there are some automatic objects constructed on the stack, which are local inside the loop? Goto will jump to some other place, jumping over the end of the blocks, where the destructors for these objects are called. In most cases, it won't do anything bad.

But there are cases where it WILL do, including losing user's data etc. SasQ: The compiler is responsible for ensuring that the actual "jmp" instruction generated for the goto is preceded by whatever code would be necessary to get rid of inner scope variables.

It's not true that goto can jump between functions. TrevorBoydSmith- You're right, I must have been thinking about longjmp same problematic concept, different interface. Show 4 more comments. JaredPar JaredPar k gold badges silver badges bronze badges. Ken Bloom Ken Bloom Gerry: that's a good one. I hadn't thought of that. Actually, with the goto he used to goto a subroutine of some sort , raptors should attack him. The unrestricted goto statement never was the problem, it simply was the mechanism that allowed the programmers to create problems.

The branch statements themselves will not lead to unorganized programs, but the unorganized thoughts of the programmers. So this chapter will not teach how to reason about assembly language programs.

All programs will be first structured in pseudo code, and then translated into assembly language. Readers who follow the methodology presented in this text will never encounter an unrestricted goto.

All branch statements will be explicitly defined, and all branches will be within blocks of code, just as in structured programming languages.



0コメント

  • 1000 / 1000