Cyclomatic Complexity in piece of code with multiple exit points

前端 未结 3 840
隐瞒了意图╮
隐瞒了意图╮ 2021-02-09 15:38

I have this method that validates a password:

/**
 * Checks if the given password is valid.
 * 
 * @param password The password to validate.
 * @return {@code tr         


        
3条回答
  •  长发绾君心
    2021-02-09 16:30

    As nicely explained here :

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

    * 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.
    

    As already mentioned, logical conditions are also calculated.

    For example if (len < 8 || len > 20) counts as 3 conditions :

    1. if
    2. len<8
    3. len > 20

    That means, that your code has complexity of 2 + 8 - 3 = 7, where :

    • 2 - it is always there (see formula up there)
    • 8 - number of branches
    • 3 - number of returns

提交回复
热议问题