Why aren't switch expressions counted in cyclomatic complexity?

113 Views Asked by At

Let's say I have this method:

void DoStuff(int a)
{
    int b;
    switch (a)
    {
        case 1: 
            b = 5;
            break;
        case 2:
            b = 100;
            break;
        default:
            b = 0;
            break;
    }
    Console.WriteLine(b);
}

Visual studio gives a cyclomatic complexity of 4 (seems to me that the actual cyclomatic complexity is 3 but that is beside the point).

Now if I refactor the code using a switch expression I get this:

void DoStuff(int a)
{
    int b = a switch
    {
        1 => 5,
        2 => 100,
        _ => 0
    };
    Console.WriteLine(b);
}

Visual studio now says the cyclomatic complexity is 1, even though the code is doing the exact same thing. Has the cyclomatic complexity actually been reduced? If so, how?

0

There are 0 best solutions below