-2

我想知道使用 LOC 和圈复杂度指标是否可以得出任何关于以下两个代码的相对复杂性的结论?有点让我头疼。

代码 A

 int i = 1;
    while(i < = 5){

    playACard(i);

    if (playerHasWon(i))
    break;
    i++
    }

代码 B

int j = 0;

int i = 2;

j = i;
j = j + i;

j++;
System.out.println (j);

System.out.println
4

2 回答 2

2

Using advanced LOC tools (i.e. my eyes and fingers :-) ) I calculate the LOC counts for the two pieces of code are the same ... ignoring blank lines.

For a cyclometric complexity count, you need parseable source code ... in some programming language ... for tools to work. I defer to @d'alar'cop 's answer for a hand calculation and the explanation of how he arrived at it.


But to be honest, LOC and Cyclometric Complexity measures for code like this is not helpful. The two samples do completely different things, so it really makes little difference which is more "complex".

To my mind, CC is only really useful for identifying excessively complex code in a first-pass review of a codebase. Once you've identified the problem areas, an experienced programmer is a better judge of real complexity (i.e. the kind of stuff that makes the code hard to understand) ... and whether that complexity is necessary / inherent in the problem at hand.

于 2013-04-08T09:23:09.070 回答
1

代码 B 的圈复杂度为 1。代码中只有1 条可能的路径。

圈复杂度衡量可能的执行路径的数量(这在软件系统的测试和大小估计中很有用。) - 但大多数人会说它对后者不准确或有用。

代码 A 的圈复杂度为 4。因为代码可以进入或不进入循环(与离开循环相同)(2 条可能的路径),并且有一个“if 语句”再次将可能性乘以 2。

干杯。

还需要什么解释吗?

于 2013-04-08T09:22:05.347 回答