Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

why is GOTO rarely used?

OpenStudy (anonymous):

It's a programming taboo; unrestricted use of goto statements can and usually will result in spaghetti code and unreadable, hard to understand programs where the control flow isn't clear. Nowadays, use of looping constructs allows you to write programs without needing any goto statements. Goto is usually considered a harmful construct, but it has its places (state machines, etc). Ultimately, it's recommended that you avoid goto whenever possible, and use looping constructs etc. Most languages these days don't come with goto at all, but they still work fine.

OpenStudy (anonymous):

eh..what is spaghetti code?

OpenStudy (anonymous):

Well Goto is generally considered bad programming... :)

OpenStudy (anonymous):

spaghetti code is very complex code because you use goto and it's hard to understand what's being done

OpenStudy (anonymous):

'Spaghetti code' is simply an expression denoting unstructured code, by analogy to the tangles in spaghetti. Egregious example of spaghetti code in C (but there's worse if you look into http://ioccc.org ) http://ideone.com/jUYfb

OpenStudy (anonymous):

In C and C++, jumps should usually be restricted to function calls and scoped operations like loops or conditionals, because within the syntax of the language, they make it easy to understand and predict how the code works. Goto can continue execution anywhere, and can make it difficult to see and understand the flow of operation of the code. Not only makes it code less readable and understandable, it can also easily lead to things like forgetting cleanup (like for example jumping past freeing allocated buffers or similar). Generally, the results achieved with goto can be easily achieved by other means, and doing so keeps code more readable and maintainable. As an example, imagine code with a couple of goto statements wrapped in if()s, that jump to different labels. It's easy to picture how this could become very difficult to keep track of as the complexity of the surrounding code increases. Another example, if you then would like to refactor the code, for example by moving some of the functionality to a separate function, you may be limited by the label placement - what if the section of code you'd like to functionalize contains a label that one of the gotos outside of that section jumps to? That's a pretty good example of how gotos can substantially reduce maintainability of code. If you find yourself absolutely needing a goto statement, it may be a good idea to look at the general architecture of your code, because it's a good sign that you went wrong somewhere ;)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!