C# 4.0 静态字典如何具有圈复杂度?

C# 4.0 静态字典如何具有圈复杂度?,c#-4.0,cyclomatic-complexity,C# 4.0,Cyclomatic Complexity,我有以下课程: public static class MyClass { private static readonly Dictionary<Type, Func<string, object>> valueTypes; static MyClass() { var dictionary = new Dictionary<Type, Func<string, object>>(); di

我有以下课程:

public static class MyClass
{
    private static readonly Dictionary<Type, Func<string, object>> valueTypes;

    static MyClass()
    {
        var dictionary = new Dictionary<Type, Func<string, object>>();
        dictionary.Add(typeof(bool), x => bool.Parse(x));
        dictionary.Add(typeof(byte), x => byte.Parse(x));
        dictionary.Add(typeof(char), x => char.Parse(x));
        dictionary.Add(typeof(decimal), x => decimal.Parse(x));
        dictionary.Add(typeof(double), x => double.Parse(x));
        dictionary.Add(typeof(float), x => float.Parse(x));
        dictionary.Add(typeof(int), x => int.Parse(x));
        dictionary.Add(typeof(long), x => long.Parse(x));
        dictionary.Add(typeof(sbyte), x => sbyte.Parse(x));
        dictionary.Add(typeof(short), x => short.Parse(x));
        dictionary.Add(typeof(uint), x => uint.Parse(x));
        dictionary.Add(typeof(ulong), x => ulong.Parse(x));
        dictionary.Add(typeof(ushort), x => ushort.Parse(x));
        MyClass.valueTypes = dictionary;
    }
}
公共静态类MyClass
{
私有静态只读字典值类型;
静态MyClass()
{
var dictionary=newdictionary();
Add(typeof(bool),x=>bool.Parse(x));
Add(typeof(byte),x=>byte.Parse(x));
Add(typeof(char),x=>char.Parse(x));
Add(typeof(decimal),x=>decimal.Parse(x));
Add(typeof(double),x=>double.Parse(x));
Add(typeof(float),x=>float.Parse(x));
Add(typeof(int),x=>int.Parse(x));
Add(typeof(long),x=>long.Parse(x));
Add(typeof(sbyte),x=>sbyte.Parse(x));
Add(typeof(short),x=>short.Parse(x));
Add(typeof(uint),x=>uint.Parse(x));
Add(typeof(ulong),x=>ulong.Parse(x));
Add(typeof(ushort),x=>ushort.Parse(x));
MyClass.valueTypes=字典;
}
}
但是,Microsoft代码分析将其标记为圈复杂度为27。我不明白为什么一系列带有委托的Add调用会导致如此高的圈复杂度


如何降低圈复杂度?

我喜欢CC的这个定义-
源代码函数中的决策逻辑量(
)(请参阅“”,还有一个很好的例子,说明如何计算CC)

因此,由于每个
Add()
都有两个不同的代码路径-
Add()
本身和
Value
函数,因此
CC+=2
。既然你把
Add()
13次-
CC=
至少
26次
。关于MSDN,最小CC是
2
,当它增加时,它从1开始增加,因此最终得到27:)在字典值中有一个匿名方法会增加复杂性,因为它可能会引发异常,因此测试也应该包括在内

:

圈复杂度–度量对象的结构复杂度 代码。它是通过计算不同代码路径的数量来创建的 在程序流程中。具有复杂控制流的程序 将需要更多的测试来实现良好的代码覆盖率,并且将减少 可维护的


感兴趣的是检查以下示例的CC是什么:

private static readonly Dictionary<Type, Func<string, object>> valueTypes
static MyClass()     
{         
    var dictionary = new Dictionary<Type, Func<string, object>>();         
    dictionary.Add(typeof(bool), x => bool.Parse(x)); 
}


static MyClass()     
{         
    var dictionary = new Dictionary<Type, Func<string, object>>();         
    Func<string, object> func = (x) => bool.Parse(x)
    dictionary.Add(typeof(bool), func); 
}

static MyClass()     
{         
    var dictionary = new Dictionary<Type, Func<string, object>>();         
    dictionary.Add(typeof(bool), null); 
}
私有静态只读字典值类型
静态MyClass()
{         
var dictionary=newdictionary();
Add(typeof(bool),x=>bool.Parse(x));
}
静态MyClass()
{         
var dictionary=newdictionary();
Func Func=(x)=>bool.Parse(x)
dictionary.Add(typeof(bool),func);
}
静态MyClass()
{         
var dictionary=newdictionary();
dictionary.Add(typeof(bool),null);
}

参见Lambda表达式就像冰山一样。语法sugar非常甜美,但这段代码在构造函数代码中创建了13个嵌套类和26个条件分支。查看它的唯一方法是以分析工具的方式查看它,在程序集上运行ildasm.exe。