There is an function which has switch case and we need to reduce its CC
string data = string.empty;
switch (value)
{
case "Less than 2 billion":
data = "0 - 2B";
break;
case "2 billion to 10 billion":
data = "2B - 10B";
break;
case "10 billion to 20 billion":
data = "10B - 20B";
break;
case "20 billion to 50 billion":
data = "20B - 50B";
break;
case "Greater than 50 billion":
data = "> 50B";
break;
case "N/A":
data = "N/A";
break;
case "[items] > 0":
data = string.Empty;
break;
}
return data;
Avoid use of switch/case statements in your code. Use Factory or Strategy design patterns instead. Complexity of 8 (1 for each CASE and 1 for method itself). Refactoring using Factory design pattern and complexity changed to 1.
It is at worst O(n). Sometimes (and this is language and compiler dependent), it translates to a jump table lookup (for "nice" switches that don't have too large a case range). Then that is O(1).
You could use a dictionary lookup in this case, it would be a little less code and clearer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With