Note that there are some explanatory texts on larger screens.

plurals
  1. POExamples of good gotos in C or C++
    primarykey
    data
    text
    <p>In this thread, we look at examples of good uses of <code>goto</code> in C or C++. It's inspired by <a href="https://stackoverflow.com/questions/244445/best-refactoring-for-the-dreaded-while-true-loop#244644">an answer</a> which people voted up because they thought I was joking.</p> <p>Summary (label changed from original to make intent even clearer):</p> <pre><code>infinite_loop: // code goes here goto infinite_loop; </code></pre> <p>Why it's better than the alternatives:</p> <ul> <li>It's specific. <code>goto</code> is the language construct which causes an unconditional branch. Alternatives depend on using structures supporting conditional branches, with a degenerate always-true condition.</li> <li>The label documents the intent without extra comments.</li> <li>The reader doesn't have to scan the intervening code for early <code>break</code>s (although it's still possible for an unprincipled hacker to simulate <code>continue</code> with an early <code>goto</code>).</li> </ul> <p><strong>Rules:</strong></p> <ul> <li>Pretend that the gotophobes didn't win. It's understood that the above can't be used in real code because it goes against established idiom.</li> <li>Assume that we have all heard of 'Goto considered harmful' and know that goto can be used to write spaghetti code.</li> <li>If you disagree with an example, criticize it on technical merit alone ('Because people don't like goto' is not a technical reason).</li> </ul> <p>Let's see if we can talk about this like grown ups.</p> <p><strong>Edit</strong></p> <p>This question seems finished now. It generated some high quality answers. Thanks to everyone, especially those who took my little loop example seriously. Most skeptics were concerned by the lack of block scope. As @quinmars pointed out in a comment, you can always put braces around the loop body. I note in passing that <code>for(;;)</code> and <code>while(true)</code> don't give you the braces for free either (and omitting them can cause vexing bugs). Anyway, I won't waste any more of your brain power on this trifle - I can live with the harmless and idiomatic <code>for(;;)</code> and <code>while(true)</code> (just as well if I want to keep my job). </p> <p>Considering the other responses, I see that many people view <code>goto</code> as something you always have to rewrite in another way. Of course you can avoid a <code>goto</code> by introducing a loop, an extra flag, a stack of nested <code>if</code>s, or whatever, but why not consider whether <code>goto</code> is perhaps the best tool for the job? Put another way, how much ugliness are people prepared to endure to avoid using a built-in language feature for its intended purpose? My take is that even adding a flag is too high a price to pay. I like my variables to represent things in the problem or solution domains. 'Solely to avoid a <code>goto</code>' doesn't cut it.</p> <p>I'll accept the first answer which gave the C pattern for branching to a cleanup block. IMO, this makes the strongest case for a <code>goto</code> of all the posted answers, certainly if you measure it by the contortions a hater has to go through to avoid it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload