Refactoring Notes-Refactoring Methods(3)
2008-02-23 10:01:28来源:互联网 阅读 ()
5.Introduce Explaining Variable
If you have a complicated expression,put the result of the expression, or parts of the expression , in a temporary variable with a name that explains the purpose.
Introduce Explaining Variable is particulaly valuable with conditional logic in which it is useful to take each clause of a condition and explain what the condition means a well-named temp.
Another case is a long algorithm,in which each step in the computation can be explained with a temp.
Mechanics
(1)Declare a final temporary variable,and set it to the result of part of the complex expression.
(2)Replace the result part of the expression with the value of the temp.
Before:
double price(){
return quantity*itemPrice-Math.max(0,quantity-500)*itemPrice*0.05 Math.min(quantity*itemPrice*0.1,100.0);
}
After:
double price(){
final double basePrice= quantity*itemPrice;
final double quantityDiscount=Math.max(0,quantity-500)*itemPrice*0.05 ;
final double shipping=Math.min(quantity*itemPrice*0.1,100.0);
return basePrice-quantityDiscount shipping;
}
Appendix: using Extract Method to complete refactoring.
The benefit of using Extract Method is that theses methodologies are available to any other part of the object that needs them.Although they are private at first,but you can relax that if another object needs them.
double price(){
return basePrice()-quantityDiscount() shipping();
}
private Double basePrice(){
return quantityDiscount*itemPrice;
}
private Double quantityDiscount(){
return Math.min(quantity*itemPrice*0.1,100.0);
}
private Double shipping(){
return Math.min(quantity*itemPrice*0.1,100.0);
}
6.Split Temporary Variable
If a temporary variable assigned to more than once,but is not a loop variable nor a collecting temporary variable(If the later assignments are of the form i=i some expression, that indicates that it is a collceting temporary variable),you should use Split Temporary Method.Any variable with more than one responsibility should be replaced with a temp for each responsibility.
Michanics:
(1)Change the name of a temp at its declaration and its first assignment.
(2)Declare the new temp as final.
(3)Change all references of the temp up to its second assignment.
(4) Declare the temp at its second assignment.
Before:
double getDistanceTravelled(int time){
double result;
double acc=primaryForce/mass;
int primaryTime=Math.min(time,delay);
result = 0.5*acc*primaryTime*primaryTime;
int secondaryTime=time-delay;
if(secondaryTime>0){
double primaryVel=acc*delay;
acc=(primaryForce secondaryForce)/mass;
result =primaryVel*secondaryTime 0.5*acc*secondaryTime*secondaryTime;
}
return result;
}
After:
double getDistanceTravelled(int time){
double result;
final double primaryAcc=primaryForce/mass;
int primaryTime=Math.min(time,delay);
result = 0.5*primaryAcc*primaryTime*primaryTime;
int secondaryTime=time-delay;
if(secondaryTime>0){
double primaryVel=primaryAcc *delay;
final double secondaryAcc=(primaryForce secondaryForce)/mass;
result =primaryVel*secondaryTime
0.5*secondaryAcc*secondaryTime*secondaryTime;
}
return result;
}
Appendix: Complete refactoring
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
double getDistanceTravelled(int time){
double result;
result = 0.5*getPrimaryAcc()*Math.pow(getPrimaryTime(time),2);
if(getSecondaryTime(time)>0){
double primaryVel=getPrimaryAcc() *delay;
final double secondaryAcc=(primaryForce secondaryForce)/mass;
result =primaryVel*getSecondaryTime(time)
0.5*secondaryAcc*Math.pow(getSecondaryTime(time),2);
}
return result;
}
private int getPrimaryTime(int time){
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash