Long Parameter List(过长参数列)---要重构的味道
int basePrice = _quantity * _itemPrice;
int discountLevel;
if (_quantity > 100) discountLevel = 2;
else discountLevel = 1;
double finalPrice = discountedPrice (basePrice, discountLevel);
return finalPrice;
} private double discountedPrice (int basePrice, int discountLevel) {
if (discountLevel == 2) return basePrice * 0.1;
else return basePrice * 0.05;
}
return discountedPrice ();
} private double discountedPrice () {
if (getDiscountLevel() == 2) return getBasePrice() * 0.1;
else return getBasePrice() * 0.05;
} private double getBasePrice() {
return _quantity * _itemPrice;
}
class Room...
boolean withinPlan(HeatingPlan plan) {
int low = daysTempRange().getLow();
int high = daysTempRange().getHigh();
return plan.withinRange(low, high);
}
class HeatingPlan...
boolean withinRange (int low, int high) {
return (low >= _range.getLow() && high <= _range.getHigh());
}
private TempRange _range;
boolean withinRange (TempRange roomRange) {
return (roomRange.getLow() >= _range.getLow() && roomRange.getHigh() <= _range.getHigh());
}
class Room...
boolean withinPlan(HeatingPlan plan) {
int low = daysTempRange().getLow();
int high = daysTempRange().getHigh();
return plan.withinRange(daysTempRange());
}
boolean withinRange (TempRange roomRange) {
return (_range.includes(roomRange));
}
class TempRange...
boolean includes (TempRange arg) {
return arg.getLow() >= this.getLow() && arg.getHigh() <= this.getHigh();
double getFlowBetween (Date start, Date end) {
double result = 0;
Enumeration e = _entries.elements();
while (e.hasMoreElements()) {
Entry each = (Entry) e.nextElement();
if (each.getDate().equals(start) ||
each.getDate().equals(end) ||
(each.getDate().after(start) && each.getDate().before(end)))
{
result += each.getValue();
}
}
return result;
}
private Vector _entries = new Vector(); client code...
DateRange (Date start, Date end) {
_start = start;
_end = end;
}
Date getStart() {
return _start;
}
Date getEnd() {
return _end;
}
private final Date _start;
private final Date _end;
}
double getFlowBetween (DateRange range) {
double result = 0;
Enumeration e = _entries.elements();
while (e.hasMoreElements()) {
Entry each = (Entry) e.nextElement();
if (range.includes(each.getDate())) {
result += each.getValue();
}
}
return result;
} class DateRange...
boolean includes (Date arg) {
return (arg.equals(_start) ||
arg.equals(_end) ||
(arg.after(_start) && arg.before(_end)));
} 参考资料:
https://sourcemaking.com/refactoring/preserve-whole-object
https://sourcemaking.com/refactoring/replace-parameter-with-method
https://sourcemaking.com/refactoring/introduce-parameter-object
Long Parameter List(过长参数列)---要重构的味道的更多相关文章
- 【重构】 代码的坏味道总结 Bad Smell (一) (重复代码 | 过长函数 | 过大的类 | 过长参数列 | 发散式变化 | 霰弹式修改)
膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...
- 重构 之 总结代码的坏味道 Bad Smell (一) 重复代码 过长函数 过大的类 过长参数列 发散式变化 霰弹式修改
膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...
- C++11变长参数模板
[C++11变长参数模板] C++03只有固定模板参数.C++11 加入新的表示法,允许任意个数.任意类别的模板参数,不必在定义时将参数的个数固定. 实参的个数也可以是 0,所以 tuple<& ...
- shell脚本处理长参数的模板
shell脚本处理长参数的模板 一个shell模板,处理命令行参数,支持长短参数: #!/bin/bash # # FILE: kvm-clone-v2.sh # # DESCRIPTION: Clo ...
- go实例—函数或方法的可变长参数
支持可变长参数列表的函数可以支持任意个传入参数,比如fmt.Println函数就是一个支持可变长参数列表的函数. 需要注意的是,可变长参数应该是函数定义的最右边的参数,即最后一个参数 package ...
- 介绍C++11标准的变长参数模板
目前大部分主流编译器的最新版本均支持了C++11标准(官方名为ISO/IEC14882:2011)大部分的语法特性,其中比较难理解的新语法特性可能要属变长参数模板(variadic template) ...
- Java语法糖初探(三)--变长参数
变长参数概念 在Java5 中提供了变长参数(varargs),也就是在方法定义中可以使用个数不确定的参数,对于同一方法可以使用不同个数的参数调用.形如 function(T …args).但是需要明 ...
- Python的不定长参数研究
通过观察程序和运行结果我们发现,传参时将1传给了a,将2传给了b,将3,4,5传给了*args,将m=6,n=7,p=8传给了**kwargs.为什么是这样传参呢?*args和**kwargs又是什 ...
- C++中的变长参数
新参与的项目中,为了使用共享内存和自定义内存池,我们自己定义了MemNew函数,且在函数内部对于非pod类型自动执行构造函数.在需要的地方调用自定义的MemNew函数.这样就带来一个问题,使用stl的 ...
随机推荐
- Notes of the scrum meeting(12.9)
meeting time:14:00~17:00p.m.,December 9th,2013 meeting place:一号教学楼209 attendees: 顾育豪 ...
- VS2005、VS2008中的快捷键、组合键大全
Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...
- iOS- 用UICollectionViewController 来进行横竖屏九宫格布局
1.简单说说UICollectionViewController 我们在做九宫格布局时,可以使用UIScrollView,也可以使用UICollectionViewController. 当我们用UI ...
- listBox和pictureBox的使用
重要属性:pictureBox中SizeMode可以更改图像显示的尺寸大小. using System; using System.Collections.Generic; using System. ...
- oracle 删除数据恢复
select * from taxi_comp_worksheet_ext as of timestamp to_timestamp('2014-09-22 13:00:00', 'yyyy-m ...
- WC2017 划水记
Day 0 (2.2) 一寒假没有好好写题....于是晚上打了人生第一场codeforces,写了Div2三道水题就弃疗了23333333 Day 1 (2.3) 从德州高铁站坐小火车G57去绍兴 ...
- 【bzoj3427】Poi2013 Bytecomputer dp
题目描述 A sequence of N integers I1,I2…In from the set {-1,0,1} is given. The bytecomputer is a device ...
- python字典的常用操作
# dic={[1,2,3]:'123'} #可变类型不能当做字典的key,value可以使用任意类型 # dic={(2,3,4):'123'} # print (dic[(2,3,4)]) #元组 ...
- Python 源码剖析(五)【DICT对象】
五.DICT对象 1.散列表概述 2.PyDictObject 3.PyDictObject的创建与维护 4.PyDictObject 对象缓冲池 5.Hack PyDictObject 这篇篇幅较长 ...
- Android Bitmap和Drawable互转及使用BitmapFactory解析图片流
一.Bitmap转Drawable Bitmap bmp=xxx; BitmapDrawable bd=new BitmapDrawable(bmp); 因为BtimapDrawable是Drawab ...