意图编程
programming by intention
名字
Use nouns or noun phrases for class names
public class Movie { // }
public class MovieRatingComparator implements Comparator { // }
public class XMLMovieListReader implements MovieListReader { // }
Use either adjectives or generic nouns and noun phrases for interfaces
public interface Serializable { }
public interface MovieListWriter { void write(MovieList movieList) throws IOException; }
Use verbs and verb phrases for method names |
|
private int calculateAverageRating() {
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();
}
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);
}
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);
}
Use nouns and noun phrases for variable names.
public class Movie {
private String name = "";
private Category category = Category.UNCATEGORIZED;
private List ratings = null;
//
}
如何意图编程
包括使用隐喻,测试优先,重构,作出假定,让编译器告诉你下一步做什么 这几种技巧。
公用词汇表 帮助你理解这个领域。
帮助你取名字。
"NO COMMENT"
Incomplete code
// TODO: The tree should be balanced after doing the insertion.
// CODE DEBT: the looping structure is a bit convoluted, could use
// some method extraction.
Refactoring doesn't make it clear enough
// NEEDS WORK: I tried extract method, but it's still awkward.
// Maybe refactoring to a Strategy would clean it up?
Use of an unusual algorithm
// I used an AVL Tree algorithm here to keep the tree balanced.
Use of a published algorithm
// This AVL alorithm was based on Brad Appleton's implementation at
// http://www.enteract.com/~bradapp/ftp/src/libs/C++/AvlTrees.html
Performance tuning
// A circular queue is used here for performance reasons: to avoid
// having to move elements around.
Class comment
/**
* This class represents a single movie title. It is responsible for
* maintaining its own ratings, reviews, etc.
*/
| |