本文介绍了Cyclomatic Complexity指标错误地求和,因此在方法级别上方不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cyclomatic Complexity基本上是B + 1,其中B是代码中分支的数量(也就是决策点或条件)。 但是,根据它的定义,这意味着通过直接将它们全部加在一起来汇总多个CC值是不正确的。 
相反,你必须从每个值中减去1,将它们加在一起,然后加1。

Cyclomatic Complexity is essentially B + 1, where B is the number of branches (aka decision points or conditions) in the code.  However, by it's very definition, this means that summing a number of CC values by directly adding them all together is incorrect.  Instead, you must subtract 1 from each value, add them together, and then add 1.

例如,如果一个简单的类有3个方法,并且每个方法都没有条件,因此CC指标为1,那么类的CC指标也必须是1而不是3. 对于这样的类,指示CC度量值为3,
仅包含没有分支或循环的线性代码,这绝对是不正确的。 然而,VS Metrics和FxCop简单地将Class和更高级别的所有CC指标相加,产生错误且误导性的指标。

For example, if a simple Class has 3 Methods, and each method has no conditions at all and therefore a CC metric of 1, then the CC metric of the Class must also be 1 and NOT 3.  It is definitely incorrect to indicate a CC metric of 3 for such a Class, which contains only linear code with no branches or loops.  And yet, VS Metrics and FxCop simply sum all of the CC metrics at the Class and higher levels, yielding incorrect and misleading metrics.

推荐答案

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    public class Stu
    {
        public void Methoda() 
        {
            Console.WriteLine();
        }
        public void Methodb()
        {
        }
        public void Methodc()
        {
        }
    }
}


如果没有If或switch语句,它应该是0-0 + 1 = 1,所以结果是正确的,即使方法体中没有代码行,但是它还有一个用于执行的路径控制流程。

If there's no If or switch statement, it should be 0-0+1=1, so the result is correct, even there's no code line in the method body, but it also has a one path control flow for execution.


这篇关于Cyclomatic Complexity指标错误地求和,因此在方法级别上方不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 06:58