I need to construct a Control Flow Diagram (simple flow graph with nodes and edges) for each method in my C# project in order to demonstrate the graph-way to calculate cyclomatic complexity.
I first counted the cyclomatic complexity using VS 2010, then I construct the graph to make sure the result value is same as the one counted from VS. However, I met some problem here because I not sure which expression is actually consider a +1 for cyclomatic complexity.
Let's look at one example here:
public ActionResult Edit(string id, string value)
{
string elementId = id;
// Use to get first 4 characters of the id to indicate which category the element belongs
string fieldToEdit = elementId.Substring(0, 4);
// Take everything AFTER the 1st 4 characters, this will be the ID
int idToEdit = Convert.ToInt32(elementId.Remove(0, 4));
// The value to be return is simply a string:
string newValue = value;
var food = dbEntities.FOODs.Single(i => i.FoodID == idToEdit);
// Use switch to perform different action according to different field
switch (fieldToEdit)
{
case "name": food.FoodName = newValue; break;
case "amnt": food.FoodAmount = Convert.ToInt32(newValue); break;
case "unit": food.FoodUnitID = Convert.ToInt32(newValue); break;
// ** DateTime format need to be modified in both view and plugin script
case "sdat": food.StorageDate = Convert.ToDateTime(newValue); break;
case "edat": food.ExpiryDate = Convert.ToDateTime(newValue); break;
case "type": food.FoodTypeID = Convert.ToInt32(newValue); break;
default: throw new Exception("invalid fieldToEdit passed");
}
dbEntities.SaveChanges();
return Content(newValue);
}
For this method, VS calculated the cyclomatic complexity as 10. However, there are only 7 case statement, I don't understand what other expressions contribute to the complexity.
I had search through many sources, but could not get a complete lists of all expressions which will be counted.
Can anyone help on this? Or there is any tool which i can generate Control Flow Diagram from C# code?
Thank you in advance...