学习重构-重新组织函数

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;
        }
...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值