代码坏味道特别篇————Long parameter List 过长的参数列表

 

刚开始学习编程时,老师说:讲方法所需要的东西都以参数的形式传入,那是我们好像还没学OO这个东东,要不就弄成全局变量,我擦,全局变量可牛逼了,刚开始学习的时候我们都在用全局变量,可是后来工作了,经理说不要用全局变量,我当时就有些醉了,突然间觉得就不会写代码了,其实我们可以用对象来解决这个问题,这样我们就不会开到过长的参数列表了

 1     private DataTable getSentInfo(string Pno, string Pname, string Psytle, string SentTime,string DealerNo,string DealerName)
 2     {
 3         string sqlStr = "select convert(decimal(18,2),round(sum(sc.Price*srd.SentNum ),2))as countPrice,sum(srd.SentNum) as num "
 4             + "from  LB_Sent_Rec sr inner join LB_Sent_RecDetail srd "
 5            + "on sr.SentID=srd.SentID inner join LB_Sale_Rec sc "
 6            + "on sc.SaleID=srd.SaleID where sc.cid='" + cidH.Value + "' and sc.Pno='" + Pno + "' and sc.Pname='" + Pname + "' and sc.PStyle='" + Psytle + "' "
 7            + "and sc.DealerNo='" + DealerNo + "' and sc.DealerName='" + DealerName + "' "
 8            + "and sr.SentTime between '" + SentTime + " 00:00:00' and '" + SentTime + " 23:59:59'";
 9         return DbHelperSQL.GetDataTable(sqlStr);
10     }
View Code

使用对象后的代码

 1 public class Product
 2 {
 3     public Product()
 4     {
 5         //
 6         //TODO: 在此处添加构造函数逻辑
 7         //
 8     }
 9 
10     public string Pno { get; set; }
11     public string Pnamr { get; set; }
12     public string Psytle { get; set; }
13     public string SentTime { get; set; }
14     public string DealerNo{get;set;}
15     public string DealerName { get; set; }
16 }
View Code

 

 1  private DataTable getSentInfo(Product pr)
 2     {
 3     string  sqlStr = "select convert(decimal(18,2),round(sum(sc.Price*srd.SentNum ),2))as countPrice,sum(srd.SentNum) as num "
 4             + "from  LB_Sent_Rec sr inner join LB_Sent_RecDetail srd "
 5            + "on sr.SentID=srd.SentID inner join LB_Sale_Rec sc "
 6            + "on sc.SaleID=srd.SaleID where sc.cid='" + cidH.Value + "' and sc.Pno='" + pr.Pno + "' and sc.Pname='" + pr.Pnamr + "' and sc.PStyle='" + pr.Psytle + "' "
 7            + "and sc.DealerNo='" + pr.DealerNo + "' and sc.DealerName='" + pr.DealerName + "' "
 8            + "and sr.SentTime between '" + pr.SentTime + " 00:00:00' and '" + pr.SentTime + " 23:59:59'";
 9         return DbHelperSQL.GetDataTable(sqlStr);
10     }
View Code

这样是不是更容易理解传入参数所表示的内容呢?

这种方法书中叫 introduce Parameter Object

转载于:https://www.cnblogs.com/ITyueguangyang/p/4193744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值