当前位置: 移动技术网 > IT编程>软件设计>架构 > 学习重构(2)-重新组织函数

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

2019年10月20日  | 移动技术网IT编程  | 我要评论
1. Extract Method(提炼函数) 将代码段放进一个独立函数中,并让函数名称解释该函数的用途。 示例: void printOwing(double amount) { printBanner(); //print details System.out.println("name: " ...

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;

        }

...

 

您可能感兴趣的文章:

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网