`

《重构》笔记 二 重新组织你的函数 Remove Assignments t Parameters

 
阅读更多

六 Remove Assignments t Parameters

 

你的代码对参数进行赋值的动作。在As3 中除了那些 基原数据 如 String Number int uint Boolean 是安值传递,其余的传递的都是引用值。

 

所以在As 3.0 中不存在这个问题。

 

七 Replace Methord with Method Object

 

 你有个大型函数,其中对局部变量的使用,使你无法采用Extact Method .

 

 动机:小型函数具有其优美动人之处,只要将它重大型函数中提炼出来,就可以大大提高代码的可阅读性。但是局部变量的存在会增加分解函数的难度。ReplaceTemporary with qury 可以帮助你减轻这一负担,但是有时候你会发现根本无法拆解需要拆解的函数。这时,你就应该祭出 函数对象(Method object)这件法宝。将所有的函数对象都变成函数对象的值域。然后你及可以对这个新对象用 Extract Method创造出新函数,从而使原来的大型函数拆解变短。

 

  做法:

  参考例子。

 

  eg.

 

class Account ...
    private function gamma(inputVal:int, quantity:int, yearToDate:int): int {
    var  importantValue1:int = (inputVal * quantity) + delta();
    var  importantValue2:int = (inputVal * yearToDate) + 100;
    if((yearToDate 0 importValue1) > 100) {
        importValue2 -= 20;
    }

     var importantValue3:int = importValue2  * 7;
     //and so on
     return imortantValue3 -2 * importantValue1;
}
 

   =>为了把这函数变成一个函数对象,首先要声明一个新的Class

 

 

class Gamma ...
    private var account:Account ;
    private var int inputVal;
    private var int quantity;
    private var int yearToDate;
    private var int importtantantValue1;
    private var int importtantantValue2;
    private var int importtantantValue3;

    public function Gamma (souce:Account,inputValArg:int,
                           quantityArg:int,yearToDateArg:int) {
        
        account = souce;
        inputVal = inputValArg;
        quantity = quantityArg;
        yearToDate = yearToDateArg;
   }

    public function compute():int {
        importtantantValue1 = (inputVal  * quantity) + account.delta();
        importtantantValue2 = (inputVal  * yearToDate ) + 100;
        if(( yearToDate - importantValue) > 100 ) {
            importtantantValue2  = -= 20;
        }

         importantValue3 = importantValue2 * 7;

         return importantValue3 - 2*  importantValue1;
   }

 

class Account ...
    private function gamma(inputVal:int, quantity:int, yearToDate:int): int {
    return new Gamma(this,inputVal,quantity,yearToDate).compute();
}

 

 

   这样一来,我们就可以轻松的对 compute()进行 Extract Method而不必担心 引数的传递

 

分享到:
评论

相关推荐

    重构-改善既有代码的设计 中文版.pdf

    第6章 重新组织你的函数 6.1 Extract Method(提炼函数) 6.2 Inline Method(将函数内联化) 6.3 Inline Temp(将临时变量内联化) 6.4 Replace Temp With Query(以查询取代临时变量) 6.5 Introduce Explaining ...

    重构:改善既有代码的设计.[美]Martin Fowler.epub【文字版手机格式】

    6.7 Remove Assignments to Parameters(移除对参数的赋值) 6.8 Replace Method with Method Object(以函数对象取代函数) 6.9 Substitute Algorithm(替换算法) 第7章 在对象之间搬移特性 7.1 Move Method(搬移...

    重构-改善既有代码的设计

    6.7 Remove Assignments to Parameters(移除对参数的赋值) 131 6.8 Replace Method with Method Object(以函数对象取代函数) 135 6.9 Substitute Algorithm(替换算法) 139 第7章 在对象之间搬移特性 ...

    重构-改善既有代码的设计+中文版

     *Remove Assignments to Parameters 去除参数赋值   Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动...

    重构-改善既有代码的设计 中文版

    第6章 重新组织你的函数 6.1 Extract Method(提炼函数) 6.2 Inline Method(将函数内联化) 6.3 Inline Temp(将临时变量内联化) 6.4 Replace Temp With Query(以查询取代临时变量) 6.5 Introduce Explaining ...

    重构-改善既有代码的设计(中文版)

     *Remove Assignments to Parameters 去除参数赋值   Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动...

    重构_改善既有代码的设计[高清版]中文版

     *Remove Assignments to Parameters 去除参数赋值   Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间...

    代码重够方法 代码重够方法

    6.1.Extract Method(提炼函数) 6. 2. Inline Method(内联化函数) …… 6.7. Remove Assignments to Parameters (移除对参数的赋值)•Mechanics

    重构 改善既有代码的设计

     *Remove Assignments to Parameters 去除参数赋值   Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间...

    重构——改善既有代码的设计

     *Remove Assignments to Parameters 去除参数赋值   Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间移动...

    重构,改善既有代码的设计

     *Remove Assignments to Parameters 去除参数赋值   Replace Method with Method Object 用方法对象代替方法   Substitute Algorithm 替换算法  Chapter 7:Moving Features Between Objects 在对象之间...

    Nonblocking and blocking Assignments

    博主的博客Verilog之blocking & nonblocking assignments有些内容是参考了这篇英文文献的,其中对verilog中有关阻塞与非阻塞赋值语句的8种准则进行了详细的举例说明,读者可以下载文章进行详细阅读,以便更好地理解...

    吴恩达 Programming Assignments of Deep Learning Specialization (5 courses)

    吴恩达 Programming Assignments of Deep Learning Specialization (5 courses)

    CS231NPPT及Assignments1-3

    斯坦福大学李飞飞教授主讲cs231n课程课堂PPT及课后作业,完成版,含有assignment1-3全部内容

    matlab描绘三维函数代码-Computer_Vision_Assignments:Computer_Vision_Assignments

    matlab嵌入三维函数代码计算机视觉分配 这些是我为慕尼黑工业大学举行的SS2018计算机视觉课程作业的解决方案。 所有分配都将在Matlab中完成,仅使用其标准功能(不允许使用工具箱)。 自从该课程以德语授课以来,...

    digital-IC-Class assignments

    digital-IC-Class assignments 期末习题课

    Assignments.rar

    Assignments.rar

    FootHill College CS2B Assignments

    These are all the assignments I did during his class CS2B. If you are a student of this class and find this resource, I highly recommend that you did it first then take a look at these. Because his ...

    eslint-plugin-align-assignments

    eslint-plugin-align-assignments ESLint规则,用于在组中强制执行分配声明的对齐。安装您首先需要安装 : $ npm install eslint --save-dev接下来,安装eslint-plugin-align-assignments : $ npm install eslint-...

    0 Reference Answer for Assignments.pptx

    0 Reference Answer for Assignments.pptx

Global site tag (gtag.js) - Google Analytics