why goto should be avoided

hello all
i read in many books that we should not use goto in our code,
but i don't get detail explanation that why we should avoid goto ?

what problems can occur by using goto can any one explain ?

It, more often then not, leads to code that is difficult if not impossible to understand. For example:

1
2
3
4
5
6
7
8
9
one: if(a == b) {
    two: a = b + 1;
    goto one;
} else if(c) {
    c = false;
    if(a != 0) goto two;
    else goto three;
}
//etc... 

This code is very difficult to follow.

Using goto also allows you do very weird/bad things, such as jump out of scope without going out of scope, etc.
Last edited on
However, it's generally accepted that using goto every now and then to brake out of multiple nested loops is better than creating flags and adding checks to the conditions.

If you're a beginner, though, it's better if you avoid it. At least until you create some criteria of when to use it and when not to use it.
I've never really had a need for it at all so far. When I was learning VB my tutors code would always have goto in nearly everything he wrote.
In 12 years of professional programming I have never once had the need for a goto.

Deeply nested code implies I have not created helper functions to shorten the function that is deeply nested.
In 12 years of professional programming I have never once had the need for a goto.

Deeply nested code implies I have not created helper functions to shorten the function that is deeply nested.


+1 for my 4yrs as a Prof.
When my students ask me this question, I explain in the following way:

Monopoly is a very simple game. Everyone has played it, and invariably, people modify it with their own "house rules". A rule that you get money if you land in free-parking, or new rules about going to and staying in jail, bankruptcy rules, partnering rules, etc...

Imagine going to a friends house, and they are using house rules. Ok, not a problem, you just have to learn the rules, and use them a while, and you're good.

But what if they don't tell you the house rules. And what if there are so many of them that they keep popping up, and interacting in strange ways that are totally foreign to how you thought Monopoly was supposed to be played...

A goto statement is like a house rule. Programming is hard enough to understand when you can reliably assume that you start at the top, execute 1 line at a time, going down, one line at a time. Using a goto statement throws that assumption out the window, and all of the sudden, the game is uncertain. It's a new and uncertain rule.

Yeah, goto's seem fine to YOU if you wrote the program, but if anyone else wants to understand it, you will have to explain it to them. It's your house rule. 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
Hmm I kind of like that explanation it gives a pretty good reasoning why not to use it :)
closed account (z05DSL3A)
I like what my lecturer said, many years ago, when asked about using goto:

"Don't use goto until you have learnt enough to know why using goto is bad,
then you will have learnt enough to us goto properly."


Topic archived. No new replies allowed.