zoukankan      html  css  js  c++  java
  • 减少case语句的编写,方便维护(虽然性能没多大体升)

    开发过程中我们可能会看到这种代码

    1 int orderState = 3;
    2 if(orderState == 1){}
    3 else if(orderState == 2){}
    4 else if(orderState == 3){}
    5 else
    6     throw new Exception("no status");

    这些状态可能会因为需求变更增加或者减少,维护很不方便。

    换了一种写法,感觉好多了。

    public string GetStatusName(string status)
    {
        string[][] statusTbl = {
            new string[]{"1","doing"}
            new string[]{"2","finished"}
            new string[]{"3","cancel"}
        };
       
        foreach(string[] item in statusTbl)
    10     {
    11         if(item[0]== status){
    12             return item[1];
    13         }
    14     }
    15
    16
    17 }

    增加减少状态很容易,只需要在数组里设定一下就行了。代码也很直观。

  • 相关阅读:
    PyQt4信号与槽
    Amazon Redshift数据库
    NoSQL数据库的认识
    如何划分子网
    VPC见解
    Linux之添加交换分区
    MySQL基础之 标准模式通配符
    MySQL基础之 LIKE操作符
    MySQL基础之 AND和OR运算符
    MySQL基础之 索引
  • 原文地址:https://www.cnblogs.com/wzg0319/p/2149772.html
Copyright © 2011-2022 走看看