Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Historical context:</strong> We should remember that Dijkstra wrote <em><a href="http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html" rel="noreferrer">Goto Considered Harmful</a></em> in 1968, when a lot of programmers used <code>goto</code> as a replacement for <a href="http://en.wikipedia.org/wiki/Structured_programming" rel="noreferrer">structured programming</a> (<code>if</code>, <code>while</code>, <code>for</code>, etc.). </p> <p>It's 44 years later, and it's rare to find this use of <code>goto</code> in the wild. Structured programming has already won, long ago.</p> <p><strong>Case analysis:</strong></p> <p>The example code looks like this:</p> <pre><code> SETUP... again: COMPUTE SOME VALUES... if (cmpxchg64(ptr, old_val, val) != old_val) goto again; </code></pre> <p>The structured version looks like this:</p> <pre><code>SETUP... do { COMPUTE SOME VALUES... } while (cmpxchg64(ptr, old_val, val) != old_val); </code></pre> <p>When I look at the structured version, I immediately think, "it's a loop". When I look at the <code>goto</code> version, I think of it as a straight line with a "try again" case at the end.</p> <p>The <code>goto</code> version has both <code>SETUP</code> and <code>COMPUTE SOME VALUES</code> on the same column, which emphasizes that most of the time, control flow passes through both. The structured version puts <code>SETUP</code> and <code>COMPUTE SOME VALUES</code> on different columns, which emphasizes that control may pass through them differently.</p> <p>The question here is what kind of emphasis do you want to put in the code? You can compare this with <code>goto</code> for error handling:</p> <p>Structured version:</p> <pre><code>if (do_something() != ERR) { if (do_something2() != ERR) { if (do_something3() != ERR) { if (do_something4() != ERR) { ... </code></pre> <p>Goto version:</p> <pre><code>if (do_something() == ERR) // Straight line goto error; // | if (do_something2() == ERR) // | goto error; // | if (do_something3() == ERR) // | goto error; // V if (do_something4() == ERR) // emphasizes normal control flow goto error; </code></pre> <p>The code generated is basically the same, so we can think of it as a typographical concern, like indentation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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