Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A Why static code analyzers such as SonarQube indicate a high code complexity for switch statements?

I would skip the theoretical part of actually computing the cyclomatic complexity of a switch statement and mention that it can see as a bunch of if statements. Since each if adds to the complexity...

posted 3y ago by Alexei‭  ·  edited 3y ago by Alexei‭

Answer
#3: Post edited by user avatar Alexei‭ · 2022-02-05T19:06:56Z (almost 3 years ago)
added information based on feedback
  • I would skip the theoretical part of actually computing the cyclomatic complexity of a switch statement and mention that it can see as a bunch of `if` statements. Since each `if` adds to the complexity, the higher the number of `case`s, the higher the complexity.
  • While a simple switch such as the one in the question is simple to read and maintain, if it grows way larger, it becomes easier to introduce bugs, even if C# prevents the flow control from passing from one case to another if the case has at least one instruction (i.e. each nonempty case must use `break` or `return`).
  • One way to mitigate this relies on the actual code complexity. If this is a simple mapping, a simple `Dictionary` can be used. Example:
  • ```c#
  • private static Dictionary<FooStasus, double> _statusMap = new()
  • {
  • { FooStasus.ToBeStarted, 1.0 },
  • { FooStasus.Starting, 1.0 },
  • { FooStasus.OnGoing, 2.0 },
  • { FooStasus.OnHold, 0.1 },
  • { FooStasus.Stopped, 0 },
  • };
  • private static double GetWeightSimple(FooStasus status) =>
  • _statusMap.GetValueOrDefault(status, 0.5);
  • ```
  • If the logic is more complex, the dictionary can hold `Action`s or `Func`s that might rely on the case value to perform some logic. If they are more than in the following example, it would be wise to define them as separate functions:
  • ```c#
  • private static Dictionary<FooStasus, Func<FooStasus, double>> GetWeightActMap = new()
  • {
  • { FooStasus.ToBeStarted, _ => { Console.WriteLine("To be started"); return 1.0;} },
  • { FooStasus.Starting, status => { Console.WriteLine($"Status is {status}"); return 1.0; } },
  • { FooStasus.OnGoing, _ => 2.0 },
  • { FooStasus.OnHold, _ => 0.1 },
  • { FooStasus.Stopped, _ => default },
  • };
  • private static double GetWeightAct(FooStasus status) =>
  • GetWeightActMap.GetValueOrDefault(status)?.Invoke(status) ?? default;
  • ```
  • I would skip the theoretical part of actually computing the cyclomatic complexity of a switch statement and mention that it can see as a bunch of `if` statements. Since each `if` adds to the complexity, the higher the number of `case`s, the higher the complexity.
  • While a simple switch such as the one in the question is simple to read and maintain, if it grows way larger, it becomes easier to introduce bugs, even if C# prevents the flow control from passing from one case to another if the case has at least one instruction (i.e. each nonempty case must use `break` or `return`).
  • One way to mitigate this relies on the actual code complexity. If this is a simple mapping, a simple `Dictionary` can be used. Example:
  • ```c#
  • private static Dictionary<FooStasus, double> _statusMap = new()
  • {
  • { FooStasus.ToBeStarted, 1.0 },
  • { FooStasus.Starting, 1.0 },
  • { FooStasus.OnGoing, 2.0 },
  • { FooStasus.OnHold, 0.1 },
  • { FooStasus.Stopped, 0 },
  • };
  • private static double GetWeightSimple(FooStasus status) =>
  • _statusMap.GetValueOrDefault(status, 0.5);
  • ```
  • If the logic is more complex, the dictionary can hold `Action`s or `Func`s that might rely on the case value to perform some logic. If they are more than in the following example, it would be wise to define them as separate functions:
  • ```c#
  • private static Dictionary<FooStasus, Func<FooStasus, double>> GetWeightActMap = new()
  • {
  • { FooStasus.ToBeStarted, _ => { Console.WriteLine("To be started"); return 1.0;} },
  • { FooStasus.Starting, status => { Console.WriteLine($"Status is {status}"); return 1.0; } },
  • { FooStasus.OnGoing, _ => 2.0 },
  • { FooStasus.OnHold, _ => 0.1 },
  • { FooStasus.Stopped, _ => default },
  • };
  • private static double GetWeightAct(FooStasus status) =>
  • GetWeightActMap.GetValueOrDefault(status)?.Invoke(status) ?? default;
  • ```
  • However, as suggested by `elgonzo` this should be avoided for anything that is a little more complex than my trivial example and use (local) functions instead, since they provide meaning to each case.
  • Also, whenever the mapping list is fairly long, a table might be more suitable for storage.
#2: Post edited by user avatar Alexei‭ · 2022-02-05T15:50:34Z (almost 3 years ago)
added complexity clarification
  • I would skip the theoretical part of actually computing the cyclomatic complexity of a switch statement and mention that it can see as a bunch of `if` statements. Since each `if` adds to the complexity, the higher the number of `case`s, the higher the complexity.
  • One way to mitigate this relies on the actual code complexity. If this is a simple mapping, a simple `Dictionary` can be used. Example:
  • ```c#
  • private static Dictionary<FooStasus, double> _statusMap = new()
  • {
  • { FooStasus.ToBeStarted, 1.0 },
  • { FooStasus.Starting, 1.0 },
  • { FooStasus.OnGoing, 2.0 },
  • { FooStasus.OnHold, 0.1 },
  • { FooStasus.Stopped, 0 },
  • };
  • private static double GetWeightSimple(FooStasus status) =>
  • _statusMap.GetValueOrDefault(status, 0.5);
  • ```
  • If the logic is more complex, the dictionary can hold `Action`s or `Func`s that might rely on the case value to perform some logic. If they are more than in the following example, it would be wise to define them as separate functions:
  • ```c#
  • private static Dictionary<FooStasus, Func<FooStasus, double>> GetWeightActMap = new()
  • {
  • { FooStasus.ToBeStarted, _ => { Console.WriteLine("To be started"); return 1.0;} },
  • { FooStasus.Starting, status => { Console.WriteLine($"Status is {status}"); return 1.0; } },
  • { FooStasus.OnGoing, _ => 2.0 },
  • { FooStasus.OnHold, _ => 0.1 },
  • { FooStasus.Stopped, _ => default },
  • };
  • private static double GetWeightAct(FooStasus status) =>
  • GetWeightActMap.GetValueOrDefault(status)?.Invoke(status) ?? default;
  • ```
  • I would skip the theoretical part of actually computing the cyclomatic complexity of a switch statement and mention that it can see as a bunch of `if` statements. Since each `if` adds to the complexity, the higher the number of `case`s, the higher the complexity.
  • While a simple switch such as the one in the question is simple to read and maintain, if it grows way larger, it becomes easier to introduce bugs, even if C# prevents the flow control from passing from one case to another if the case has at least one instruction (i.e. each nonempty case must use `break` or `return`).
  • One way to mitigate this relies on the actual code complexity. If this is a simple mapping, a simple `Dictionary` can be used. Example:
  • ```c#
  • private static Dictionary<FooStasus, double> _statusMap = new()
  • {
  • { FooStasus.ToBeStarted, 1.0 },
  • { FooStasus.Starting, 1.0 },
  • { FooStasus.OnGoing, 2.0 },
  • { FooStasus.OnHold, 0.1 },
  • { FooStasus.Stopped, 0 },
  • };
  • private static double GetWeightSimple(FooStasus status) =>
  • _statusMap.GetValueOrDefault(status, 0.5);
  • ```
  • If the logic is more complex, the dictionary can hold `Action`s or `Func`s that might rely on the case value to perform some logic. If they are more than in the following example, it would be wise to define them as separate functions:
  • ```c#
  • private static Dictionary<FooStasus, Func<FooStasus, double>> GetWeightActMap = new()
  • {
  • { FooStasus.ToBeStarted, _ => { Console.WriteLine("To be started"); return 1.0;} },
  • { FooStasus.Starting, status => { Console.WriteLine($"Status is {status}"); return 1.0; } },
  • { FooStasus.OnGoing, _ => 2.0 },
  • { FooStasus.OnHold, _ => 0.1 },
  • { FooStasus.Stopped, _ => default },
  • };
  • private static double GetWeightAct(FooStasus status) =>
  • GetWeightActMap.GetValueOrDefault(status)?.Invoke(status) ?? default;
  • ```
#1: Initial revision by user avatar Alexei‭ · 2022-02-05T15:46:02Z (almost 3 years ago)
I would skip the theoretical part of actually computing the cyclomatic complexity of a switch statement and mention that it can see as a bunch of `if` statements. Since each `if` adds to the complexity, the higher the number of `case`s, the higher the complexity.

One way to mitigate this relies on the actual code complexity. If this is a simple mapping, a simple `Dictionary` can be used. Example:

```c#
private static Dictionary<FooStasus, double> _statusMap = new()
{
    { FooStasus.ToBeStarted, 1.0 },
    { FooStasus.Starting, 1.0 },
    { FooStasus.OnGoing, 2.0 },
    { FooStasus.OnHold, 0.1 },
    { FooStasus.Stopped, 0 },
};

private static double GetWeightSimple(FooStasus status) =>
    _statusMap.GetValueOrDefault(status, 0.5);
```

If the logic is more complex, the dictionary can hold `Action`s or `Func`s that might rely on the case value to perform some logic. If they are more than in the following example, it would be wise to define them as separate functions:

```c#
private static Dictionary<FooStasus, Func<FooStasus, double>> GetWeightActMap = new()
{
    { FooStasus.ToBeStarted, _ => { Console.WriteLine("To be started"); return 1.0;} },
    { FooStasus.Starting, status => { Console.WriteLine($"Status is {status}"); return 1.0; } },
    { FooStasus.OnGoing, _ => 2.0 },
    { FooStasus.OnHold, _ => 0.1 },
    { FooStasus.Stopped, _ => default },
};

private static double GetWeightAct(FooStasus status) =>
    GetWeightActMap.GetValueOrDefault(status)?.Invoke(status) ?? default;
```