如何使这个c#代码小或重构此代码

时间:2014-08-27 08:27:15

标签: c#

我是编码新手,最近我读到了有关代码重构的文章。所以我已经制作了一个用于预订船上房间的控制台应用程序。我认为在我的项目中只有两部分我需要重构,如下所示。 一个是if else声明。

        ship1 = new Ship("Olympic Countess");

        ArrayList groupA = new ArrayList();
        for (int i = 0; i < 10; i++)
        {
            groupA.Add(new room(5000, "A" + (i + 1)));
        }

        ArrayList groupB = new ArrayList();
        for (int i = 0; i < 10; i++)
        {
            groupB.Add(new room(4000, "B" + (i + 1)));
        }

        ArrayList groupC = new ArrayList();
        for (int i = 0; i < 30; i++)
        {
            groupC.Add(new room(3500, "C" + (i + 1)));
        }

        ArrayList groupD = new ArrayList();
        for (int i = 0; i < 36; i++)
        {
            groupD.Add(new room(3400, "D" + (i + 1)));
        }

        ArrayList groupE = new ArrayList();
        for (int i = 0; i < 40; i++)
        {
            groupE.Add(new room(3300, "E" + (i + 1)));
        }

        ArrayList groupF = new ArrayList();
        for (int i = 0; i < 30; i++)
        {
            groupF.Add(new room(3400, "F" + (i + 1)));
        }

        ArrayList groupG = new ArrayList();
        for (int i = 0; i < 36; i++)
        {
            groupG.Add(new room(3300, "G" + (i + 1)));
        }

        ArrayList groupH = new ArrayList();
        for (int i = 0; i < 40; i++)
        {
            groupH.Add(new room(3200, "H" + (i + 1)));
        }

        ship1.addDeck("Balcony Suite", groupA);
        ship1.addDeck("Suite", groupB);
        ship1.addDeck("Deck 3 - Outside Twin", groupC);
        ship1.addDeck("Deck 2 - Outside Twin", groupD);
        ship1.addDeck("Deck 1 - Outside Twin", groupE);
        ship1.addDeck("Deck 3 - Inside Twin", groupF);
        ship1.addDeck("Deck 2 - Inside Twin", groupG);
        ship1.addDeck("Deck 1 - Inside Twin", groupH);      
    }

另一个是if else声明如下

    public Reservation bookPassage(String cabinclass, Customer booker, int number)
      {
        ArrayList cabins;
        if (cabinclass.Equals("a", StringComparison.OrdinalIgnoreCase))
            cabins = ship1.getDeck("Balcony Suite");
        else if (cabinclass.Equals("b", StringComparison.OrdinalIgnoreCase))
            cabins = ship1.getDeck("Suite");
        else if (cabinclass.Equals("c", StringComparison.OrdinalIgnoreCase))
            cabins = ship1.getDeck("Deck 3 - Outside Twin");
        else if (cabinclass.Equals("d", StringComparison.OrdinalIgnoreCase))
            cabins = ship1.getDeck("Deck 2 - Outside Twin");
        else if (cabinclass.Equals("e", StringComparison.OrdinalIgnoreCase))
            cabins = ship1.getDeck("Deck 1 - Outside Twin");
        else if (cabinclass.Equals("f", StringComparison.OrdinalIgnoreCase))
            cabins = ship1.getDeck("Deck 3 - Inside Twin");
        else if (cabinclass.Equals("g", StringComparison.OrdinalIgnoreCase))
            cabins = ship1.getDeck("Deck 2 - Inside Twin");
        else 
            cabins = ship1.getDeck("Deck 1 - Inside Twin");

我不明白的是,我的参数在两个逻辑中都在变化。 那么当我的机舱每次改变时,如何为这种逻辑制作一个单独的方法呢?

3 个答案:

答案 0 :(得分:1)

看一下这段代码,你的代码中很可能会有更多的部分应该(或可能)被重构。

让我们从提取常量开始。只需将每个常量字符串(例如&#34; Deck 1 - Inside Twin&#34;)放入const中。如果您尝试重命名任何套牌,这将对您有所帮助。

然后你应该坚持常见的编码约定。 C#中的类名称始终以大写字母开头(将room重命名为Room)。

然后你可以从

中提取一个方法
new ArrayList();
for (int i = 0; i < 30; i++)
{
    groupC.Add(new room(3500, "C" + (i + 1)));
}

然后你可以使用Dictionary或类似的东西代替你的if语句来提取正确的牌组,这只会删除if-then-else。

你可以更多地重构以使代码更好:)

答案 1 :(得分:1)

所有迭代都可以连接在一起,因此可以在一次运行中执行,如:

ArrayList groupA = new ArrayList();
    ArrayList groupB = new ArrayList();
    ArrayList groupC = new ArrayList();
    ArrayList groupD = new ArrayList();
    ArrayList groupE = new ArrayList();
    ArrayList groupF = new ArrayList();
    ArrayList groupG = new ArrayList();
    ArrayList groupH = new ArrayList(); 

    for (int i = 0; i < 40; i++)
    {           
        if (i < 10)
        {
            groupA.Add(new room(5000, "A" + (i + 1)));
            groupB.Add(new room(4000, "B" + (i + 1)));
        }
        if (i < 30)
        {
            groupF.Add(new room(3400, "F" + (i + 1)));
            groupC.Add(new room(3500, "C" + (i + 1)));
        }
        if (i < 40)
        {
            groupE.Add(new room(3300, "E" + (i + 1)));
            groupH.Add(new room(3200, "H" + (i + 1)));          
        }
        if (i < 36)
        {
            groupD.Add(new room(3400, "D" + (i + 1)));
            groupG.Add(new room(3300, "G" + (i + 1)));
        }               
    }

    ship1.addDeck("Balcony Suite", groupA);
    ship1.addDeck("Suite", groupB);
    ship1.addDeck("Deck 3 - Outside Twin", groupC);
    ship1.addDeck("Deck 2 - Outside Twin", groupD);
    ship1.addDeck("Deck 1 - Outside Twin", groupE);
    ship1.addDeck("Deck 3 - Inside Twin", groupF);
    ship1.addDeck("Deck 2 - Inside Twin", groupG);
    ship1.addDeck("Deck 1 - Inside Twin", groupH);   

并且可以在cse lik中加入一堆互斥的if:

    switch (cabinclass.ToLower())
{
    case "a":
        cabins = ship1.getDeck("Balcony Suite");
        break;
    case "b":
        cabins = ship1.getDeck("Suite");
        break;
    case "c":
        cabins = ship1.getDeck("Deck 3 - Outside Twin");
        break;
    case "d":
        cabins = ship1.getDeck("Deck 2 - Outside Twin");
        break;
    case "e":
        cabins = ship1.getDeck("Deck 1 - Outside Twin");
        break;
    case "f":
        cabins = ship1.getDeck("Deck 3 - Inside Twin");
        break;
    case "g":
        cabins = ship1.getDeck("Deck 2 - Inside Twin");
        break;
    default:
        cabins = ship1.getDeck("Deck 1 - Inside Twin");
        break;              
}

答案 2 :(得分:0)

使用参数化功能。

E.g。在

    ArrayList groupC = new ArrayList();
    for (int i = 0; i < 30; i++)
    {
        groupC.Add(new room(3500, "C" + (i + 1)));
    }

项目不同的是:groupC,30,3500和“C”。

使用上述项作为参数创建一个方法,并使用每个片段的正确参数调用该方法。