涉及递归函数时计算圈复数

Calculate Cyclomatic complex when it comes to a recursion function

传统上,圈复杂度(CC)可以通过"if-else"的个数加一的方式得到。但是当涉及到递归函数时,我发现我无法计算出"if-else"的个数。更具体地说,在这个代码块

public int m1(int k){
    if(k==0)
        return 0;
    else
        return m2(k-1)+(k%2);
}

public int m2(int k){
    if(k==0)
        return 0;
    else
        return m1(k-1)+(1-k%2);
}

如何确定m1的CC?

说明:

定义一个函数CC(func),代表函数的CC "func"

所以,CC(m1) = 1(k==0) + CC(m2) (k!=0)

CC(m2) = 1(k==0) + CC(m1) (k!=0)

我的意思是,我们应该考虑调用函数的 CC。

感谢您的帮助。

参见https://en.m.wikipedia.org/wiki/Cyclomatic_complexity. Recursion by definition does not impact cyclomatic complexity and the tools measuring it also do not actually 'run' the code and measure it. This is because the original definition of McCabe states that CC is the number of possible path that the code can take, and that's about it. If you are still not convinced , just give it a different name, say recursive cylcomatic complexity, and measure it, by counting the actual number of invocations at runtime. Refer: https://www.guru99.com/cyclomatic-complexity.html