在没有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