用相似的字符串重构if中的大量if和comparis以匹配

时间:2017-09-14 18:09:51

标签: c# .net refactoring

这是我所做的示例,关于应该用于回滚操作系统并根据已经安装在PC中的版本安装它的版本,唯一的限制是它应该在.NET 2.0中

重构此代码的最佳方法是考虑到版本的第一部分字符串是共同的。

在版本“8.40.1”的情况下,(文本)在内部是重要的,它们是不同的,而不像其他只是同一版本的不同名称。

//previous
if (version.Equals("8.80.0(PrinterData)") || version.Equals("8.80.0(PrinterData)(Box)") || version.Equals("8.80.0(Box)") || version.Equals("8.80.0"))
{
    myPCVersion = "8.40.1(PrinterData)";
}
else if (version.Equals("8.80.0(CNA)") || version.Equals("8.80.0(CNA)(Box)") || version.Equals("8.80.0(Box)") || version.Equals("8.80.0"))
{
    myPCVersion = "8.40.1(CNA)";
}
else if (version.Equals("7.50.0(Windows 7 Home)") || version.Equals("7.50.0(Windows 7 Home)(Box)") || version.Equals("7.50.0(Box)") || version.Equals("7.50.0"))
{
    myPCVersion = "7.0.b(Windows 7 Home)";
}
else if (version.Equals("6.50.0(Windows 8 Pro)") || version.Equals("6.50.0(Windows 8 Pro)(Box)") || version.Equals("6.50.0(Box)") || version.Equals("6.50.0"))
{
    myPCVersion = "6.42.1(Windows 8 Pro)";
}
else if (version.Equals("2.50.0(Home)") || version.Equals("2.50.0(Home)(Box)") || version.Equals("2.50.0(Box)") || version.Equals("2.50.0"))
{
    myPCVersion = "2.4.0(Home)";
}




//now
if (version.Equals("8.40.1(PrinterData)") || version.Equals("8.40.1(PrinterData)(Box)") || version.Equals("8.40.1(Box)") || version.Equals("8.40.1"))
{
    myPCVersion = actionToRun.Equals("Install") ? "8.80.0(PrinterData)" : "8.40.1(PrinterData)";
}
else if (version.Equals("8.40.1(CNA)") || version.Equals("8.40.1(CNA)(Box)") || version.Equals("8.40.1(Box)") || version.Equals("8.40.1"))
{
    myPCVersion = actionToRun.Equals("Install") ? "8.80.0(CNA)" : "8.40.1(CNA)";
}
else if (version.Equals("7.0.b(Windows 7 Home)") || version.Equals("7.0.b(Windows 7 Home)(Box)") || version.Equals("7.0.b(Box)") || version.Equals("7.0.b"))
{
    myPCVersion = actionToRun.Equals("Install") ? "7.50.0(Windows 7 Home)" : "7.0.b(Windows 7 Home)";
}
else if (version.Equals("6.42.1(Windows 8 Pro)") || version.Equals("6.42.1(Windows 8 Pro)(Box)") || version.Equals("6.42.1(Box)") || version.Equals("6.42.1"))
{
    myPCVersion = actionToRun.Equals("Install") ? "6.50.0(Windows 8 Pro)" : "6.42.1(Windows 8 Pro)";
}
else if (version.Equals("2.4.0(Home)") || version.Equals("2.4.0(Home)(Box)") || version.Equals("2.4.0(Box)") || version.Equals("2.4.0"))
{
    myPCVersion = actionToRun.Equals("Install") ? "2.50.0(Home)" : "2.4.0(Home)";
}
Console.WriteLine();
Console.WriteLine("New Version: " + myPCVersion);

1 个答案:

答案 0 :(得分:1)

正如许多人所说,字典是用于重构if / switch语句的数据结构的类型。这是我嘲笑的东西,可以让你接近你需要的东西。

public class VersionMap
{
    public String Install { get; private set; }
    public String Rollback { get; private set; }

    public VersionMap(String install, String rollback)
    {
        Install = install;
        Rollback = rollback;
    }
}

public String GetVersion(String version, String actionToRun)
{
    var _8800PrinterData = new VersionMap("8.80.0(PrinterData)", "8.40.1(PrinterData)");
    var _8800CNA = new VersionMap("8.80.0(CNA)", "8.40.1(CNA)");

    var map = new Dictionary<string, VersionMap>
    {
        { "8.40.1(PrinterData)", _8800PrinterData },
        { "8.40.1(PrinterData)(Box)", _8800PrinterData },
        { "8.40.1(Box)", _8800PrinterData },
        { "8.40.1", _8800PrinterData },
        { "8.40.1(CNA)", _8800CNA },
        { "8.40.1(CNA)(Box)", _8800CNA },
        { "8.40.1(Box)",_8800CNA },
    };

    return actionToRun.Equals("Install") ? map[version].Install : map[version].Rollback;
}

可能需要进行修改才能在2.0中使用它,我不知道2.0的确切语法差异

编辑:我还建议您在开始进行更改之前,添加一些单元测试,以确保您的当前功能在重构时不会发生变化。