简化if-if-if以减小代码大小和可读性

本文关键字:可读性 代码 简化 if-if-if | 更新日期: 2023-09-27 18:20:16

我需要一些帮助来简化这个包含数百行的庞大代码,但我真的不知道如何做到这一点。代码看起来非常混乱,我需要的是返回带有预定义文本颜色的模型。有什么简单的方法吗?

我必须再解释一点:-有一份有很多型号的手机列表,如果有这些型号的手机,我需要打印出型号和颜色。该列表每小时/每天都在更改。。。这就是它的工作原理。因此,我需要用所有的模型填写我的代码,然后搜索可用的模型。此外,所有型号的"EndsWith"都有一个特定的代码(例如A23X),我认为这样搜索更快。

if (text.EndsWith("model-1")) model.Add(new Line { text = "Sony, Smartphone(model-1)", color = () => Sony.Color1 });
if (text.EndsWith("model-2")) model.Add(new Line { text = "Sony, Smartphone(model-2)", color = () => Sony.Color2 });
if (text.EndsWith("model-3")) model.Add(new Line { text = "Sony, Smartphone(model-3)", color = () => Sony.Color3 });
if (text.EndsWith("model-4")) model.Add(new Line { text = "Sony, Smartphone(model-4)", color = () => Sony.Color4 });
if (text.EndsWith("model-5")) model.Add(new Line { text = "Sony, Smartphone(model-5)", color = () => Sony.Color5 });
if (text.EndsWith("model-6")) model.Add(new Line { text = "Sony, Smartphone(model-6)", color = () => Sony.Color6 });
if (text.EndsWith("model-7")) model.Add(new Line { text = "Sony, Smartphone(model-7)", color = () => Sony.Color7 });
if (text.EndsWith("model-8")) model.Add(new Line { text = "Sony, Smartphone(model-8)", color = () => Sony.Color8 });
if (text.EndsWith("model-9")) model.Add(new Line { text = "Sony, Smartphone(model-9)", color = () => Sony.Color9 });
if (text.EndsWith("model-10")) model.Add(new Line { text = "Nokia, Smartphone(model-10)", color = () => Nokia.Color10 });
if (text.EndsWith("model-11")) model.Add(new Line { text = "Nokia, Smartphone(model-11)", color = () => Nokia.Color11 });
if (text.EndsWith("model-12")) model.Add(new Line { text = "Nokia, Smartphone(model-12)", color = () => Nokia.Color12 });
if (text.EndsWith("model-13")) model.Add(new Line { text = "Nokia, Smartphone(model-13)", color = () => Nokia.Color13 });
if (text.EndsWith("model-14")) model.Add(new Line { text = "Nokia, Smartphone(model-14)", color = () => Nokia.Color14 });
if (text.EndsWith("model-15")) model.Add(new Line { text = "Nokia, Smartphone(model-15)", color = () => Nokia.Color15 });
if (text.EndsWith("model-16")) model.Add(new Line { text = "Nokia, Smartphone(model-16)", color = () => Nokia.Color16 });
if (text.EndsWith("model-17")) model.Add(new Line { text = "Nokia, Smartphone(model-17)", color = () => Nokia.Color17 });
if (text.EndsWith("model-18")) model.Add(new Line { text = "Nokia, Smartphone(model-18)", color = () => Nokia.Color18 });

简化if-if-if以减小代码大小和可读性

您可以创建这样一个字典来从模型名称中查找Line实例:

Dictionary<string, Line> ModelNames = new Dictionary<string, Line>
{
    {"model-1", new Line { text = "Sony, Smartphone(model-1)", color = Sony.Color1 }},
    {"model-2", new Line { text = "Sony, Smartphone(model-2)", color = Sony.Color2 }},
    {"model-3", new Line { text = "Sony, Smartphone(model-3)", color = Sony.Color3 }},
    // ...
    {"model-10", new Line { text = "Nokia, Smartphone(model-10)", color = Nokia.Color10 }},
    {"model-11", new Line { text = "Nokia, Smartphone(model-11)", color = Nokia.Color11 }},
    {"model-12", new Line { text = "Nokia, Smartphone(model-12)", color = Nokia.Color12 }},
    // ...
};

现在你可以看看是否有一个已知的模型:

Line matchingModelNameLine = ModelNames
    .Where(kv => text.EndsWith(kv.Key, StringComparison.InvariantCultureIgnoreCase))
    .Select(kv => kv.Value)
    .FirstOrDefault();
if (matchingModelNameLine != null)
{
    model.Add(matchingModelNameLine);
}

我不知道,也许可以这样试试。

Regex model = new Regex(@"?<=model-)'d+");
int model;
String description;
object sColor; //Take the proper type here, no object
if (model.IsMatch("") && int.TryParse(model.Match("").Value, out model)) {
    if (model > 0 && model < 10) {
        description = String.Format("Sony, Smartphone(model-{0})", model);
        sColor = Enum.Parse(typeof(Sony), String.Format("Color{0}", model))
    }
    else if (model > 9 && model < 19) {
        description = String.Format("Nokia, Smartphone(model-{0})", model);
        sColor = Enum.Parse(typeof(Nokia), String.Format("Color{0}", model))
    }
    model.Add(new Line { text = description, color = sColor });
}

在这种情况下,我通常做的是匹配->操作的Dictionary。首先提取每次迭代中发生变化的数据,并为其创建一个类,例如:

class OperationData{
      public string Manufacturer { get; set;}
      public ColorEnum Color { get; set;}    //some way to persist the color, might be enum value, stringified enum name, parent class...
}

现在用执行操作所需的参数映射所有后缀(我假设OperationData有一个构造函数):

var operationMap = new Dictionary<string, OperationData>();
//now add all the definitions
oprationMap.Add("model-10", new OperationData("Nokia", Nokia.Color10));
//.....

并处理数据(为了简单起见,foreach,但LINQ将是我的首选):

foreach(var row in operationMap){
     var key = row.Key; 
     var data = row.Value;
     if (text.EndsWith(key)) {
            model.Add(new Line { 
                 text = String.Format("{0}, Smartphone({1})", data.Manufacturer, key), 
                 color = () => data.Color 
            });
            break;
   }
}

这种解决方案的最大好处是,如果您需要更改逻辑,您可以一次性更改。您可以很容易地修改匹配字符串或字符串格式的方式,甚至可以修改您创建的Line类。您还可以想象将它与不同的数据集一起重用。