tags:

views:

237

answers:

7

Possible Duplicates:
Why is it bad to use goto?
GOTO still considered harmful?

I was ramdomming through xkcd and saw this one (if also read some negative texts about them some years ago):
your slow connection sucks, get a faster one to see this image
What is actually wrong with it? Why are goto's even possible in C++ then?

Why should I not use them?

A: 

http://en.wikipedia.org/wiki/Considered_harmful

Dykstra's Article

Ed Swangren
I think the URL is longer than the article itself.
Charlie Salts
You would be wrong.
Ed Swangren
+5  A: 

There is nothing wrong with goto in itself. It's a very useful construct in programming and has many valid uses. The best that comes to mind is structured resource freeing in C programs.

Where goto's go wrong is when they are abused. Abuse of gotos can lead to thoroughly unreadable and unmaintainable code.

JaredPar
@downvoter care to explain?
JaredPar
A: 

It's possible in C++ because it's possible in C. Whether you should or shouldn't use it is long-standing religious war.

Nikolai N Fetissov
Apparently the author of the question is unaware why "goto" is available in C, too.
dionyziz
C++ is **not** a superset of C.
Time Machine
It's not, but one of the original goals (and, apparently, banes) of C++ was strong compatibility with C. And I still don't get what you are so upset about.
Nikolai N Fetissov
@kon to be fair you have to write really ancient style C code to make it not valid in C++, mod russians back up :-)
aaa
Hey, thanks, I didn't know there was russian mob around here :)
Nikolai N Fetissov
+1  A: 

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!) http://en.wikipedia.org/wiki/GOTO

John Smith
+5  A: 

Because they lead to spaghetti code.

In the past programming languages didn't have while() if() etc, and programmers used goto to make up the logic of their programs. It lead to an unmaintainable mess.

That's why the CS gods created functions conditionals and loops. Structred programming was a revolution at the time.

goto's are appropriate in a few places, such as for jumping out of nested loops

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.
Charlie Salts
+1 for acknowledging that `goto` still has a purpose on rare occasions.
Matt Davis
+1  A: 

In 1968, Edsger Dijkstra wrote a famous letter to the editor of Communications of the ACM GOTO is considered harmful in which he laid out the case for structured programming with while loops and if...then...else conditionals. When GOTO is used to substitute for these control structures, the result is very often spaghetti code. Pretty much every programming language in use to day is a structured programming language, and use of GOTOs has been pretty much eliminated. In fact, Java, Scala, Ruby, and Python don't have a goto command at all.

C, C++ and Perl still do have a GOTO command, and there are situations (in C particularly) where a GOTO is useful, for example a break statement that exits multiple loops, or as a way of concentrating cleanup code in a single place in a function even when there are multiple ways to terminate the function (e.g. by returning error codes at multiple points in the progress of a function). But generally its use should be restricted to specific design patterns that call for it in a controlled and recognized way.

(In C++, it's better to use RAII or a ScopeGuard (more) instead of using GOTO for cleanup. But GOTO is a frequently used idiom in the Linux kernel which is a great example of idiomatic C code.)

The XKCD comic is a joke on the question "Should GOTO always be considered harmful when there are certain specific design patterns that are helped greatly by its use?"

Ken Bloom
And that GOTO's went out with the (computing) dinosaurs
Gerry
@Gerry: that's a good one. I hadn't thought of that.
Ken Bloom
Actually, with the `goto` he used (to `goto` a subroutine of some sort), raptors should attack him.
Ken Bloom
+3  A: 

Nothing is wrong with goto if it is used properly. The reason it is "taboo" is because in the early days of C, programmers (often coming from an assembly background) would use goto to create incredibly hard-to-understand code.

Most of the time, you can live without goto and be fine. There are a few instances, however, where goto can be useful. The prime example is a case like:

for (i = 0; i < 1000; i++) {
    for (j = 0; j < 1000; j++) {
        for (k = 0; k < 1000; k++) {
            ...
            if (condition)
                goto break_out;
            ....
        }
    }
}
break_out:

Using a goto to jump out of a deeply-nested loop can often be cleaner than using a condition variable and checking it on every level.

Using goto to implement subroutines is the main way it is abused. This creates so-called "spaghetti code" that is unnecessarily difficult to read and maintain. goto can also be used to jump from one function into another without making a function call, but this is almost always dangerous and disastrous.

bta