静态词典如何具有圈复杂度? [英] How does a static Dictionary have cyclomatic complexity?

查看:75
本文介绍了静态词典如何具有圈复杂度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

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;
    }
}

但是,Microsoft Code Analysis将此标记为具有循环特性27.我不明白为什么一系列带有委托的Add调用会导致如此高的循环复杂性。

However, Microsoft Code Analysis flags this as having a cyclomatic complexity of 27. I do not understand why a series of Add calls with delegates results in such a high cyclomatic complexity.

我该怎么做才能降低循环复杂性?

What can I do to reduce the cyclomatic complexity?

推荐答案

我喜欢CC的定义-源代码功能中决策逻辑的数量(有关更多信息,请参见 代码度量标准-循环复杂度,还有一个很好的示例如何计算CC)。

I like this definition for CC - the amount of decision logic in a source code function (See more on "Code Metrics – Cyclomatic Complexity", there is also very good example how is CC calculated).

因此,由于每个添加()具有两个不同的代码路径- Add()本身和 Value 函数,因此 CC + = 2 。由于您放置 Add() 13次- CC == 至少 26 。关于MSDN,最小CC为 2 ,当它增加时,它从1开始增加,所以最终得到27 :)在字典值中使用匿名方法会增加复杂性,因为

So since each Add() has two different code paths - Add() itself and Value function, so CC+=2. Since you put Add() 13 times - CC == at least 26. And regarding MSDN minimal CC is 2 and when it increased it start increasing from 1, so you ends up with 27 :) Having an anonymous method in a dictionary value increases a complexity since potentially it may raise an exception so should be covered by a test as well.

代码度量值,MSDN


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

Cyclomatic Complexity – Measures the structural complexity of the code. It is created by calculating the number of different code paths in the flow of the program. A program that has complex control flow will require more tests to achieve good code coverage and will be less maintainable.






只需检查以下示例的抄送是什么?


Just of interest check what are CC for these examples:

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); 
}

这篇关于静态词典如何具有圈复杂度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆