232

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?

6

6 Answers 6

172

Because they lead to spaghetti code.

In the past, programming languages didn't have while loops, if statements, 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 methods, conditionals and loops. Structured programming was a revolution at the time.

gotos are appropriate in a few places, such as for jumping out of nested loops.

14
  • 33
    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. Aug 19, 2010 at 0:14
  • 133
    +1 for acknowledging that goto still has a purpose on rare occasions.
    – Matt Davis
    Aug 19, 2010 at 1:25
  • 3
    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'.
    – brw59
    Jun 19, 2016 at 3:40
  • 2
    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.
    – Denziloe
    Feb 19, 2019 at 16:35
  • 6
    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
    Oct 4, 2019 at 9:57
108

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.

9
  • 5
    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. Right? In most cases, it won't do anything bad. But there are cases where it WILL do, including losing user's data etc.
    – SasQ
    May 12, 2012 at 6:21
  • 16
    @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.
    – supercat
    May 13, 2012 at 20:31
  • 3
    It's not true that goto can jump between functions. C has longjmp() for that, and C++ has exceptions. Jul 19, 2016 at 23:53
  • regarding your claim that "goto can also be used to jump from one function into another without making a function call": **your claim that you can use goto to "jump from one function into another without making a function call" is wrong as explained by here and here and also see the comment directly above by @celticminstrel. Mar 1, 2018 at 18:05
  • 1
    If you need a O(N^3) loop, maybe goto is the least of your concerns.
    – anastaciu
    Apr 5, 2021 at 8:55
48

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.

0
33

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 (another source) 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?"

7
  • 8
    And that GOTO's went out with the (computing) dinosaurs
    – Gerry Coll
    Aug 19, 2010 at 0:31
  • @Gerry: that's a good one. I hadn't thought of that.
    – Ken Bloom
    Aug 19, 2010 at 0:38
  • Actually, with the goto he used (to goto a subroutine of some sort), raptors should attack him.
    – Ken Bloom
    Aug 19, 2010 at 13:39
  • 1
    I find that the DIjkstra explanation (and its direct consequences) is the true reason not to use goto, and not just the "unstructured code" everybody think it´s all about. There are more implications, and you are right to mention it.
    – Senua
    Jan 20, 2015 at 13:25
  • Java don't have a goto command but a break who serve the same purpose Oct 7, 2015 at 7:34
10

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

1
  • 2
    Ironically, he actually tolerated far more GOTO than programmers today. For Djikstra, forward GOTO is okay, backward GOTO and GOTO into the inside of the loop is not.
    – Xwtek
    Jun 1, 2021 at 9:34
9

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

It was a design goal of C++ that 99-point-something percent of C programs in the wild should compile and run with the same functionality in C++. Including goto was part of the effort to reach that goal.

8
  • 2
    Apparently the author of the question is unaware why "goto" is available in C, too.
    – dionyziz
    Aug 19, 2010 at 0:30
  • 6
    C++ is not a superset of C.
    – user142019
    Aug 19, 2010 at 0:46
  • 15
    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. Aug 19, 2010 at 1:08
  • 2
    @kon to be fair you have to write really ancient style C code to make it not valid in C++, mod russians back up :-)
    – Anycorn
    Aug 19, 2010 at 3:11
  • Hey, thanks, I didn't know there was russian mob around here :) Aug 19, 2010 at 3:15