当前位置: 移动技术网 > IT编程>开发语言>Java > 重构之重新组织函数

重构之重新组织函数

2019年07月02日  | 移动技术网IT编程  | 我要评论

目的:学习基本重构手法

出处:《重构 改善既有代码的设计》

记录方式:只记录示例代码,深入细节可自行搜索

列表:

1、extract method(提炼函数)

2、inline temp(内联临时变量)

3、replace temp with query(以查询取代临时变量)

4、introduce explaining variable(引入解释性变量)

5、split temporary variable(分解临时变量)

6、remove assignments to parameters(移除对参数的赋值)

7、substiture algorithm(替换算法)

 

一、extract method(提炼函数)

 

你有一段代码可以被组织在一起并独立出来。

将这段代码放进一个独立函数中,并让函数名称解释该函数的用途。

 

1 void printowing(double amount){
2        printbanner();
3        system.print("name:" + _name);
4        system.print("name:" + amount);
5      }
1 void printowing(double amount){
2        printbanner();
3        printdetails(amount);
4 }
5 
6 void printdetails(double amount){
7        system.print("name:" + _name);
8        system.print("name:" + amount);
9 }

 

二、inline temp(内联临时变量)

 

你有个临时变量,只是被简单表达式赋值一次,而它妨碍了其他重构手法。

将所有对该对象的引用动作,替换为对它赋值的那个表达式自身。

 

1 double baseprice = anorder.baseprice();
2 return (baseprice > 1000)
1 return (anorder.baseprice() > 1000)

 

三、replace temp with query(以查询取代临时变量)

 

你的程序以一个临时变量保存某一表达式的运算结果。

将这个表达式提炼到一个独立函数中,将这个临时变量的所有引用点替换为对新函数的调用。此后,新函数就可被其他函数使用。

 

1 double baseprice = _quantity * _itemprice;
2       if(baseprice  >1000){
3              return baseprice  * 0.95;
4        }else{
5              return baseprice  * 0.98;
6        }
if(baseprice() > 1000){
    return baseprice() * 0.95;
}else{
    return baseprice() * 0.98;
}
...
double baseprice(){
     return _quantity * _itemprice;
}

 

四、introduce explaining variable(引入解释性变量)

 

你有一个复杂的表达式

将该复杂表达式(或其中一部分)的结果放进一个临时变量,以此变量名称来解释表达式用途。

 

1 if((platform.touppercase().indexof("mac") > -1 )&&
2    (brower.touppercase().indexof("ie") > -1 )&&
3      wasinititialized() && resize > 0 ) ){
4     //do something
5 }
final boolean ismacos = platform.touppercase().indexof("mac") > -1;
final boolean isiebrowser = platform.touppercase().indexof("ie") > -1;
final boolean wasresized = resize > 0;

if(ismacos && isiebrowser && wasresized ){
     //do  something
}

 

五、split temporary variable(分解临时变量)

 

你的程序有某个临时变量被赋值超过一次,它既不是循环变量,也不被用于收集计算结果。

针对每次赋值,创造一个独立、对应的临时变量。

 

 double temp = 2 * (_height * _width);
system.out.println(temp);
temp = _height * _width;
system.out.println(temp);
1 double temp = 2 * (_height * _width);
2 system.out.println(perimeter);
3 final double area = _height * _width;
4 system.out.println(area);

 

六、remove assignments to parameters(移除对参数的赋值)

 

代码对一个参数进行赋值。

以一个临时变量取代该参数的位置。

 

1 int discount (int inputval,int quantity,int yeartodate){
2    if(inputval > 50 ) inputval -= 2;
3 }
int discount (int inputval,int quantity,int yeartodate){
   int result =  inputval;
   if(inputval > 50 ) result  -= 2;
}

 

七、substiture algorithm(替换算法)

 

你想要把某个算法替换为另一个更清晰的算法。

将函数本体替换为另一个算法。

 

 1 string foundperson(string[] people){
 2       for(int i = 0;i < people.length;i++){
 3             if(people[i].equals("don")){
 4                return "don";
 5             }
 6             if(people[i].equals("john")){
 7                return "john";
 8             }
 9             if(people[i].equals("kent")){
10                return "kent";
11             }
12        }
13         return "";
14 }
1 string foundperson(string[] people){
2       list candidates = arrays.aslist(new string[] {"don","john","kent"});
3       for(int i = 0;i < people.length;i++){
4             if(candidates.contains(people[i])){
5                return people[i];
6             }
7        }
8         return "";
9 }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网