Search code examples
c#visual-studiocode-metricscyclomatic-complexity

Why cyclomatic complexity of code with `switch` higher than that of with 'if-else'


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

Solution

  • The Visual Studio Cyclomatic Complexity (CC) tool calculates values from IL code and thus, is subject to compiler details.

    Here you are stumbling on a performance compiler detail: when a switch over a string has strictly more than 6 cases, the compiler creates a hash table for fast string search. This hash table doesn't appear in the C# code but only in the IL code. The IL code becomes more complex to handle this hash table and you get the unexpected CC value. This situation is well illustrated by this link from Michael Randall.

    Alternatively you can use the tool NDepend to compute the CC from C# source code, visualize CC values in a colored treemap and run some rules to forbid too complex methods.

    Colored Treemap Cyclomatic Complexity

    (Disclaimer I work for NDepend)