??xml version="1.0" encoding="utf-8" standalone="yes"?> programming by intention
There are always exceptions. Sometimes it is just clearer, and reads better, if you have type information in the method name. 包括使用隐喻Q测试优先,重构Q作出假定,让编译器告诉你下一步做什?q几U技巧?BR>
意图~程
int totalRating = calculateTotalRating();
return totalRating / ratings.size();
}
Use accepted conventions for accessors and mutators
return category;
}
public void setCategory(Category aCategory) {
category = aCategory;
}
public boolean isOfCategory(Category aCategory) {
return category.equals(aCategory);
}
public boolean isRated() {
return !ratings.isEmpty();
}
public boolean hasRating() {
return !ratings.isEmpty();
}
public int size() {
return movies.size();
}
Don't put redundant information in method names
if (this.contains(movieToAdd)) {
throw new DuplicateMovieException(movieToAdd.getName());
}
movies.add(movieToAdd);
}
ratings.add(ratingToAdd);
}
private String name = "";
private Category category = Category.UNCATEGORIZED;
private List ratings = null;
//
}
如何意图~程
公用词汇?帮助你理解这个领域?BR> 帮助你取名字?BR>
l护一套程序员试的框?/P>
除非它已l合试Q品不写入M代码
先写试
试军_你需要写什么代?/P>
你需要增加类或者方法,~译器会告诉你。(eclipse会向你抱怨有cannot be resolved 的)
下面看看代码的演q?BR>☆写试的时间比写代码的旉q多?
在有些情况下的确如此,但是不要太担?Z么呢? Ҏ我的体会:
1.有了试,你会写很多本来不需?初看h应该是有?的代?/P>
2.写测试的q程是在解决问题的q程,因此你会比较Ҏ,早地明白你到底应该做什?q样在写代码?p节省旉
3.寚w构帮助很?有了试,你才能放心大胆的q行重构.
4.长远来看,因ؓTDD会促q你写出好的代码,q且你会l常的重?因此会降低维护代?/P>
☆需要ؓ每个Ҏ~写试?
当然不需?我们所写的试必须是针Ҏ口方法的.一般认为处理业务逻辑的方?以及领域模型对象的关键行为是必须q行试. 其它的一些方法需要自己把?当然q需要经?
我现在只是一个新?没有啥经?我判断某个方法是否需要测?依据有两?
1.是否满我上面列出的必须试条g
2.是否值的试,q一条主要是心理因素,例如Ҏ个方法感觉心里没?那就先编写测?
☆TDD是一U测试新技术吗?
当然不是,TDDҎ׃是一Ҏ试技?它是一U新的开发方?只是借助试而已.
☆项目一开始没有采用TDD,在项目中期再引入TDD,可行?
一般来说不推荐在项目中期再引入TDD,q是׃TDD内在Ҏ决定的.
1.TDD是一U新的开发方?在开发过E中需要你转变思想,需要在实践不断完善自己,而且它本w就h一个较陡学习的坡度,q一点在很多文章中都提到q?因此在项目中期引入TDD,会立x廉目进?寚w目本w帮助也不会太大.
2.TDD在你开始写试?会驱动你寚w题进行思?然后持箋q行功能增强和重?在项目中?如果你编写一个测?q时你需要项目早期的一个组?但是q个lgq没有满你的测?因ؓҎ没有测?.现在因ؓ该组件有问题,试通不q?如果q时你再lg~写单元试,失M试驱动开发的优势?此时TDD的效果就大打折扣?
当然,在没有项目压力的情况?引入TDD是没有Q何问题的.不过我还是推荐在目开始就引入TDD是最佳选择.
☆ؓ也存在的lg补充单元试值得?
在上一问题的第二点原因中已l提到过,感觉不值得.在这U情况下,用一般的Ҏ试一下即?比如java的main()Ҏ.
☆TDD~写的测试案例是比较复杂的吗?
在TDD?试是一步一步演化的,需要你一直保持简单设计的理念,因此,一般测试案例是比较清晰?如果发现你的试非常复杂,应该是你没有抓住问题的重Ҏ者没有掌握正?有效的方?
Extreme Programming is obsessed with feedback, and in software development, good feedback requires good testing. Top XP teams practice "test-driven development", working in very short cycles of adding a test, then making it work. Almost effortlessly, teams produce code with nearly 100 percent test coverage, which is a great step forward in most shops. (If your programmers are already doing even more sophisticated testing, more power to you. Keep it up, it can only help!)
It isn't enough to write tests: you have to run them. Here, too, Extreme Programming is extreme. These "programmer tests", or "unit tests" are all collected together, and every time any programmer releases any code to the repository (and pairs typically release twice a day or more), every single one of the programmer tests must run correctly. One hundred percent, all the time! This means that programmers get immediate feedback on how they're doing. Additionally, these tests provide invaluable support as the software design is improved.