Goto is almost universally discouraged. Is this statement every worthwhile using?
|
This has been discussed several times on Stack Overflow, and Chris Gillum summarized the possible uses of
I'd argue, as many others would argue, that in all of these cases, the usage of |
|||||||||||||||||||||
|
|
Higher-level control flow constructs tend to correspond to concepts in the problem domain. An if/else is a decision based on some condition. A loop says to perform some action repeatedly. Even a break statement says "we were doing this repeatedly, but now we need to stop". A goto statement, on the other hand, tends to correspond to a concept in the running program, not in the problem domain. It says to continue execution at a specified point in the program. Someone reading the code has to infer what that means with respect to the problem domain. Of course all the higher-level constructs can be defined in terms of gotos and simple conditional branches. That doesn't mean that they're merely gotos in disguise. Think of them as restricted gotos -- and it's the restrictions that make them useful. A break statement is implemented as a jump to the end of the enclosing loop, but it's better thought of as operating on the loop as a whole. All else being equal, code whose structure reflects that of the problem domain tends to be easier to read and maintain. There are no cases where a goto statement is absolutely required (there's a theorem to that effect), but there are cases where it can be the least bad solution. Those cases vary from language to language, depending on what higher-level constructs the language supports. In C, for example, I believe there are three basic scenarios where a goto is appropriate.
On the other hand, an explicit finite state machine can also be implemented with a switch statement inside a loop. This has the advantage that every state starts at the same place in the code, which can be useful for debugging, for example. The main use of a goto in a reasonably modern language (one that supports if/else and loops) is to simulate a control flow construct that's missing from the language. |
|||||||
|
|
Surely it depends on the programming language. The main reason Java has attempted to “solve” this problem by disallowing In C#, |
||||
|
|
|
Yes. When your loops are nested several levels deep, Of course, Java's labelled |
|||||||||||
|
|
The odd goto here or there, so long as it's local to a function, rarely significantly harms readability. It often benefits it by drawing attention to the fact that there's something unusual going on in this code that requires the use of a somewhat uncommon control structure. If (local) gotos are significantly harming readability, then it's usually a sign that the function containing the goto has become too complex. The last goto I put into a piece of C code was to build a pair of interlocking loops. It doesn't fit into the normal definition of "acceptable" goto use, but the function ended up significantly smaller and clearer as a result. To avoid the goto would have required a particularly messy violation of DRY. |
|||
|
|
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:
This has nothing more to do with the In particular the first main point above is anymore permitted and the second is cleaned (if you You can refer this answer to have an idea of how even code not using goto can be unreadable. The problem is not goto itself, but the bad use of it. I can write an entire program without using But the problem is not Things like It's 2011: it's time to understand that |
|||||||||
|
|
Yes, the goto can be used to benefit the experience of the developer: http://adamjonrichardson.com/2012/02/06/long-live-the-goto-statement/ However, just as with any powerful tool (pointers, multiple inheritance, etc.), one has to be disciplined using it. The example provided in the link uses PHP, which restricts the usage of the goto construct to the same function/method and disables the ability to jump into a new control block (e.g., loop, switch statement, etc.) |
|||
|
|
|
Depends on the language. It's still widely used in Cobol programming, for example. I've also worked on a Barionet 50 device, whose firmware programming language is an early BASIC dialect that of course requires you to use Goto. |
|||
|
|
|
I think this whole issue has been a case of barking up the wrong tree. GOTO as such doesn't seem problematic to me, but rather it's very often a symptom of an actual sin: spaghetti code. If the GOTO causes major crossing of flow control lines then it's bad, period. If it crosses no flow control lines it's harmless. In the grey zone in between we have things like loop bailouts, there are still some languages that haven't added constructs that cover all the legit grey cases. The only case I've found myself actually using it in many years is the case of the loop where the decision point is in the middle of the loop. You're left with either duplicated code, a flag or a GOTO. I find the GOTO solution the best of the three. There is no crossing of flow control lines here, it's harmless. |
|||||||
|
|
I would say no. If you find the need for using GOTO, I'll bet that there's a need to redesign the code. |
|||||||||||
|
|
|
|||
|
|
|
I would argue no. They should always be replaced with a tool more specific to the problem the goto is being used to solve (unless it's not available in your language). For example, break statements and exceptions solve to previously goto-solved problems of loop escaping and error handling. |
|||||||||||
|
gotois what the cpu ultimately do, but it does not work well with what humans need to comprehend and abstract due to there is no mark on the target end. Compare to IntercalCOMEFROM. – user1249 Feb 6 '12 at 9:57gotois not required. It is just the most rational way of implementing them in the imperative languages, since it is the closest thing to the very semantics of the state transition. And its destination can be quite far from the source, it won't hinder the readability. My favourite example of such a code is D.E. Knuth's implementation of the Adventure game. – SK-logic Feb 6 '12 at 10:30