如何简化switch语句以减少圈复杂度

时间:2019-12-19 10:30:53

标签: c# switch-statement refactoring

我有一个很大的switch语句,其圈复杂度为31,必须将其重构为至少25。 这是错误:严重级代码描述项目文件行抑制状态抑制状态 错误CA1502'Worker.StartListening()'的循环复杂度为31。重写或重构该方法可将复杂度降低到25。

谢谢!

代码如下:

public void StartListening()
    {
        var consumerSettingsSection= this.configurationManager.GetSection<ConsumerSettingsSection>("appZ/consumer");
        foreach (var setting in consumerSettingsSection.QueueSettings)
        {
            var eventType = ConsumedEventType.NotSpecified;

            switch (setting.Name)
            {
                case "A":
                    eventType = ConsumedEventType.A;
                    break;

                case "B":
                    eventType = ConsumedEventType.B;
                    break;

                case "C":
                    eventType = ConsumedEventType.C;
                    break;

                case "D":
                    eventType = ConsumedEventType.D;
                    break;

                case "E":
                    eventType = ConsumedEventType.E;
                    break;

                case "F":
                    eventType = ConsumedEventType.F;
                    break;

                case "G":
                    eventType = ConsumedEventType.G;
                    break;

                case "H":
                    eventType = ConsumedEventType.H;
                    break;

                case "I":
                    eventType = ConsumedEventType.I;
                    break;

                case "J":
                    eventType = ConsumedEventType.J;
                    break;

                case "K":
                    eventType = ConsumedEventType.K;
                    break;

                case "L":
                    eventType = ConsumedEventType.L;
                    break;

                default:
                    eventType = ConsumedEventType.NotSpecified;
                    break;
            }

            var consumer = new ChannelConsumer(setting, eventType);
            consumer.MessageConsumed += this.Consumer_MessageConsumed;
            consumer.StartConsuming();
        }
    }

2 个答案:

答案 0 :(得分:7)

您可以尝试使用Enum.TryParse method来简化代码:

 if (Enum.TryParse(setting.Name, true, out eventType)) 
     return eventType;
 else
     return ConsumedEventType.NotSpecified;

您可以使用名称设置为事件类型的字符串进行解析,使用解析后的值,否则返回NotSpecified值。

比维护值列表更容易。根据上面的代码,您可以编写类似的内容

foreach (var setting in consumerSettingsSection.QueueSettings)
{
    var eventType = ConsumedEventType.NotSpecified;
    if (Enum.TryParse(setting.Name, true, out ConsumedEventType parsedEvent))
    {
         eventType = parsedEvent;
    }
    //rest of code
}

甚至更容易

if (!Enum.TryParse(setting.Name, true, out ConsumedEventType eventType))
{
    eventType = ConsumedEventType.NotSpecified;
}

请记住,仅C#7支持内联out变量

答案 1 :(得分:3)

使用Dictionary<string, ConsumedEventType>

var d = new Dictionary<string, ConsumedEventType>() 
{
    { "A", ConsumedEventType.A },
    { "B", ConsumedEventType.B },
    { "C", ConsumedEventType.C },
    ...
}

现在获取适合您的settings.Name的实际值:

ConsumedEventType type;
var type = !d.ContainsKey(settings.Name)
    ConsumedEventType.NotSpecified : 
    d[setting.sName];