六 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而不必担心 引数的传递