【问题标题】:Is there any way to improve the maintainability index and cyclomatic complexity of this code snippet? [closed]有什么办法可以提高这段代码片段的可维护性指数和圈复杂度? [关闭]
【发布时间】:2015-01-14 11:38:44
【问题描述】:

好的,这就是我在应用程序某些地方的代码 sn-p。我需要提高这个函数的可维护性指数和圈复杂度:

private HotelBookLogEntry ParseService(XmlReader xmlReader, HotelBookLogEntry hotelBookLogEntry)
    {
        while (!xmlReader.EOF)
        {
            if (xmlReader.IsStartElement())
            {
                switch (xmlReader.Name.ToLower())
                {
                    case "datefrom":
                        hotelBookLogEntry.HotelBookCriteria.CheckInDate = DateTime.ParseExact(xmlReader.GetAttribute("date"), "yyyymmdd", CultureInfo.InvariantCulture);
                        xmlReader.Read();
                        break;
                    case "dateto":
                        hotelBookLogEntry.HotelBookCriteria.CheckOutDate = DateTime.ParseExact(xmlReader.GetAttribute("date"), "yyyymmdd", CultureInfo.InvariantCulture);
                        xmlReader.Read();
                        break;
                    case "currency":
                        hotelBookLogEntry.HotelBookCriteria.RequestedCurrency = xmlReader.GetAttribute("code");
                        xmlReader.Read();
                        break;
                    case "hotelinfo":
                        xmlReader.ReadToDescendant("Code");
                        hotelBookLogEntry.HotelBookCriteria.SupplierHotelId = xmlReader.ReadElementContentAsString();
                        break;
                    case "availableroom":
                        hotelBookLogEntry = ParseAvailableRoom(xmlReader.ReadSubtree(), hotelBookLogEntry);
                        break;
                    case "errorlist":
                        hotelBookLogEntry = GetErrors(xmlReader, hotelBookLogEntry);
                        break;
                    default:
                        xmlReader.Read();
                        break;
                }
            }
            else
            {
                xmlReader.Read();
            }
        }
        return hotelBookLogEntry;

基本上,switch case 中使用了不同的方法,具有不同的参数和返回类型,以及不同的字符串集,但常见的是while loop and the if else conditions。我需要找到一种方法让这段代码“不那么复杂”,尤其是在使用的条件和循环的数量方面。 我有将 switch case 移动到一个方法然后调用它的想法,但这仍然可以。 那么,有没有办法改进这段代码呢?

编辑:不幸的是,我被特别要求不要使用 Object-XML 序列化或 XMLDoc

【问题讨论】:

  • 您可以应用多态性。使用单个方法为 switch case 中的每个 case 字符串分隔派生类型。以及创建派生类型实例的工厂。
  • 这个问题似乎跑题了,因为它属于codereview.stackexchange.com
  • 将您的源 xml 序列化为对象并使用它们创建 HotelBookLogEntry。

标签: c# xml parsing while-loop xmlreader


【解决方案1】:

即使我真的不知道这是否表现更好或产生更好的复杂性结果,您可以做的就是使用Dictionary 并为您的案例定义多个Actions。
对于像你这样的场景,我个人喜欢这个解决方案,因为它看起来非常漂亮,并且如果你必须添加更多案例,它不会弄乱你的代码。

我刚刚创建了一个简单的例子来说明我的意思:

class Program
{
    static void Main(string[] args)
    {
        var delegates = new Dictionary<char, Action<char>>
        {
            {'e', c => { Console.WriteLine("Found an 'e'"); }},
            {'o', c => { Console.WriteLine("Found an 'o'"); }}
        };

        var s = "Hello World";

        var it = s.GetEnumerator();
        while (it.MoveNext())
        {
            if (delegates.ContainsKey(it.Current))
                delegates[it.Current](it.Current);
        }

        Console.ReadKey();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    相关资源
    最近更新 更多