Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclomatic complexity count of 31, where has this come from?

I'm developing an application that pulls data from an Excel file (I don't have access to the actual database) and I've written a method which has as its only function to pull the data from the Excel Spreadsheet, as seen below.

private IEnumerable<SMEntity> ExtractSMData(List<MSExcel.Range> SMData)
{
    List<SMEntity> SMEntities = new List<SMEntity>();

    foreach (MSExcel.Range Row in SMData)
    {
        SMEntity entity = new SMEntity();
        entity.IncidentNumber = Row.get_Range("K1").get_Value();
        entity.SRNumber = Row.get_Range("L1").get_Value();
        entity.SRCategory = Row.get_Range("M1").get_Value();
        entity.SiebelClientCall = EntityConversions.DateTimeConversion(Row.get_Range("N1").get_Value());
        entity.SiebelOpenedDate = EntityConversions.DateTimeConversion(Row.get_Range("O1").get_Value());
        entity.IncidentOpenDate = EntityConversions.DateTimeConversion(Row.get_Range("P1").get_Value());
        entity.PickedUpBeforeClient = Row.get_Range("Q1").get_Value().ToString().ToLowerCase() == "no" ? false : true;
        entity.OutageStartTime = EntityConversions.DateTimeConversion(Row.get_Range("R1").get_Value());
        entity.DetectionPoint = EntityConversions.DateTimeConversion(Row.get_Range("S1").get_Value());
        entity.SecondsToDetection = EntityConversions.ConvertDetectionTimeToInt(Row.get_Range("T1").get_Value());
        entity.OutageEndTime = EntityConversions.DateTimeConversion(Row.get_Range("U1").get_Value());
        entity.MTTR = EntityConversions.ConvertMTTRStringToInt(Row.get_Range("V1").get_Value());
        entity.RepairedOnTime = Row.get_Range("W1").get_Value().ToString().ToLowerCase() == "no" ? false : true;
        SMEntities.Add(entity);
    }

    return SMEntities;
}

I've run Code Analysis (I'm using Visual Studio 2012 and developing in .NET 4.5) and I have a CA1502: Avoid excessive complexity (copied below). As a junior developer (I'm 17) I tried finding out more about this using MSDN however, I'm a little stumped as to why I have a cyclomatic complexity of 33.

CA1502

Avoid excessive complexity

'Extraction.ExtractSMData(List<Range>)' has a cyclomatic complexity of 33. Rewrite or refactor the method to reduce complexity to 25.

Core.Extraction.cs:104

I can see with my quick-ifs (condition ? if_true : if_false, what are these called?) that it might be bad, but I can still only see it as 5.

UPDATE:

Cyclomatic complexity is now at 33...

If I comment out entity.IncidentNumber = Row.get_Range("K1").get_Value(); the complexity becomes 32. I thought get_Range() and get_Value() were one each but okay...

If I comment out entity.RepairedOnTime = Row.get_Range("W1").get_Value().ToString().ToLower() == "no" ? false : true; the complexity becomes 28...

get_Range(), get_Value(), quick-if is 3, do ToString() and ToLower() count?

like image 260
Jake Hendy Avatar asked Dec 06 '12 10:12

Jake Hendy


People also ask

What is considered high cyclomatic complexity?

Consequences: A high cyclomatic complexity for a particular function means that the function will be difficult to understand, and more difficult to test. That in turn means functions with a cyclomatic complexity above 10 or 15 can be reasonably expected to have more undetected defects than simpler functions.

Is there a cyclomatic complexity of 10?

If a method has a cyclomatic complexity of 10, it means there are 10 independent paths through the method. This implies is that at least 10 test cases are needed to test all the different paths through the code. The lesser the number, the easier it is to test.

What does cyclomatic complexity tell us?

Cyclomatic complexity is a measurement developed by Thomas McCabe to determine the stability and level of confidence in a program. It measures the number of linearly-independent paths through a program module. Programs with lower Cyclomatic complexity are easier to understand and less risky to modify.

How do you fix high cyclomatic complexity?

Reduce the Number of Decision Structures You might consider this one a no-brainer. If the decision structures—especially if-else and switch case—are what cause more branches in the code, it stands to reason that you should reduce them if you want to keep cyclomatic complexity at bay.


1 Answers

I calculate the complexity for the method itself, the foreach, and the two conditional operators as 4 total. If each of the 13 calls to get_Range is worth +1 complexity and each of the 13 calls to get_Value is worth +1 complexity then the total complexity will add up to 30 (still 1 short, but close). I'm not sure why those two functions could be increasing the complexity but it seems plausible.

Try removing one of the lines calling get_Range and get_Value and see if the cyclomatic complexity goes down to 29.

like image 58
Mike Cowan Avatar answered Oct 21 '22 05:10

Mike Cowan