重构学习笔记

[b]引子[/b]
[b]过度设计[/b],是指代码的灵活性和复杂性超出所需。他们之所以这样做,是希望今天就把方案设计得更加灵活、更复杂,以适应明天的需求。如果预计中的需求分配不会成为现实,浪费的将是宝贵的时间和金钱。为了避免这一问题,分头负责系统的各个部分。但是每个人都在自己的小天地里工作,很少看别处代码是否已经完成了自己需要的功能,最后产生大量的重复代码。
[b]模式万灵药[/b],模式代表的是一种灵活、精妙,非常优雅的面向对象设计方法。模式的强大容易使人对简单的代码编写方式视而不见。把精力放在短小、简单和直截了当的代码上,可以使你减少过度设计。
[b]设计不足[/b],这个比过度设计要常见得多。产生原因有以下几种:
[list]
[*]程序员没有时间,没有抽出时间,或者时间不允许重构。
[*]程序员在好的软件设计方面知识不足。
[*]程序员被要求在既有系统中快速地添加新功能。
[*]程序员补充被迫同时进行太多的项目。
[/list]
长期的设计不足,可能造成如下后果:
[list]
[*]系统的1.0版本很快就交付了,但是代码质量很差。
[*]系统的2.0版本也交付了,但质量低劣的代码使我们慢了下来。
[*]在企图交付未来版本时,随着劣质代码的倍增,开发也越来越慢,最后人们对系统、程序员乃至使大学陷于这种境地的整个过程都失去了信心。
[*]到了4.0版或者之后,我们意识到这样肯定不行,开始考虑推倒重来。
[/list]
[b]测试驱动和持续重构[/b],两个实践能帮助降低过度设计和设计不足的几率。
[b]演进式设计[/b],关于软件设计的资料更多的集中在讲授优秀的解决方案上,而对于这些解决方案的演变过程不够重视。演变所得到的设计结构虽然有些帮助,但是不知道设计如何发展而来的,在一下项目中你就很可能错误的应用,或者陷入过度设计的误区。
[b][i]了解优秀软件设计的演变过程比学习优秀设计本身更有价值。[/i][/b]

[b]Extract Method[/b]
Turn the fragment into a method whos name explains the purpose of the method.
将这段代码放入一个独立的方法中,让方法名来解释方法的用途。
    void printOwing(double amount) {
printBanner();

// print details
System.out.println("name" + _name);
System.out.println("amount" + _amount);
}

    void printOwing(double amount) {
printBanner();
printDetails(amount);
}

void printDetails(double amount) {
System.out.println("name" + _name);
System.out.println("amount" + _amount);
}

[b]Inline Method[/b]
Put the method's body into the body of its callers and remove the method.
    int getRating() {
return (moreThanFiveLateDeliveries()) ? 2 : 1;
}

boolean moreThanFiveLateDeliveries() {
return _numberOfLateDeliveries > 5;
}

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

[b]InlineTemp[/b]
        double basePrice = anOrder.basePrice();
return (basePrice > 1000);

        return (anOrder.basePrice() > 1000);

[b]Replace Temp with Query[/b]
    public double getPrice () {
double basePrice = _quantity * _itemPrice;
if (basePrice > 1000)
return basePrice * 0.95;
else
return basePrice * 0.98;
}

    public double getPrice () {
if (basePrice() > 1000)
return basePrice() * 0.95;
else
return basePrice() * 0.98;
}

double basePrice() {
return _quantity * _itemPrice;
}

[b]Introduce Explaining Variable[/b]
        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 something
}

[b]Split Temporary Variable[/b]
        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);

[b]Remove Assignments to Parameters[/b]
    int discount(int inputVal, int quantity, int yearToDate) {
if (inputVal > 50)
inputVal -= 2;
if (quantity > 100)
inputVal -= 1;
if (yearToDate > 10000)
inputVal -= 4;
return inputVal;
}

    int discount(int inputVal, int quantity, int yearToDate) {
int result = inputVal;
if (inputVal > 50)
result -= 2;
if (quantity > 100)
result -= 1;
if (yearToDate > 10000)
result -= 4;
return result;
}

[b]Replace Method with Method Object[/b]
class Account {

int gamma(int inputVal, int quantity, int yearToDate) {
int importantValue1 = (inputVal * quantity) + dalta();
int importantValue2 = (inputVal * yearToDate) + 100;
if ((yearToDate - importantValue1) > 100)
importantValue2 -= 20;
int importantValue3 = importantValue2 * 7;
// and so on
return importantValue3 - 2 * importantValue1;
}
}

class Account {

int gamma(int inputVal, int quantity, int yearToDate) {
return new Gamma(this, inputVal, quantity, yearToDate).compute();
}
}

class Gamma {
private Account _account;
private int inputVal;
private int quantity;
private int yearToDate;

public Gamma(Account _account, int inputVal, int quantity, int yearToDate) {
this._account = _account;
this.inputVal = inputVal;
this.quantity = quantity;
this.yearToDate = yearToDate;
}

int compute() {
int importantValue1 = (inputVal * quantity) + _account.dalta();
int importantValue2 = (inputVal * yearToDate) + 100;
if ((yearToDate - importantValue1) > 100)
importantValue2 -= 20;
int importantValue3 = importantValue2 * 7;
// and so on
return importantValue3 - 2 * importantValue1;
}
}

[b]Substitute Algorithm[/b]
    String foundPerson(String[] people) {
for (int i = 0; i < people.length; i++) {
if (people[i].equals("Don")) {
return "Don";
}
if (people[i].equals("Jonh")) {
return "Jonh";
}
if (people[i].equals("Kent")) {
return "Kent";
}
}
return "";
}

    String foundPerson(String[] people) {
List<String> candidates = Arrays.asList(
new String[] { "Don", "Jonh", "Kent" });
for (int i = 0; i < people.length; i++) {
if (candidates.contains(people[i]))
return people[i];
}
return "";
}

……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值