クリーニングコードchap 2.意味のある名前


chap 2. 意味のある名前
  • 例1-悪い例:aとbが何を意味するか理解しにくい.
  • int a;
    String b;
  • 例2-名前で意味がわかる良い例です.
  • int itemCount;
    String itemName;
  • 例3−は、例2でより良好である.クラスの使用、適切な変数名、意味が明確です.
  • class SalesItem {
    ItemCode code;
    String name;
    int count;
    }
    
    SalesItem selectedItem = salesIempRepositor.getItemByCode(purchase.Request.getItemCode())
    System.out.println("User Requested %s. count = %d",
    					selectedItem.getName(), selectedItem.getCount());
    リング内のijkを無効にする
  • インデックスが必要ない場合は、lamdaまたは拡張for文を使用します.
  • for文
  • for (int i = 0; i < messages.size(); i++) {
      // ..
    }
  • 改良for文
  • for (String message : messages) {
      // ..
    }
  • lamda
  • messages.stream().forEach(
      message -> // ...
    )
  • i,j,kの代わりに脈絡に合致する名前がある.
  • i, h -> row, col/width, height
  • i、j、k->行、col、deptなど
  • 統一された単語を使う
  • 類似の意味の語は1つに統一されている.
  • Memeber/Customer/User
  • Service/Manager
  • Repository/Dao
  • 変数名にタイプを入れない
    String nameString(X) -> name(O)
    Int itemPriceAmount(X) -> itemPrice(O)
    
    Account[] accountArray(X) -> accounts(O)
    List<Account> accountList, accounts(둘다 O)
    Map<Account> accountMap(O)
    
    public interface IShapFactory(X) -> ShapeFactory(O)
    public class ShapeFactoryImpl(soso) -> CircleFactory
  • ListとMapの場合、変数名にタイプを追加できます.
  • インプリメンテーションクラスの後にImplを付けてもよいし、どのインプリメンテーションクラスであるかを示すこともできる.
  • チームメンバーと
  • を統一的に使用
    Google Java Naming Code
    パッケージ名
  • All lower case, no underscores
  • com.example.deepspace // Ok
    com.example.deepSpace // no
    com.example.deep_space // no
    クラス名
  • UpperCamelCase(大文字で始まる)
  • // 클래스는 명사구
    Character, ImmutableList
    
    // 인터페이스는 명사, 명사구, 형용사
    List, Readable
    
    // 테스크 클래스는 Test로 끝나기
    HashTest, hashIntegationTest
  • 形容詞で記述されたインタフェースを実現する際には、名前が自然であるため、ClassImplementReadableのように使用することができる.
  • メソッド名
  • LowerCamelCase(小文字で始まる)
  • // 메서드는 동사, 동사구
    sendMessage, stop
    
    // jUnit 테스트에 Undersore 사용되기도 함.
    // <methodUnderTest>_<state> 패턴으로 사용됨
    pop_emptyStack