# goto (programming)

**URL:** <https://boards.straightdope.com/t/goto-programming/318168>\
**Category:** In My Humble Opinion\
**Created:** [August 22, 2005, 8:36pm UTC](https://boards.straightdope.com/t/goto-programming/318168 "2005-08-22T20:36:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkts](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rkts](https://boards.straightdope.com/u/rkts)\
**Post date:** [August 22, 2005, 8:36pm UTC](https://boards.straightdope.com/t/goto-programming/318168/1 "2005-08-22T20:36:26Z")

</div>

Recently there was a thread about the use of goto, and the general consensus was that goto was worthless and should never be used.

Recently I have been doing a lot of programming in C, and I think I have found three very legitimate uses for goto.

The first is exactly the same as the break keyword, but for nested loops:

```auto

for(loop)
  {
    for(anotherloop)
      {
	if(condition)
	  goto endloop;
      }
  }
endloop:

```

If you accept that break is legitimate, I don’t see why this use of goto would not be legitimate. When I was in computer science class and my instructor regarded the use of goto as a mortal sin, I would write such loops this way:

```auto

int endloop = 0;
for(loop)
  {
    for(anotherloop)
      {
        . . .
      	if(condition)
	  {
	    endloop = 1;
	    break;
	  }
	. . .
      }
    if(endloop)
      break;
    . . .
  }

```

which is just horribly complicated and inelegant.

Another use: suppose, in a complicated function, there are several circumstances under which things could go wrong, in which case you want to clean things up and return an error code. You could use a separate function, or in C++ use a try-catch, but to me it’s simpler to use a goto:

```auto

int bigfunction()
{
  dostuff;
  if(condition)
    goto error;
  dostuff;
  if(condition)
    goto error;
  dostuff;
  . . .
  return SUCCESS;

 error:
  cleanupstuff;
  return ERROR;
}

```

A matter of taste, I suppose, but I find the above code very palatable.

Finally, suppose you have a for loop and under certain circumstances you want to restart the loop:

```auto

startloop:
for(loop)
  {
    . . .
    if(condition)
      goto startloop;
    . . .
  }

```

Now I know in some loops you could do this:

```auto

int i;
for(i = 0 ; i < 10 ; i++)
  {
    . . .
    if(condition)
      {
        i = -1;
	continue;
      }
    . . .
  }

```

But what if you were stepping through a linked list?

```auto

llist_t *tmp_node;
for(tmp_node = llist_head ; tmp_node != NULL ; tmp_node = tmp_node->next)
  {
    . . .
    if(condition)
      ???
    . . .
  }

```

I believe there is no way to do this without a goto.

Admittedly, this last circumstance is rare, and not one that ended up being in my program. But I contend that it could happen, and a goto would then be very useful.

Debate.

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 22, 2005, 8:46pm UTC](https://boards.straightdope.com/t/goto-programming/318168/2 "2005-08-22T20:46:22Z")

</div>

The first two are pretty much the default programming style for Linux. And looking over that code, I’ve never been boggled in the least. But the coders doing Linux are generally not n00b haxx0rz–since, pretty much, whether goto ends up being good or not comes down to whether or not the coder was a good coder, not the evil-quotient of goto.

The third one… do {} while (!done); ? I’ve never seen a goto used like that and it certainly doesn’t look encouraging in your presentation–it’s a manual do {} while and just adds extra work to make it go.

---

<div class="post-metadata">

**Author:** ![rkts](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rkts](https://boards.straightdope.com/u/rkts)\
**Post date:** [August 22, 2005, 8:59pm UTC](https://boards.straightdope.com/t/goto-programming/318168/3 "2005-08-22T20:59:06Z")

</div>

You’re right. I never thought to use a do-while because, frankly, I almost never use them, and haven’t in a long time. I am a little puzzled, though, when you say that my way takes _more work_. Your method, as far as I can tell, would require at least one extra test.

---

<div class="post-metadata">

**Author:** ![FatBaldGuy](https://avatars.discourse-cdn.com/v4/letter/f/ecd19e/32.png) [@FatBaldGuy](https://boards.straightdope.com/u/FatBaldGuy)\
**Post date:** [August 22, 2005, 8:59pm UTC](https://boards.straightdope.com/t/goto-programming/318168/4 "2005-08-22T20:59:28Z")

</div>

I first started to learn programming in the early 70’s, before structured coding really came into vogue. Trying to follow another programmer’s code was literally like trying to trace a stand of spaghetti. Everyone’s code was liberally sprinkled with GOTO statements and it was a big mess.

The concept of structured code was to make things more readable and easily followed by others. One of the “prime directives” of the structured coding movement (if you can call it that) was to make it possible to perform any given algorithm without using a GOTO statement. Hence the directive that GOTO is evil and is to be avoided at all costs.

In many cases the structured equivalent would compile to the exact same machine code as if a GOTO were used, but it would be cleaner and simpler to read.

Even in your last case, there are ways to avoid using a GOTO. For example, on testing for [condition] you could set a flag to be tested downstream somewhere, and then use an IF…THEN…ELSE construction based on the results of the flag.

The object of the structured paradigm is not to find valid reasons to use a GOTO, but to find ways around it.

I’ve done it both ways, and I can readily tell you which I prefer.

---

<div class="post-metadata">

**Author:** ![Ponder\_Stibbons](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ponder_stibbons/32/343_2.png) [@Ponder\_Stibbons](https://boards.straightdope.com/u/Ponder_Stibbons)\
**Post date:** [August 22, 2005, 9:12pm UTC](https://boards.straightdope.com/t/goto-programming/318168/5 "2005-08-22T21:12:10Z")

</div>

I, too, have been to GOTO land. That is, I’ve had to work on code where the original author used GOTO. And in my nearly twenty years in the business, I have never, ever, ever, ever found a GOTO that was justifiable. For those of you who think you have an elegant situation that just cries out for GOTO, I beg you to reconsider, for the sake of the next guy that has to come along and figure out your oh-so-clever solution.

---

<div class="post-metadata">

**Author:** ![rkts](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rkts](https://boards.straightdope.com/u/rkts)\
**Post date:** [August 22, 2005, 9:19pm UTC](https://boards.straightdope.com/t/goto-programming/318168/6 "2005-08-22T21:19:38Z")

</div>

I first learned to program on a TI calculator, and I ended up writing some very elaborate programs on it before I learned a real language. So I am familiar with the horrors of writing and maintaining code that depends on gotos.

My only suggestion is that for _some_ problems, goto is still the best and simplest solution. My latest project is up to 1,100 lines and uses goto only twice. I don’t think that’s spaghetti code.

How would you rewrite my first example in a way that is simpler for someone else to understand?

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 22, 2005, 9:27pm UTC](https://boards.straightdope.com/t/goto-programming/318168/7 "2005-08-22T21:27:05Z")

</div>

> [@rkts](#):
>
> Your method, as far as I can tell, would require at least one extra test.

Well, let’s see; testing the linked list one.

```auto

retry:
for (current = head; current != head; current = current.next) { /* one test here, current != head */
        list_needs_to_be_reprocessed = do_funky_stuff();

        if (list_needs_to_be_reprocessed) goto retry; /* another test */
}

```

```auto

current = head;

do {
        ist_needs_to_be_reprocessed = do_funky_stuff();

        current = next;
} while (list_needs_to_be_reprocessed); /* one test here */

```

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 22, 2005, 9:31pm UTC](https://boards.straightdope.com/t/goto-programming/318168/8 "2005-08-22T21:31:54Z")

</div>

patch #1

- 

```
   ist_needs_to_be_reprocessed = do_funky_stuff();

```

- 
- 

```
   current = next;

```

- 

```
   list_needs_to_be_reprocessed = do_funky_stuff();

```

- 
- 

```
   current = current.next;
```

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 22, 2005, 9:50pm UTC](https://boards.straightdope.com/t/goto-programming/318168/9 "2005-08-22T21:50:45Z")

</div>

Alright, let’s just ignore that second bit of code (it’s 6:45am here.) That’s what I was thinking of when I wrote that, but it’s busted code. So yeah, you need three tests to do the do-while.

```auto

do {
        for (current = head; current != head; current = current.next) { /* one test */
                list_needs_to_be_reprocessed = do_funky_stuff();

                if (list_needs_to_be_reprocessed) break; /* two tests */
        }
} while (list_needs_to_be_reprocessed); /* three tests */

```

I must say though that I’ve never needed to cease processing on a structure midway through to go back and restart it. If you’ve come to that, I think that minimizing the number of conditions you are testing is the least of your concerns.

---

<div class="post-metadata">

**Author:** ![rkts](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rkts](https://boards.straightdope.com/u/rkts)\
**Post date:** [August 22, 2005, 10:08pm UTC](https://boards.straightdope.com/t/goto-programming/318168/10 "2005-08-22T22:08:54Z")

</div>

Indeed, I only ever wrote that code because my more efficient algorithm wasn’t working, and I was basically brute-forcing it as an alternative. When I discovered the problem, the need for the goto went away. But I felt that there may be some circumstances where restarting the loop as such is the only option.

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 22, 2005, 10:28pm UTC](https://boards.straightdope.com/t/goto-programming/318168/11 "2005-08-22T22:28:11Z")

</div>

> [@rkts](#):
>
> I felt that there may be some circumstances where restarting the loop as such is the only option.

The proper indentation of the do-while does a lot more for me to make the code work correctly than shaving one nanosecond off the loop.

But as I said earlier, goto is mostly bad if the programmer isn’t good at thinking in terms of maintenance. If he is good at thinking ahead to four years down the line and feels that a goto makes perfect sense, then all the power to him. The question is: Are you that programmer? Well, probably best to wait four years and go back to your code before making that decision.

---

<div class="post-metadata">

**Author:** ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)\
**Post date:** [August 22, 2005, 10:53pm UTC](https://boards.straightdope.com/t/goto-programming/318168/12 "2005-08-22T22:53:07Z")

</div>

To handle the first case, the Java language introduced the “labelled break”, which was essentially a goto with a different name. I’d say that goto in the first case is appropriate.

I’ve only ever considered using goto once. I found that I could beat the optimizer and shave an instruction off of a function by using a goto to the end of the function rather than returning. Normally I wouldn’t have cared, but in this case I had to fit the entire program in 64 KB, and having it go above 12 KB had enormous performance implications. Now, I was doing a lot of assembly coding at the time, so I wasn’t exactly in a structured programming frame of mind. I can’t remember if I ended up using the goto or not, but I do remember being annoyed that the optimizer couldn’t perform such a simple optimization for me.

---

<div class="post-metadata">

**Author:** ![Khadaji](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Khadaji](https://boards.straightdope.com/u/Khadaji)\
**Post date:** [August 22, 2005, 11:18pm UTC](https://boards.straightdope.com/t/goto-programming/318168/13 "2005-08-22T23:18:03Z")

</div>

I see nothing wrong with your first example and would not chastise my developers for doing it. Indeed, as you pointed out, the alternative is to have more flags than the united nations. I have long said that a properly used goto is not as horrible as it has been made out to be. However, I am in the minority usually.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [August 23, 2005, 12:02am UTC](https://boards.straightdope.com/t/goto-programming/318168/14 "2005-08-23T00:02:08Z")

</div>

In good code, the physical structure of the code on the page corresponds very highly with the logical structure of what it’s doing. For 99% of us, that correspondence is significantly more valuable than the time saved by not doing an extra comparison on every iteration of a loop, or the space saved by not throwing in error handling for breaking out of a loop.

goto violates the structure correspondence, and as a result is all but inexcusable. If you’re a student, or otherwise haven’t ever had to do maintenance programming, you may not appreciate that sentiment, but I’d have a really hard time believing that you have significant experience and don’t see the value of readable code.

To address your last example, consider this:

```auto

llist_t *tmp_node;
for(tmp_node = llist_head ; tmp_node != NULL ; tmp_node = tmp_node->next)
  {
    . . .
    if(condition)
      tmp_node = llist_head;
    . . .
  }

```

---

<div class="post-metadata">

**Author:** ![VunderBob](https://avatars.discourse-cdn.com/v4/letter/v/839c29/32.png) [@VunderBob](https://boards.straightdope.com/u/VunderBob)\
**Post date:** [August 23, 2005, 12:17am UTC](https://boards.straightdope.com/t/goto-programming/318168/15 "2005-08-23T00:17:13Z")

</div>

One other point regarding break vs. goto is that with C++, where you can declare your loop variables within the loop, break will immediately descope the variables, where goto will not. To make it plainer, goto will introduce memory leaks.

---

<div class="post-metadata">

**Author:** ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)\
**Post date:** [August 23, 2005, 12:22am UTC](https://boards.straightdope.com/t/goto-programming/318168/16 "2005-08-23T00:22:46Z")

</div>

> [@VunderBob](#):
>
> One other point regarding break vs. goto is that with C++, where you can declare your loop variables within the loop, break will immediately descope the variables, where goto will not. To make it plainer, goto will introduce memory leaks.

Ewww, did not know that. (Though it makes sense.)

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [August 23, 2005, 12:24am UTC](https://boards.straightdope.com/t/goto-programming/318168/17 "2005-08-23T00:24:29Z")

</div>

Leaking a stack-based variable is an accomplishment.

---

<div class="post-metadata">

**Author:** ![Manduck](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/manduck/32/256_2.png) [@Manduck](https://boards.straightdope.com/u/Manduck)\
**Post date:** [August 23, 2005, 1:00am UTC](https://boards.straightdope.com/t/goto-programming/318168/18 "2005-08-23T01:00:15Z")

</div>

The thing I like least about goto is that it requires a labelled statement as its target. As soon as you have a statement label in your program, it could potentially be targetted by any number of gotos anywhere in the code, which makes it a bitch to maintain, especially if it is being maintained by somebody other than the original author.

---

<div class="post-metadata">

**Author:** ![rkts](https://avatars.discourse-cdn.com/v4/letter/r/c4cdca/32.png) [@rkts](https://boards.straightdope.com/u/rkts)\
**Post date:** [August 23, 2005, 1:13am UTC](https://boards.straightdope.com/t/goto-programming/318168/19 "2005-08-23T01:13:41Z")

</div>

**ultrafilter** , your suggestion would not work. Notice the ellipses: there is code being executed _before_ the test. With your method that code would not be executed on the head of the list.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [August 23, 2005, 1:24am UTC](https://boards.straightdope.com/t/goto-programming/318168/20 "2005-08-23T01:24:11Z")

</div>

> [@rkts](#):
>
> **ultrafilter** , your suggestion would not work. Notice the ellipses: there is code being executed _before_ the test. With your method that code would not be executed on the head of the list.

With the loop as you’ve written it, that’s not correct.

[Next page](https://boards.straightdope.com/t/goto-programming/318168.md?page=2)
