关于重构(四)--代码的坏味道

 

代码的坏味道主要有:

  1. Duplicated Code---(重复的代码):如果你在两个以上的地点看到相同的程序结构,那可以:设法将它们合二为一,程序会变得更好。
  2. Long Method ------(过长函数):
      1 private void bindSaleInfo(string swhere)
      2     {
      3         ArrayList proList = getProductInfo(swhere);
      4         string colorStr = "";
      5         StringBuilder rowHtml = new StringBuilder();// 行代码
      6         //repeaer头部信息
      7        // rowHtml.Append("<table border='0' width='98%' cellspacing='1' cellpadding='0' bgcolor='#6DA9DE'>");
      8         rowHtml.Append("<asp:Repeater ID='Repeater1' runat='server'>");
      9         rowHtml.Append("<HeaderTemplate><tr bgcolor='#FFFFFF'>");
     10 
     11         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>序号</b></td>");//0
     12         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>日期</b></td>");//1
     13         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>大区</b></td>");//3
     14         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>业务员</b></td>");//2
     15         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>经销商</b></td>");//4
     16         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>产品编号</b></td>");//5
     17         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>产品名称</b></td>");//6
     18         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>产品规格</b></td>");//7
     19         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>到货地点</b></td>");//8
     20         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>发货数量</b></td>");//9
     21         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>发货单价</b></td>");//10
     22         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>发货金额</b></td></tr></HeaderTemplate>");//14
     23         string str1 = "", str2 = "", str3 = "", str4 = "", str5 = "", str6 = "", str7 = "", str8 = "";
     24         string str9 = "", str10 = "", str11 = "";
     25 
     26         decimal tcount1 = 0, tcount3 = 0;
     27         //按日期统计每一条
     28         for (int i = 0; i < proList.Count; i++)
     29         {
     30             str1 = proList[i].ToString().Split(',')[0];
     31             str2 = proList[i].ToString().Split(',')[1];
     32             str3 = proList[i].ToString().Split(',')[2];
     33             str4 = proList[i].ToString().Split(',')[3];
     34             str5 = proList[i].ToString().Split(',')[4];
     35             str6 = proList[i].ToString().Split(',')[5];
     36             str7 = proList[i].ToString().Split(',')[6];
     37             str8 = proList[i].ToString().Split(',')[7];
     38             str9 = proList[i].ToString().Split(',')[8];
     39             str10 = proList[i].ToString().Split(',')[9];
     40             str11 = proList[i].ToString().Split(',')[10];
     41        
     42 
     43             if (!str9.Equals(""))
     44             {
     45                 tcount1 += Convert.ToDecimal(str9);
     46             }
     47            
     48             if (!str11.Equals(""))
     49             {
     50                 tcount3 += Convert.ToDecimal(str11);
     51             }
     52            
     53 
     54             if (i % 2 != 0)
     55             {
     56                 colorStr = "#FFFFFF";// 奇数行
     57                 rowHtml.Append("<ItemTemplate>");
     58                 //序号
     59                 rowHtml.Append("<tr title='双击显示详细'  bgcolor='" + colorStr + "' ><td height='25' align='center' width='4%'>"
     60                   + (i + 1) + "</td>");
     61                 
     62                 //日期
     63                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str1 + "</td>");
     64                
     65                 //区域
     66                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str3 + "</td>");
     67                 //业务员
     68                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str2 + "</td>");
     69                 //经销商
     70                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str4 + "</td>");
     71                 //产品编号
     72                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str5 + "</td>");
     73                 //产品名称
     74                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str6 + "</td>");
     75                 //产品规格
     76                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str7 + "</td>");
     77                 //到货地点
     78                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str8 + "</td>");
     79                 //发货数量
     80                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str9 + "</td>");
     81                 //发货单价
     82                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str10 + "</td>");
     83                 //发货金额
     84                 rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str11 + "</td>");
     85    
     86                 rowHtml.Append("</tr></ItemTemplate>");
     87             }
     88             else
     89             {
     90                colorStr = "#DFF4FF";// 偶数行
     91                rowHtml.Append("<AlternatingItemTemplate>");
     92                //序号
     93                rowHtml.Append("<tr title='双击显示详细'  bgcolor='" + colorStr + "' ><td height='25' align='center' width='4%'>"
     94                   + (i + 1) + "</td>");
     95     
     96                //日期
     97                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str1 + "</td>");
     98               
     99                //区域
    100                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str3 + "</td>");
    101                //业务员
    102                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str2 + "</td>");
    103                //经销商
    104                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str4 + "</td>");
    105                //产品编号
    106                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str5 + "</td>");
    107                //产品名称
    108                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str6 + "</td>");
    109                //产品规格
    110                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str7 + "</td>");
    111                //到货地点
    112                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str8 + "</td>");
    113                //发货数量
    114                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str9 + "</td>");
    115                //发货单价
    116                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str10 + "</td>");
    117                //发货金额
    118                rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str11 + "</td>");
    119                rowHtml.Append("</tr></AlternatingItemTemplate>");
    120             }
    121         }
    122 
    123         if (proList.Count % 2 != 0)
    124         {
    125             colorStr = "#FFFFFF";// 奇数行
    126         }
    127         else
    128         {
    129             colorStr = "#DFF4FF";// 偶数行
    130         }
    131         rowHtml.Append("<tr title='双击显示详细'  bgcolor='" + colorStr + "' ><td height='25' align='center' width='4%'>汇总</td>");
    132        //日期
    133        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
    134        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
    135        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
    136        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
    137        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
    138        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
    139        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
    140        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
    141        //发货数量
    142        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + tcount1.ToString() + "</td>");
    143        //发货单价
    144        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'></td>");
    145        //发货金额
    146        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + tcount3.ToString() + "</td>");
    147        rowHtml.Append("</tr>");
    148         rowHtml.Append("</asp:Repeater>");
    149         reportContent.Text = rowHtml.ToString();
    150     }
    View Code

    可以修改一下

      1   private void bindSaleInfo(string swhere)
      2     {
      3         ArrayList proList = getProductInfo(swhere);
      4         string colorStr = "";
      5         StringBuilder rowHtml = new StringBuilder();// 行代码
      6         //repeaer头部信息
      7        // rowHtml.Append("<table border='0' width='98%' cellspacing='1' cellpadding='0' bgcolor='#6DA9DE'>");
      8         rowHtml.Append("<asp:Repeater ID='Repeater1' runat='server'>");
      9         rowHtml.Append("<HeaderTemplate><tr bgcolor='#FFFFFF'>");
     10 
     11         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>序号</b></td>");//0
     12         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>日期</b></td>");//1
     13         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>大区</b></td>");//3
     14         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>业务员</b></td>");//2
     15         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>经销商</b></td>");//4
     16         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>产品编号</b></td>");//5
     17         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>产品名称</b></td>");//6
     18         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>产品规格</b></td>");//7
     19         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>到货地点</b></td>");//8
     20         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>发货数量</b></td>");//9
     21         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>发货单价</b></td>");//10
     22         rowHtml.Append("<td height='30' align='center' style='word-break: break-all;'><b>发货金额</b></td></tr></HeaderTemplate>");//14
     23         string str1 = "", str2 = "", str3 = "", str4 = "", str5 = "", str6 = "", str7 = "", str8 = "";
     24         string str9 = "", str10 = "", str11 = "";
     25 
     26         decimal tcount1 = 0, tcount3 = 0;
     27         //按日期统计每一条
     28         for (int i = 0; i < proList.Count; i++)
     29         {
     30             str1 = proList[i].ToString().Split(',')[0];
     31             str2 = proList[i].ToString().Split(',')[1];
     32             str3 = proList[i].ToString().Split(',')[2];
     33             str4 = proList[i].ToString().Split(',')[3];
     34             str5 = proList[i].ToString().Split(',')[4];
     35             str6 = proList[i].ToString().Split(',')[5];
     36             str7 = proList[i].ToString().Split(',')[6];
     37             str8 = proList[i].ToString().Split(',')[7];
     38             str9 = proList[i].ToString().Split(',')[8];
     39             str10 = proList[i].ToString().Split(',')[9];
     40             str11 = proList[i].ToString().Split(',')[10];
     41        
     42 
     43             if (!str9.Equals(""))
     44             {
     45                 tcount1 += Convert.ToDecimal(str9);
     46             }
     47            
     48             if (!str11.Equals(""))
     49             {
     50                 tcount3 += Convert.ToDecimal(str11);
     51             }
     52            
     53 
     54             if (i % 2 != 0)
     55             {
     56                 colorStr = "#FFFFFF";// 奇数行
     57                 rowHtml.Append("<ItemTemplate>");
     58                 //序号
     59                 rowHtml.Append("<tr title='双击显示详细'  bgcolor='" + colorStr + "' ><td height='25' align='center' width='4%'>"
     60                   + (i + 1) + "</td>");
     61 
     62                 SetStyle(rowHtml, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11);
     63                 rowHtml.Append("</tr></ItemTemplate>");
     64             }
     65             else
     66             {
     67                colorStr = "#DFF4FF";// 偶数行
     68                rowHtml.Append("<AlternatingItemTemplate>");
     69                //序号
     70                rowHtml.Append("<tr title='双击显示详细'  bgcolor='" + colorStr + "' ><td height='25' align='center' width='4%'>"
     71                   + (i + 1) + "</td>");
     72 
     73                SetStyle(rowHtml, str1, str2, str3, str4, str5, str6, str7, str8, str9, str10, str11);
     74                rowHtml.Append("</tr></AlternatingItemTemplate>");
     75             }
     76         }
     77 
     78         if (proList.Count % 2 != 0)
     79         {
     80             colorStr = "#FFFFFF";// 奇数行
     81         }
     82         else
     83         {
     84             colorStr = "#DFF4FF";// 偶数行
     85         }
     86         rowHtml.Append("<tr title='双击显示详细'  bgcolor='" + colorStr + "' ><td height='25' align='center' width='4%'>汇总</td>");
     87        //日期
     88        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
     89        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
     90        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
     91        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
     92        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
     93        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
     94        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
     95        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'> </td>");
     96        //发货数量
     97        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + tcount1.ToString() + "</td>");
     98        //发货单价
     99        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'></td>");
    100        //发货金额
    101        rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + tcount3.ToString() + "</td>");
    102        rowHtml.Append("</tr>");
    103         rowHtml.Append("</asp:Repeater>");
    104         reportContent.Text = rowHtml.ToString();
    105     }
    106 
    107     private static void SetStyle(StringBuilder rowHtml, string str1, string str2, string str3, string str4, string str5, string str6, string str7, string str8, string str9, string str10, string str11)
    108     {
    109         //日期
    110         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str1 + "</td>");
    111 
    112         //区域
    113         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str3 + "</td>");
    114         //业务员
    115         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str2 + "</td>");
    116         //经销商
    117         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str4 + "</td>");
    118         //产品编号
    119         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str5 + "</td>");
    120         //产品名称
    121         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str6 + "</td>");
    122         //产品规格
    123         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str7 + "</td>");
    124         //到货地点
    125         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str8 + "</td>");
    126         //发货数量
    127         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str9 + "</td>");
    128         //发货单价
    129         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str10 + "</td>");
    130         //发货金额
    131         rowHtml.Append("<td height='25' align='center' style='word-break: break-all;'>" + str11 + "</td>");
    132 
    133     }
    View Code

    当然我只是随便弄了一下,比如这样下会出现第4种坏味道(可以看一下代码坏味道特别篇---过长的参数列)

  3. Large Class -------(过大的类):
  4. Long Parameter List -----(过长参数列)http://www.cnblogs.com/ITyueguangyang/p/4193744.html
  5. Divergent Change ---(发散式变化)
  6. Shotgun Surgery ---(散弹式修改)-
  7. Feature Envy -----(依恋情结)
  8. Data Clumps------(数据泥团)
  9. Primitive Obsession(基本类型偏执)
  10. Swith Statements ( swith 惊悚现身------个人以为这种情况出现的比较少)
  11. Parallel inheritance Hierarchies (平行继承体系)
  12. Speculative Generality(夸夸其谈未来性)
  13. Temporary Field (令人迷惑的暂时字段)
  14. Message Chains(过度耦合的消息链)
  15. Inappropriate Intimacy (过分狎昵的关系)
  16. Alternative Classes With Different interfaces (异曲同工的类)
  17. Incomplete library Class (不完美的类库)
  18. Data Class(纯粹的数据库类)
  19. Refused Bequest (被拒绝的遗赠)
  20. Comments(过多的注释)

 

posted @ 2014-12-30 16:34  门外客  阅读(247)  评论(0编辑  收藏  举报