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
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 thres...
#4: Post edited
During a presentation of a pipeline configuration, a colleague showed a SonarQube integration and of its reports. One warning was related by overrunning the max value for the code complexity 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:
- ```c#
- 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;
- }
- }
- ```
- []()
- 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:
- ```c#
- 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;
- }
- }
- ```
- []()
#2: Post edited
- During a presentation of a pipeline configuration, a colleague showed a SonarQube integration and of its reports. One warning was related by overrunning the max value for the code complexity in a function containing a fairly large switch statement.
Why are switch statements considered too complex and what to do about them?
- During a presentation of a pipeline configuration, a colleague showed a SonarQube integration and of its reports. One warning was related by overrunning the max value for the code complexity 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:
- ```c#
- 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: Initial revision
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 of its reports. One warning was related by overrunning the max value for the code complexity in a function containing a fairly large switch statement. Why are switch statements considered too complex and what to do about them?