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.
Comments on Why static code analyzers such as SonarQube indicate a high code complexity for switch statements?
Post
Why static code analyzers such as SonarQube indicate a high code complexity for switch statements?
During a presentation of a pipeline configuration, a colleague showed a SonarQube integration and one of its reports. A warning was caused by overrunning the max value for the code complexity threshold in a function containing a fairly large switch statement.
Why are switch statements considered too complex and what to do about them?
The problematic code was similar to the one below, but containing about double of the case statements:
enum FooStasus
{
ToBeStarted = 1,
Starting = 2,
OnGoing = 3,
OnHold = 4,
Stopped = 5
}
private static double GetWeight(FooStasus status)
{
switch (status)
{
case FooStasus.ToBeStarted:
case FooStasus.Starting: return 1.0;
case FooStasus.OnGoing: return 2.0;
case FooStasus.OnHold: return 0.2;
case FooStasus.Stopped: return 0;
default: return 0.5;
}
}
1 comment thread