Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the main thing here is that conditionals do short-circuiting, which is a form of control flow. What helps is to re-write the code to make that explicit. This sort of normalization is common when doing program analysis. Some ad-hoc normalization (not formal and a machine wouldn't generate this, but it gets the point across) would make your code look like the following:</p> <pre><code>public static boolean validatePassword(String password) { int len = password.length(); //evaluate 'len &lt; 8 || len &gt; 20' bool cond1 = len &lt; 8; if (!cond1) cond1 = len &gt; 20; //do the if test if (cond1) return false; boolean hasLetters = false; boolean hasDigits = false; //for loops are equivalent to while loops int i = 0; while(i &lt; len) { if (!Character.isLetterOrDigit(password.charAt(i))) return false; //evaluate 'hasDigits || Character.isDigit(password.charAt(i))' bool hasDigitsVal = hasDigits; if (!hasDigitsVal) hasDigitsVal = Character.isDigit(password.charAt(i)); //hasDigits = ... hasDigits = hasDigitsVal //evaluate 'hasLetters || Character.isLetter(password.charAt(i))' bool hasLettersVal = hasLetters; if (!hasLettersVal) hasLettersVal = Character.isLetter(password.charAt(i)); //hasLetters = ... hasLetters = hasLettersVal; i++; } //evaluate 'hasDigits &amp;&amp; hasLetters' bool cond2 = hasDigits; if (cond2) cond2 = hasLetters; //return ... return cond2; } </code></pre> <p>Notice how the <code>||</code> and <code>&amp;&amp;</code> operators essentially just add <code>if</code> statements to the code. Also notice that you now have 6 <code>if</code> statements and one <code>while</code> loop! Maybe that is the 7 you were looking for?</p> <hr> <p>About multiple exit points, that's a red herring. Consider each function as having one exit node, the end of the function. If you have multiple <code>return</code> statements, each <code>return</code> statement would draw an edge to that exit node.</p> <pre><code>void foo() { if (cond1) return a; if (cond2) return b; return c; } </code></pre> <p>The graph would look like this, where <code>-----val----&gt; EXIT</code> means exiting the function with a value of <code>val</code>:</p> <pre><code>START -&gt; cond1 ------------------------a------------&gt; EXIT | | cond2 ------------------------b----------------+ | | return -----------------------c----------------| </code></pre> <p>If you re-write the code, then you just basically add another "pre-return" node that then goes to the exit node:</p> <pre><code>void foo() { int val; if (cond1) { val= a; } else { if (cond2) { val= b; } else { val= c; } } return val; } </code></pre> <p>Now it looks like this:</p> <pre><code>START -&gt; cond1 ---&gt; val=a --------------------------&gt; return ----val----&gt; EXIT | | cond2 ---&gt; val=b ------------------------------+ | | + -----&gt; val=c ------------------------------+ </code></pre> <p>It's still as complex, and the code is just uglier.</p>
    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.
    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.
 

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