修改代码的艺术 读书笔记(二,修改现有代码的四种技巧)


在没有Unit Tests的代码下如何更好的更改代码?四种方法:

1 Sprout Method,衍生方法

Class Experiment {

  void existingMethod() {

    ...

    sproutMethod();

    ...

  }

  void sproutMethod() {}

}

2 Sprout Class,衍生类

Class SproutClass {

  public static void experiment(){}

}

Class Experiment {

  void existingMethod() {

    SproutClass.experiment();

  }

}

3 Wrap Method,包装方法

Class Experiment {

  void existingMethod(){

    System.out.println("existing method");

  }

  void wrapMethod() {

    // do additional things

    existingMehtod();

  }

}

Then make 'existingMethod' private and rename 'wrapMethod' to 'existingMethod'.

4 Wrap Class,包装类

class WrapClass {

  Experiment experiment;

  public WrapClass(Experiment experiment);

  public existingMethod() {

    // do additional things

    experiment.existingMethod();

  }

}

用这四种方法是因为在现实世界中,每个任务都被分配一定的资源(时间,人力等),我们想使我们新写的代码有Unit Tests来保证质量,但是没有更多的时间去增加没有Unit Tests的已有的代码的Unit Tests。条件是资源有限,追求的目标是使新写的代码有Unit Tests来保证质量。

原文这样说:

When you introduce more interfaces and packages into your design to break dependencies, the amount of time it takes to rebuild the entire system goes up slightly. There are more files to

compile. But the average time for a make, a build based on what needs to be recompiled, can go down dramatically.

原文:http://blog.youkuaiyun.com/hongchangfirst/article/details/52150855

作者:hongchangfirst

hongchangfirst的主页:http://blog.youkuaiyun.com/hongchangfirst



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值