隨筆 - 3, 文章 - 152, 評論 - 17, 引用 - 0
          數(shù)據(jù)加載中……

          TDD(3) --轉自http://www.aygfsteel.com/yandazhi

          意圖編程

          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;

            
          //
          }


          如何意圖編程

          包括使用隱喻,測試優(yōu)先,重構,作出假定,讓編譯器告訴你下一步做什么 這幾種技巧。

          公用詞匯表 幫助你理解這個領域。
                                幫助你取名字。


          "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.
           */
          

          posted on 2005-07-25 12:28 閱讀(213) 評論(0)  編輯  收藏 所屬分類: Test-Driven Development

          主站蜘蛛池模板: 法库县| 文昌市| 滦南县| 阳东县| 高淳县| 西丰县| 屏边| 西充县| 广西| 萍乡市| 茂名市| 海门市| 和林格尔县| 舟曲县| 南投市| 新河县| 呼玛县| 探索| 泽普县| 周至县| 贵州省| 石城县| 宁城县| 博野县| 屏山县| 铜山县| 永德县| 通许县| 乳源| 桐庐县| 长沙市| 石狮市| 开江县| 玉环县| 上蔡县| 岳池县| 丽江市| 龙陵县| 威信县| 册亨县| 双桥区|