0

I have a method that creates a dictionary of delegates (below). As you can see, this method just has one linearly independent code path because it's literally just creating the delegates and putting them in the dictionary. It never executes any of the delegates inside the generateDictionary method.

However, doing a code analysis (I'm using Visual Studio 2015 Enterprise), says that this method has a cyclomatic complexity of 30. Which is weird taking into account what I said above.

Could it be that the code analysis is incorrectly counting the delegates' return statement, even if these are never executed inside generateDictionary? Or does cyclomatic complexity calculation, takes more things into account besides if-else and switch statements?

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;
}
Pedro Gordo
  • 1,825
  • 3
  • 21
  • 45
  • Is there a specific reason you are using `delegate` instead of lambda expression? I think `Func compareName = (onlineResource, value) => onlineResource.Name != null && !onlineResource.Name.Equals(value);` is more readable and concise. – kiziu Jun 08 '17 at 11:14
  • No specific reason for it. I could do with either. But unfortunately, using lambda expressions still gives me CI factor of 30. – Pedro Gordo Jun 08 '17 at 11:23
  • 1
    Lambdas and anonymous functions are not free. They still contribute to cyclomatic complexity! – Cody Gray - on strike Jun 08 '17 at 11:53
  • @CodyGray Yes, from reading the MSDN description, I can see that. Previously I only read the Wikipedia article on CC, and it just takes into account the code paths. – Pedro Gordo Jun 08 '17 at 13:06

0 Answers0