之一,当应尽量消除switch语句? [英] When should one try to eliminate a switch statement?

查看:228
本文介绍了之一,当应尽量消除switch语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的代码库switch语句我工作,我试图找出如何更好的东西,因为的 switch语句被认为是一个代码味道。不过,在通过几个岗位上有关的替换切换 语句我似乎无法想象来代替这个特殊的switch语句的有效途径。

I've come across a switch statement in the codebase I'm working on and I'm trying to figure out how to replace it with something better since switch statements are considered a code smell. However, having read through several posts on stackoverflow about replacing switch statements I can't seem to think of an effective way to replace this particular switch statement.

它让我想,如果这个特殊的switch语句是确定的,如果有。在哪里switch语句被认为是适当的具体情况

Its left me wondering if this particular switch statement is ok and if there are particular circumstances where switch statements are considered appropriate.

在我的情况下,代码(略自然混淆)是我与挣扎是这样的:

In my case the code (slightly obfuscated naturally) that I'm struggling with is like this:

private MyType DoSomething(IDataRecord reader)
{
    var p = new MyType
                {
                   Id = (int)reader[idIndex],
                   Name = (string)reader[nameIndex]
                }

    switch ((string) reader[discountTypeIndex])
    {
        case "A":
            p.DiscountType = DiscountType.Discountable;
            break;
        case "B":
            p.DiscountType = DiscountType.Loss;
            break;
        case "O":
            p.DiscountType = DiscountType.Other;
            break;
    }

    return p;
}



任何人都可以提出一个方法来消除此开关?或者这是一个合适的使用交换机?如果是,是否有switch语句其他适当的用途?我真的很想知道他们是合适的,所以我不会浪费太多时间试图消除每一个switch语句中我碰到过,只是因为他们被认为在某些情况下气味。

Can anyone suggest a way to eliminate this switch? Or is this an appropriate use of a switch? And if it is, are there other appropriate uses for switch statements? I'd really like to know where they are appropriate so I don't waste too much time trying to eliminate every switch statement I come across just because they are considered a smell in some circumstances.

更新:迈克尔我做了一些寻找这个逻辑的重复,发现有人在另一个类中,有效地使整个switch语句创建冗余的逻辑。所以在这个代码特定比特的上下文switch语句是不必要的。不过,我的问题是更多的代码switch语句的恰当性和我们是否应该总是试图取代他们时,他们被发现所以在这种情况下,我倾向于接受的答案,这个switch语句是合适的。

Update: At the suggestion of Michael I did a bit of searching for duplication of this logic and discovered that someone had created logic in another class that effectively made the whole switch statement redundant. So in the context of this particular bit of code the switch statement was unnecessary. However, my question is more about the appropriateness of switch statements in code and whether we should always try to replace them whenever they are found so in this case I'm inclined to accept the answer that this switch statement is appropriate.

推荐答案

这是一个switch叙述的适当使用,因为它使选择易读,易增加或减少的。

This is an appropriate use for a switch statment, as it makes the choices readable, and easy to add or subtract one.

请参阅此链接。

这篇关于之一,当应尽量消除switch语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆