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

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

使用对象后的代码

 public class Product
{
public Product()
{
//
//TODO: 在此处添加构造函数逻辑
//
} public string Pno { get; set; }
public string Pnamr { get; set; }
public string Psytle { get; set; }
public string SentTime { get; set; }
public string DealerNo{get;set;}
public string DealerName { get; set; }
}
  private DataTable getSentInfo(Product pr)
{
string sqlStr = "select convert(decimal(18,2),round(sum(sc.Price*srd.SentNum ),2))as countPrice,sum(srd.SentNum) as num "
+ "from LB_Sent_Rec sr inner join LB_Sent_RecDetail srd "
+ "on sr.SentID=srd.SentID inner join LB_Sale_Rec sc "
+ "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 + "' "
+ "and sc.DealerNo='" + pr.DealerNo + "' and sc.DealerName='" + pr.DealerName + "' "
+ "and sr.SentTime between '" + pr.SentTime + " 00:00:00' and '" + pr.SentTime + " 23:59:59'";
return DbHelperSQL.GetDataTable(sqlStr);
}

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

这种方法书中叫 introduce Parameter Object

代码坏味道特别篇————Long parameter List 过长的参数列表的更多相关文章

  1. 吐槽一下项目中的代码坏味道:滥用java常量

    我们的项目中是否充斥着类似以下的代码呢?定义一个专门存放常量的java类(接口),非常多其它类依赖该常量类. public interface IConstant { int ZERO = 0; St ...

  2. Sonar项目主要指标以及代码坏味道详解

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6766994.html 众所周知Sona ...

  3. 单元测试系列之四:Sonar平台中项目主要指标以及代码坏味道详解

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6766994.html 众所周知Sona ...

  4. 代码重构 & 代码中的坏味道

    1.重构 1.1 为什么要重构 1.1.1 改进程序设计 程序员为了快速完成任务,在没有完全理解整体架构之前就开始写代码, 导致程序逐渐失去自己的结构.重构则帮助重新组织代码,重新清晰的体现 程序结构 ...

  5. 代码的坏味道(14)——重复代码(Duplicate Code)

    坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...

  6. SonarQube规则之坏味道类型

    1.Abbreviation As Word In Name (默认 关闭)坏味道 主要检查验证标识符名称中的缩写(连续大写字母)长度,还允许执行骆驼案例命名allowedAbbreviationLe ...

  7. 代码的坏味道(4)——过长参数列(Long Parameter List)

    坏味道--过长参数列(Long Parameter List) 特征 一个函数有超过3.4个入参. 问题原因 过长参数列可能是将多个算法并到一个函数中时发生的.函数中的入参可以用来控制最终选用哪个算法 ...

  8. 【转】Bad Smell(代码的坏味道)

    1.Duplicated Code(重复的代码) 臭味行列中首当其冲的就是Duplicated Code.如果你在一个以上的地点看到相同的程序结构,那么当可肯定:设法将它们合而为一,程序会变得更好. ...

  9. 代码的坏味道(17)——夸夸其谈未来性(Speculative Generality)

    坏味道--夸夸其谈未来性(Speculative Generality) 特征 存在未被使用的类.函数.字段或参数. 问题原因 有时,代码仅仅为了支持未来的特性而产生,然而却一直未实现.结果,代码变得 ...

随机推荐

  1. The Bip Buffer - The Circular Buffer with a Twist

    Introduction The Bip-Buffer is like a circular buffer, but slightly different. Instead of keeping on ...

  2. Android开发笔记-加载xml资源

    1.Activity获取strings.xml中键的值 需要通过 getResources().getString(R.string.*)方法获得 以“state”为例 String value= g ...

  3. imx6 生成 spi设备节点

    开发板需要使用spi接口,但是spi接口被touch占用,使用event进行操作.所以需要更改配置,生成spi设备节点. 参考链接 https://community.nxp.com/thread/3 ...

  4. eclipse打开jar包出现乱码问题解决方法

    今天做项目时候,用eclipse打开.class文件出现乱码问题.jar编码和本地编辑器编码格式不对造成的错误. 首先我们打开eclipse,点击菜单下的window-->preferences ...

  5. RabbitMQ学习总结 第四篇:发布/订阅 Publish/Subscribe

    目录 RabbitMQ学习总结 第一篇:理论篇 RabbitMQ学习总结 第二篇:快速入门HelloWorld RabbitMQ学习总结 第三篇:工作队列Work Queue RabbitMQ学习总结 ...

  6. Android Studio IDE 主题设置

    1.界面主题设置,如下图: 2.代码字体设置,如下图:

  7. 学习RFS,所有文章的参考

    所有文章的主要参考: CSDN博客:http://blog.csdn.net/tulituqi 微博:@齐涛-道长

  8. nagios监控linux主机监控内存脚本

    说明 工作包括两部分监控端(一二三)和被监控端(四) 一.nrpe.cfg中添加脚本 nrpe.cfg中添加命令索引 command[check_used_mem]=/usr/local/nagios ...

  9. UISegment属性

    1.segmentedControlStyle 设置segment的显示样式. typedefNS_ENUM(NSInteger, UISegmentedControlStyle) { UISegme ...

  10. curl获得http响应码 302 和绑定host

    shell curl 取得HTTP返回的状态 获取状态码 curl -I -m 10 -o /dev/null -s -w %{http_code} www.baidu.com 获取时间   curl ...