I have two sample functions TestComplexityIf
and TestComplexitySwitch
. VisualStudio-2017 'Calculate Code Metrics' tool reports a cyclomatic complexity of 10 for the function with switch
statement ad 7 for the one with if-else
. I wonder how the complexity for TestComplexitySwitch() is calculated.
private static void TestComplexityIf(String arg)
{
if (arg == "a")
{ }
else if (arg == "b")
{ }
else if (arg == "c")
{ }
else if (arg == "d")
{ }
else if (arg == "d")
{ }
else if (arg == "f")
{ }
else if (arg == "g")
{ }
}
private static void TestComplexitySwitch(String arg)
{
switch (arg)
{
case "a":
break;
case "b":
break;
case "c":
break;
case "d":
break;
case "e":
break;
case "f":
break;
case "g":
break;
}
}
Also, if I comment the last case, complexity suddenly changes to 6.
private static void TestComplexitySwitch(String arg)
{
switch (arg)
{
case "a":
break;
case "b":
break;
case "c":
break;
case "d":
break;
case "e":
break;
case "f":
break;
//case "g":
//break;
}
}