Cyclomatic Complexity in a Code Piece with Multiple Exit Points

I have this method that checks the password:

/**
 * Checks if the given password is valid.
 * 
 * @param password The password to validate.
 * @return {@code true} if the password is valid, {@code false} otherwise.
 */
public static boolean validatePassword(String password) {
    int len = password.length();
    if (len < 8 || len > 20)
        return false;
    boolean hasLetters = false;
    boolean hasDigits = false;
    for (int i=0; i<len; i++) {
        if (!Character.isLetterOrDigit(password.charAt(i)))
            return false;
        hasDigits = hasDigits || Character.isDigit(password.charAt(i));
        hasLetters = hasLetters || Character.isLetter(password.charAt(i));
    }
    return hasDigits && hasLetters;
}

Let's focus on the cyclical number of complexity: what is its significance?

Figures 1.3.6 say it's 7, but I can't find seven independent paths: I find only 5! And Wikipedia didn’t help much, how do I suppose to use this formula π - s + 2?

I have 2 if's, 1 forand 3 exit points, but I'm stuck: do I need to read the entry point? Do I have to count twice the first if, since it has two conditions?

EDIT:

, , Cyclomatic Number 7. , 7 , 7 , 100% , ?

, ! :

  • : asdf1234
  • : asdf123
  • : asdfsgihzasweruihioruldhgobaihgfuiosbhrbgtadfhsdrhuorhguozr
  • : asdf * 123
  • : 12345678
  • : asdfghjk
  • WTF???
+5
3

, , .

"" (http://metrics.sourceforge.net/) " Cyclomatic Complex McCabe":

1

3 (, , )

3 (||, ||, ||)

: 7

+3

, , , . , . . ( , , ) :

public static boolean validatePassword(String password) {
    int len = password.length();

    //evaluate 'len < 8 || len > 20'
    bool cond1 = len < 8;
    if (!cond1) cond1 = len > 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 < 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 && hasLetters'
    bool cond2 = hasDigits;
    if (cond2) cond2 = hasLetters;
    //return ...
    return cond2;
}

, || && if . , 6 if while! , 7, ?


, . node, . return, return node.

void foo() {
    if (cond1) return a;
    if (cond2) return b;
    return c;
}

, -----val----> EXIT val:

START -> cond1 ------------------------a------------> EXIT
           |                                            |
         cond2 ------------------------b----------------+
           |                                            |
         return -----------------------c----------------|

, "pre-return" node, node:

void foo() {
    int val;
    if (cond1) {
        val= a;
    }
    else {
        if (cond2) {
            val= b;
        }
        else {
            val= c;
        }
    }
    return val;
}

:

START -> cond1 ---> val=a --------------------------> return ----val----> EXIT
           |                                            |
         cond2 ---> val=b ------------------------------+
           |                                            |
           + -----> val=c ------------------------------+

, .

+2

:

Cyclomatic Complexity = (2 + ifs + loops + cases - return) :

* ifs is the number of IF operators in the function,
* loops is the number of loops in the function,
* cases is the number of switch branches in the function (without default), and
* return is the number of return operators in the function.

, .

, if (len < 8 || len > 20) 3 :

  • if
  • len<8
  • len > 20

, 2 + 8 - 3 = 7, :

  • 2 - (. )
  • 8 -
  • 3 -
0

All Articles