6

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?

Nigel
  • 2,961
  • 1
  • 14
  • 32
  • 1
    If I had to guess, it's because no one ever a)marked it as a bug, b)created it as a task on the _switch expression_ story story, or c)bothered. Switch expressions are pretty new, maybe they'll fix it in time – Flydog57 Jun 23 '22 at 22:03

0 Answers0