我有以下類:靜態詞典如何具有圈複雜性?
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;
}
}
然而,微軟的代碼分析其標記爲具有27圈複雜,我不明白爲什麼一個系列添加在如此高的圈複雜度代表結果的呼叫。
我該怎麼做才能減少圈複雜度?
見http://stackoverflow.com/questions/10244131/how-can-the-cyclomatic-complexity-be -27-in-a-method-with-13-event-handler-subscr – cm007
Lambda表達式就像一座冰山一樣。語法糖非常甜,但是此代碼在構造函數代碼中創建了13個嵌套類和26個條件分支。要看到它的唯一方法是以分析工具的方式來查看它,在程序集上運行ildasm.exe。 –