学习重构(2)-重新组织函数

1. Extract Method(提炼函数)

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

示例:

void printOwing(double amount)  {

    printBanner();

 

    //print details

    System.out.println("name: " + _name);

    System.out.println("amount: " + amount);

}

重构为:

void printOwing(double amount) {

    printBanner();

    printDetails();

}

void printDetails(double amount) {

    System.out.println("name: " + _name);

    System.out.println("amount: " + amount);

}

 

2. Inline Method(内联函数)

在函数调用点插入函数本体,然后移除该函数。

示例:

int getRating() {

    return moreThanFiveLateDeliveries() ? 2 : 1;

}

boolean moreThanFiveLateDeliveries() {

    return _numberOfLateDeliveries > 5;

}

重构为:

int getRating() {

    return (_numberOfLateDeliveries > 5) ? 2 : 1;

}

 

3. Inline Temp(内联临时变量)

将所有对该变量的引用动作,替换为对它复制的那个表达式本身。

示例:

double basePrice = anOrder.BasePrice();

return (basePrice > 1000);

重构为:

return (anOrder.BasePrice() > 1000);

 

4. Replace Temp with Query(以查询取代临时变量)

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

示例:

double basePrice = _quantity * _itemPrice;

if (basePrice > 1000) {

    return basePrice * 0.95;

} else {

    return basePrice * 0.98;

}

重构为:

if (basePrice() > 1000) {

    return basePrice() * 0.95;

} else {

    return basePrice() * 0.98;

}

...

double basePrice() {

    return _quantity * _itemPrice;

}

 

5. Introduce Explaining 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 && isIEBrowser && wasInitialized() && wasResized) {

    //do someting

}

 

6. Split Temporary Variable(分解临时变量)

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

示例:

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(perimeter);

final double area = _height * _width;

System.out.println(area);

 

7. Remove Assignments to Parameters(移除对参数的赋值)

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

示例:

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

    if (inputVal > 50) {

        inputVal -= 2;

    }

...

重构为:

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

    int result = inputVal

    if (inputVal > 50) {

        result -= 2;

    }

 

8. Replace Method with Method Object(以函数对象取代函数)

将这个函数放进一个单独对象中,如此以来局部变量就成了对对象内的字段。然后你可以在同一个对象中将这个大型函数分解为多个小型函数。

示例:

class Order...

    double price() {

        double primaryBasePrice;

        double secondaryBasePrice;

        double tertiaryBasePrice;

        //long computation;

        ...

    }

重构为:

class PriceCalculator {

    private double primaryBasePrice;

    private double secondaryBasePrice;

    private double tertiaryBasePrice;

    void compute() {

        //long computation

    }

}

class Order...

    private PriceCalculator _priceCalculator = new PriceCalculator();

    double price() {

        return new PriceCalculator().compute();

...

 

9. Substitute Algorithm(替换算法)

将函数逻辑本体替换为另一个简洁清晰的算法

示例:

String findPerson(String[] people) {

    for (String person:people) {

        if(person.equals("Don") || person.equals("John") || person.equals("Kent")) {

            return person;

        }

...

重构为:

String findPerson(String[] people) {

    List candidates = Arrays.asList(new String[] {"Don", "John", "Kent"});

    for(String person:people) {

        if(candidates.contains(person)) {

            return person;

        }

...

 

posted @ 2019-10-20 00:03  流浪的小丑  阅读(292)  评论(0编辑  收藏  举报