b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

微服务架构 云计算 重构 -改变既有代码的设计 ---- 笔记(6)

电脑杂谈  发布时间:2018-10-13 16:05:07  来源:网络整理

一个函数本体和函数名称一样容易理解


    int getRating() {
        return (moreThanFiveLateDeliveries()) ? 2 : 1;
    }

    boolean moreThanFiveLateDeliveries() {
        return _numberOfLateDeliveries > 5;
    }

to


    int getRating() {
        return (_numberOfLateDeliveries > 5) ? 2 : 1;
    }

动机

当间接不是必要的时候

当一组方法被严格的分解,会使这个方法变得清晰

你申明了一个临时的变量在一段表达里面,然后临时的变量将会阻挡你重构


    double basePrice = anOrder.basePrice();
    return (basePrice > 1000)

to


    return (anOrder.basePrice() > 1000)

Motivation

Use it with 4. Replace Temp with Query

使用这个 4. Replace Temp with Query

你正在使用临时变量来保存表达式的结果


    double basePrice = _quantity * _itemPrice;
    if (basePrice > 1000){
        return basePrice * 0.95;
    }
    else{
        return basePrice * 0.98;
    }

to


    if (basePrice() > 1000){
        return basePrice() * 0.95;
    }
    else{
        return basePrice() * 0.98;
    }
    ...
    double basePrice() {
        return _quantity * _itemPrice;
    }

动机

使用方法代替临时变量,类中的任何方法都可以获取信息

这是一个十分重要的步骤在其之前1. Extract Method

有一个复杂的表达式


    if ( (platform.toUpperCase().indexOf("MAC") > -1) &&
        (browser.toUpperCase().indexOf("IE") > -1) &&
        wasInitialized() && resize > 0 )
    {
        // do something
    }

to


    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 something
    }


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-89441-6.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    每日福利
    热点图片
    拼命载入中...