I am developing an application that extracts data from an Excel file (I do not have access to the actual database), and I wrote a method that has a single function to extract data from an Excel spreadsheet, as shown 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 launched Code Analysis (I am using Visual Studio 2012 and developing in .NET 4.5) and I have CA1502: Avoid excessive complexity (copied below). As a junior developer (I'm 17), I tried to learn more about this using MSDN, but I'm a bit puzzled by why I have cyclic complexity of 33.
CA1502
Avoid excessive complexity
'Extraction.ExtractSMData(List<Range>)' has a cyclomatic complexity of 33. Rewrite or reorganize the method to reduce complexity to 25.
Core.Extraction.cs:104
I can see my quick ifs ( condition ? if_true : if_false , what are they called?) That this might be bad, but I can still see only 5.
UPDATE
Cyclomatic complexity is now 33 ...
If I comment on entity.IncidentNumber = Row.get_Range("K1").get_Value(); , the complexity will be 32. I thought that get_Range() and get_Value() were one at a time, but everything is fine ...
If I comment on entity.RepairedOnTime = Row.get_Range("W1").get_Value().ToString().ToLower() == "no" ? false : true; entity.RepairedOnTime = Row.get_Range("W1").get_Value().ToString().ToLower() == "no" ? false : true; , the difficulty will be 28 ...
get_Range() , get_Value() , quick-if is 3, do ToString() and ToLower() count?
Jake hendy
source share