分享
 
 
 

Refactoring Notes-Refactoring Methods(3)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

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){

return Math.min(time,delay);

}

private double getPrimaryAcc(){

return primaryForce/mass;

}

private int getSecondaryTime(int time){

return time-delay;

}

7.Remove Assignments to Parameters

Before:

int discount(int inputVal, int quantity, int yearToDate){

if(inputVal>50) inputVal-=2;

if(quantity>100) inputVal-=1;

if(yearToDate>10000) inputVal-=4;

return inputVal;

}

After:

int discount(int inputVal, int quantity, int yearToDate){

int result=inputVal;

if(inputVal>50) result-=2;

if(quantity>100) result-=1;

if(yearToDate>10000) result-=4;

return result;

}

8.Replace Method with Method Object

If there is a long method that uses local variables in such a way that you cannot apply Extract Method.Turn the mothod into its own object so that all the local variables became fields on that object.You can then decompose the method into other methods on the same object.

Michanics:

(1)Create a new class ,name it after the method.

(2)Give the new class a final field for the object that hosted the original method and a field for each temporary variable and each parameter int the method.

(3)Give the new class a constructor that takes the source object and each parameter.

(4)Give the new class a method named “compute” and copy the body of the original method into it.

Before:

class Account{

int gamma(int inputVal,int quantity,int yearToDate){

int importantVal1=(inputVal*quantity)+delta();

int importantVal2=(inputVal*yearToDate)+100;

if((yearToDate-importantVal1>100))

importantVal2-=20;

int importantVal3=importantVal2*7;

return importantVal3-2*importantVal1;

}

}

After:

class Account{

int gamma(int inputVal,int quantity,int yearToDate){

return new Gamma(this, inputVal, quantity, yearToDate);

}

}

class Gamma{

public Gamma(Account account,int inputVal,int quantity,int yearToDate){

account=account;

inputVal=inputVal;

quantity=quantity;

yearToDate=yearToDate;

}

final Account account;

int importantVal1;

int importantVal2;

int importantVal3;

int inputVal;

int quantity;

int yearToDate;

int compute(){

int importantVal1=(inputVal*quantity)+account.delta();

int importantVal2=(inputVal*yearToDate)+100;

if((yearToDate-importantVal1>100))

importantVal2-=20;

int importantVal3=importantVal2*7;

return importantVal3-2*importantVal1;

}

}

9.Substitue Algorithm

Replace an algorithm with one that is clearer.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有