在没有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.csdn.net/hongchangfirst/article/details/52150855
作者:hongchangfirst
hongchangfirst的主页:http://blog.csdn.net/hongchangfirst
本文介绍在没有单元测试的情况下,通过四种方法改进代码:衍生方法、衍生类、包装方法和包装类。这些方法有助于在资源有限的情况下,确保新增代码的质量。
979

被折叠的 条评论
为什么被折叠?



