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
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...
Answer
#3: Post edited
- 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
- 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
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; ```