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

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;

}

...

原文地址:https://www.cnblogs.com/youknowzcm/p/11706411.html

时间: 2024-08-04 11:24:27

学习重构(2)-重新组织函数的相关文章

代码重构之重新组织函数

重新组织函数 一.提炼函数(Extract Method) 你有一段代码可以被组织在一起并独立出来,将这段代码放入一个独立的函数中,并让函数名称解释该函数的用途(以他做什么来命名). public void printOwing(){ int em=88; int outstanding=9999; //计算outstanding do { em--; outstanding+=em; } while (em==0); //pirnt outstanding System.out.println

学习重构(4)-重新组织数据

1. Self Encapsulate Field(自封装值域) 你直接访问一个值域,但与值域之间的耦合关系逐渐变得笨拙.为这个值域建立取值/设值函数(get/set),并且只以这些函数来访问值域. 应用场景:其实这种为每个变量提供两个操作函数的做法并不是百分百可取的,这样做的一个好处就是做到数据存储和使用隔离,尤其对于对象数据的保护是非常好的,可以返回给调用方一个数据拷贝,不用担心自己的数据会被篡改.但是也有不好的地方,就是会带来非常多的小函数,无形中增大了类的体量. 示例: private

重新组织函数--《重构》阅读笔记

1)寻找引用点时,最好使用工具,然后再人工review.在看到这个问题的时候,我估计应该是很久之前了.现在用IDE.这个要方便很多. 2)重新组织函数的方法和目标. 其实目标很简单.就是消灭长函数. 常用方法 Extract method Inline Method Replace Temp with Query Temporary Variable Replace Method with Method Object Remove Assignments to Parameters Substi

重构摘要6_重新组织函数

Extract Method 提炼函数 过长的函数或者一段需要注释才能理解的代码,就将这段代码放进一个独立的函数中. 函数粒度小,复用机会变大,覆写容易. 高层函数如一系列注释 关键看函数名称和函数本体之间的语义距离. 变清晰,做什么来命名 Inline Method 内联函数 有时可内联到一个大函数后,再从中提炼出组织合理的小型函数. Inline Temp 内联临时变量 return (order.basePrice>0); //可定义final确定是否被赋值一次 Replace Temp

重构摘要6_又一次组织函数

Extract Method 提炼函数 过长的函数或者一段须要凝视才干理解的代码,就将这段代码放进一个独立的函数中. 函数粒度小,复用机会变大,覆写easy. 高层函数如一系列凝视 关键看函数名称和函数本体之间的语义距离. 变清晰,做什么来命名 Inline Method 内联函数 有时可内联到一个大函数后,再从中提炼出组织合理的小型函数. Inline Temp 内联暂时变量 return (order.basePrice>0); //可定义final确定是否被赋值一次 Replace Tem

重构改善既有代码的设计--第6章--重新组织函数

第6章 重新组织函数 6.1 Extract Method Long methods,因为包含太多信息和逻辑,不容易处理和修改.所以需要进行Extract Method. (1)场景 当一个函数过长,或者一段代码需要注释才能看懂,就可以考虑将其放入独立函数中. (2)优点 每个函数粒度小,被复用的几率大,被修改的难度也会低一些:高层函数逻辑分明. (3)做法 以"做什么"命名,而不是"怎么做".比如,printDetail(). 将源函数中的某一段提取到目标函数中.

重构笔记——内联函数

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42261333         在上一篇文章中介绍了"提炼函数".本文将介绍"内联函数"这种重构手法.         下面让我们一起来学习该重构手法把. 开门见山         发现:一个函数的本体与名称同样清楚易懂. 解决:在函数调用点插入函数本体,然后移除该函数. //重构前 public int getRati

重构笔记——搬移函数

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42679983         我们都知道,类往往因为承担过多的责任而变得臃肿不堪.这种情况下,一般会使用"提炼类"这种手法将一部分责任分离出去.如果一个类变得"不负责任",一般会使用"内联类"这种手法将它融入另一个类.如果一个类使用了另一个类,一般会运用"隐藏委托关系"手法将这种关系隐藏

jQuery源码学习笔记:扩展工具函数

// 扩展工具函数 jQuery.extend({ // http://www.w3school.com.cn/jquery/core_noconflict.asp // 释放$的 jQuery 控制权 // 许多 JavaScript 库使用 $ 作为函数或变量名,jQuery 也一样. // 在 jQuery 中,$ 仅仅是 jQuery 的别名,因此即使不使用 $ 也能保证所有功能性. // 假如我们需要使用 jQuery 之外的另一 JavaScript 库,我们可以通过调用 $.noC