如何降低Switch案例陈述的循环复杂性
问题描述:
有一个带有开关盒的功能,我们需要减少它的CC
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;
答
在这种情况下,您可以使用字典查找,这样可以节省一些代码并且更加清晰.
You could use a dictionary lookup in this case, it would be a little less code and clearer.