圈复杂度为30的方法只有一个代码路径

时间:2017-06-08 11:09:46

标签: c# visual-studio cyclomatic-complexity

我有一个创建委托词典的方法(如下)。正如您所看到的,此方法只有一个线性独立的代码路径,因为它实际上只是创建委托并将它们放在字典中。它永远不会执行generateDictionary方法中的任何委托。

然而,进行代码分析(我使用的是Visual Studio 2015 Enterprise),说这种方法的圈复杂度为30.考虑到我上面所说的内容,这很奇怪。

可能是代码分析错误地计算了代表' return语句,即使它们永远不会在generateDictionary内执行?或者,除了if-else和switch语句之外,圈复杂度计算还需要考虑更多因素吗?

private Dictionary<string, Func<EnterpriseResource, object, bool>> generateDictionary()
{
    Dictionary < string, Func < EnterpriseResource, object, bool>> comparisonsDictionary
        = new Dictionary<string, Func<EnterpriseResource, object, bool>>();

    Func<EnterpriseResource, object, bool> compareName = delegate(EnterpriseResource onlineResource, object value)
    {
        return onlineResource.Name != null && !onlineResource.Name.Equals(value);
    };
    comparisonsDictionary.Add(resourceName, compareName);

    Func<EnterpriseResource, object, bool> compareEmail = delegate (EnterpriseResource onlineResource, object value)
    {
        return onlineResource.Email != null && !onlineResource.Email.Equals(value);
    };
    comparisonsDictionary.Add(emailAddress, compareEmail);

    Func<EnterpriseResource, object, bool> compareGroup = delegate (EnterpriseResource onlineResource, object value)
    {
        return onlineResource.Group != null && !onlineResource.Group.Equals(value);
    };
    comparisonsDictionary.Add(group, compareGroup);

    Func<EnterpriseResource, object, bool> compareCode = delegate (EnterpriseResource onlineResource, object value)
    {
        return onlineResource.Code != null && !onlineResource.Code.Equals(value);
    };
    comparisonsDictionary.Add(code, compareCode);

    Func<EnterpriseResource, object, bool> compareCostCenter = delegate (EnterpriseResource onlineResource, object value)
    {
        return onlineResource.CostCenter != null && !onlineResource.CostCenter.Equals(value);
    };
    comparisonsDictionary.Add(costCenter, compareCostCenter);

    Func<EnterpriseResource, object, bool> compareCanLevel = delegate (EnterpriseResource onlineResource, object value)
    {
        return !onlineResource.CanLevel.Equals(bool.Parse((string) value));
    };
    comparisonsDictionary.Add(canLevel, compareCanLevel);

    Func<EnterpriseResource, object, bool> compareIsActive = delegate (EnterpriseResource onlineResource, object value)
    {
        return !onlineResource.IsActive.Equals(bool.Parse((string)value));
    };
    comparisonsDictionary.Add(active, compareIsActive);

    Func<EnterpriseResource, object, bool> compareBaseCalendar = delegate (EnterpriseResource onlineResource, object value)
    {
        return onlineResource.BaseCalendar != null && !onlineResource.BaseCalendar.Name.Equals(value);
    };
    comparisonsDictionary.Add(baseCalendar, compareBaseCalendar);

    Func<EnterpriseResource, object, bool> compareHireDate = delegate (EnterpriseResource onlineResource, object value)
    {
        return onlineResource.HireDate != null && !onlineResource.HireDate.Equals(DateTime.Parse((string)value));
    };
    comparisonsDictionary.Add(hireDate, compareHireDate); 

    Func<EnterpriseResource, object, bool> compareTerminationDate = delegate (EnterpriseResource onlineResource, object value)
    {
        return onlineResource.TerminationDate != null && !onlineResource.TerminationDate.Equals(DateTime.Parse((string)value));
    };
    comparisonsDictionary.Add(terminationDate, compareTerminationDate);

    Func<EnterpriseResource, object, bool> compareInitials = delegate (EnterpriseResource onlineResource, object value)
    {
        return onlineResource.Initials != null && !onlineResource.Initials.Equals(value);
    };
    comparisonsDictionary.Add(initials, compareInitials);

    return comparisonsDictionary;
}

0 个答案:

没有答案