2장 이름을 잘 짓는 몇가지 규칙을 소개한다.
bad code
int d;
good code
int daysSinceCreation
int fileAgeInDays
bad code
public List<int[]> getThem(){
List<int[]> list1 = new ArrayList<int[]>();
for (int[] x : theList) if (x[0] == 4)
list1.add(x);
return list1;
}
good code
public List<Cell> getFlaggedCells(){
List<Cell> flaggedCells = new ArrayList<Cell>();
for (Cell cell : gameBoard)
if (cell[STATUS_VALUE] == FLAGGED)
flaggedCells.add(cell);
return flaggedCells; }
bad code
연속된 숫자
public static void copyChars(char a1[], char a2[]) {
for (int i = 0; i< a1.length; i++) {
a2[i] = a1[i];
}
}
good code
의미 있는 이름으로 변경
public static void copyChars(char source[], char destination[]) {
for (int i = 0; i< source.length; i++) {
destination[i] = source[i];
}
}
bad code
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private final
String pszqint = "102";
/* ... */
};
good code
class Customer {
private Date generationTimeStamp;
private Date modificationTimeStamp;
private final String rdcordId = "102";
};
명사
나 명사구
가 적합하다.동사
나 동사구
가 적합하다.good code
bad code
코드를 읽는 사람도 프로그래머이므로 전상 용어, 알고리즘 이름,패턴 이름 수학 용어 등을 사용해도 괜찮다.
JobQueue와 같이 기술 개념에는 기술 이름이 가장 적합한 선택이다.
프로그래밍 용어
가 없다면 문제 영역(도메인 관련 전문 이름)에서 이름을 가져온다.accountAddress와 customerAddress는 Address 클래스 인스턴스로는 좋은 이름이나 클래스 이름으로는 적합하지 못하다. Address는 클래스 이름으로 적합하다.
포트 주소, MAC 주소, 웹 주소를 구분해야 한다면 PostalAddress, MAC, URI라는 이름도 괜찮다.