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