重构 改善既有的代码设计 读书笔记4 composing method

来源:互联网 发布:js .tostring 的用法 编辑:程序博客网 时间:2024/06/14 15:53

ExtractMethod: 将一段代码抽出来,放到一个方法里面,并用这段代码的功能来对方法进行命名。

当一段代码需要注释来说明它的作用的时候,可以考虑使用Extract Method。

有3种情况:

抽取出来的代码没有本地变量

抽取出来的代码有本地变量,但不改变变量,那么可以将这个变量作为参数传递

需要重新分配本地变量:

情况一:这个变量只在抽取出来的这段代码中使用,那么将变量直接移植进新方法中

情况二:这个变量在抽取出来的代码外面使用,那么就需要在改变变量的值之后,再将这个值返回

 

Inline Method:  如果一个方法不是多态,方法中的代码又很少,调用的地方也不多那么可以直接将代码拷贝到需要用的地方,去掉方法。

还有就是如果间接调用 用得太多,也可以直接inlineMethod。

 

InlineTemp:

Double basePrice = anOrder.basePrice();

Return (base > 1000);

 

Return (anOrder.basePrice() > 1000);

 

ReplqceTemp with Query:

如果一个临时变量的值是有一些表达式计算出来的,那么,这部分可以抽取出来作为一个方法,那所有用到这个临时变量的地方,可以用调用方法来代替。

这种临时变量有时候会造成方法过长的问题,因为如果后面的逻辑需要访问这个变量的值,方法的长度就必须延长到那里。

 

Double basePrice = quantity * itemPrice;

If (basePrice  > 1000)

     ReturnbasePrice * 0.95;

Else

    Return basePrice * 0.98;

 

 重构为:

Double basePrice()

{

         Returnquantity * itemPrice;

}

If(basePrice() > 1000)

         ReturnbasePrice() * 0.95;

Else

ReturnbasePrice() * 0.98;

 

 

IntroduceExplaining Variable:

你有一个复杂的表达式,那么用一个临时变量来返回表达式的一部分值或整个值,临时变量的名称解释表达式的目的。

 

If (platform.toUpperCase().indexOf(“MAC”) )> -1) &&(browser.toUpperCase().indexOf(“IE”)) > -1

 && wasInitialized() && resize > 0)

{

// do something
}

 重构为:

Final boolean isMacOs = platform.toUpperCase().indexOf(“MAC”) >-1;

Final boolean isIEBrowser = browser.toUpperCase().indexOf(“IE”) >-1;

Final boolean wasResized = resize > 0;

If (isMacOs && isIEBroser && wasInitialized()&& wasResized)

{

   // do something

}

在这里,如果这些表达式的值在其他地方还需要用到,那么完全可以使用Extract Method.

 

SplitTemporary Variable:

如果一个变量在你的程序中被多次赋值,它既不是一个循环变量,也不是一个collection变量,那么,在每次赋值的时候,需要重新声明一个临时变量。

Double temp =2*(height+width);

System.out.println(temp);

Temp=height*width;

System.out.println(temp);

 

 重构为:

Final double perimeter = 2*(height+width);

System.out.println(temp);

Final double area = height*width;

System.out.println(temp);

 

 

RemoveAssignments to Parameter:

如果程序给方法传递进来的参数分配了个值,那么,一定要重新声明一个临时变量来表示这个值。

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

{

         If(inputVal> 50) inputVal -= 2;

}

 重构为:

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

{

         Intresult = inputVal;

         If(inputVal> 50)  result -= 2;

}

 

ReplaceMethod with Method Object:

如果你有一个很长的方法,方法中的本地变量之间有逻辑关系,那么可以将方法变成一个类,这些变量就成为类的成员变量,然后在类中再拆分方法。

Class order

{

         Doubleprice()

{

         Double primaryBasePrice;

         Double secondaryBasePrice;

         Double tertiaryBasePrice;

         //long computation;

         ……….

}

}

 

Substitute algorithm:

String foundPerson(string[] peopel)

{

         For(intI = 0; I < people.length; i++)

         {

                   If(people[i].equals(“Don”))

                   {

                            Return“Don”;

                   }

                   If(people[i].equals(“John”))

                   {

                            Return“John”;

                   }

                   If(people[i].equals(“Kent”))

                   {

                            Return“Kent”;

                   }

}

Return “”;

}

 重构为:

String foundPerson(string[] people)

{

         Listcandidates = Arrays.asList(new string[]{“Don”, “John”, “Kent”});

For(int I = 0; I< people.length; i++)

                   If(candidates.contains(people[i]))

                            Return people[i];

Return “”;

}

原创粉丝点击