TDD(3) --轉自http://www.aygfsteel.com/yandazhi
private int calculateAverageRating() {
int totalRating = calculateTotalRating();
return totalRating / ratings.size();
}
int totalRating = calculateTotalRating();
return totalRating / ratings.size();
}
Use accepted conventions for accessors and mutators
public Category getCategory() {
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();
}
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
public void add(Movie movieToAdd) throws DuplicateMovieException {
if (this.contains(movieToAdd)) {
throw new DuplicateMovieException(movieToAdd.getName());
}
movies.add(movieToAdd);
}
if (this.contains(movieToAdd)) {
throw new DuplicateMovieException(movieToAdd.getName());
}
movies.add(movieToAdd);
}
There are always exceptions. Sometimes it is just clearer, and reads better, if you have type information in the method name.
public void addRating(Rating ratingToAdd) {
ratings.add(ratingToAdd);
}
ratings.add(ratingToAdd);
}
public class Movie {
private String name = "";
private Category category = Category.UNCATEGORIZED;
private List ratings = null;
//
}
private String name = "";
private Category category = Category.UNCATEGORIZED;
private List ratings = null;
//

}
如何意圖編程
包括使用隱喻,測試優(yōu)先,重構,作出假定,讓編譯器告訴你下一步做什么 這幾種技巧。
公用詞匯表 幫助你理解這個領域。
幫助你取名字。
|
posted on 2005-07-25 12:28 辰 閱讀(213) 評論(0) 編輯 收藏 所屬分類: Test-Driven Development