Note that there are some explanatory texts on larger screens.

plurals
  1. POAbsolutely-DRY conditionals in a language that doesn't allow goto?
    primarykey
    data
    text
    <p>How do you fake conditionals that don't nest in a language that doesn't allow goto? I want to do the following:</p> <pre><code>if (condition1) action1; if (!condition1 &amp;&amp; condition2) action2; if (!condition2 &amp;&amp; condition3) action3; </code></pre> <p>Without:</p> <ol> <li>Needlessly evaluating any condition more than once.</li> <li>Needlessly storing the result of any such evaluation in a variable.</li> <li>Needlessly specifying more than once that any action should be performed.</li> </ol> <p>The original snippet fails to meet requirement 1.</p> <p>The following snippet fails to meet requirement 2:</p> <pre><code>if (condition1) { action1; c2 = false; } else if (c2 = condition2) action2; if (!c2 &amp;&amp; condition3) action3; </code></pre> <p>And the following snippet fails to meet requirement 3:</p> <pre><code>if (condition1) { action1; if (condition3) action3; } else if (condition2) action2; else if (condition3) action3; </code></pre> <hr> <p><strong>EDIT:</strong></p> <ol> <li><p>It is impossible that <code>condition1</code> and <code>condition2</code> be true simultaneously.</p></li> <li><p>It is impossible that <code>condition2</code> and <code>condition3</code> be true simultaneously.</p></li> </ol> <p>Here is the original code (in JavaScript):</p> <pre><code>// If only the array of elements was specified, // wrap it inside an object. if (info.constructor !== Object) info = {children: info}; // If no array of elements was specified, create // an empty array of elements. if (!info.children) info.children = []; // If, instead of an array of elements, we have // a single element, wrap it inside an array. if (info.children.constructor !== Array) info.children = [info.children]; </code></pre>
    singulars
    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.
 

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