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

本文关键字:代码 重构 何使这 | 更新日期: 2023-09-27 18:19:29

我是编码新手,最近读了一篇关于代码重构的文章。所以我做了一个控制台应用程序来预订船上的房间。我认为在我的项目中,只有两个部分需要重构,如下所示。一个是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");

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

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

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

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;              
}

看看这段代码,代码中很可能还有更多的部分需要重构。

让我们从提取常量开始。只需将每个常量字符串(如"Deck 1-Inside Twin")放入一个常量中。如果您尝试重命名任何甲板,这将对您有所帮助。

然后,您应该坚持通用的编码约定。C#中的类名总是以大写字母开头(将room重命名为Room)。

然后你可以从中提取一种方法

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

然后,您可以使用Dictionary或类似的语句来提取正确的deck,而不是if语句,只需删除if-Then-else即可。

您可能还可以进行更多的重构以使代码变得更好:)

使用参数化函数。

例如在中

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

不同的项目有:C组、30组、3500组和"C"组。

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