Etc.
[Coding] 의도를 분명하게 하라
Accept
2024. 1. 27. 20:03
좋지 못한 코드
public List<int[]> getThem() {
List<int[]> list1 = new ArrayList<int[]>();
for (int[] x : theList) {
if (x[0] == 4) {
list1.add(x);
}
return list1;
}
}
// theList에 무엇이 들었는가?
// theList에서 0번째 index는 왜 중요하지?
// 4는 무슨 의미야?
// list1은 어떻게 사용이 될까?
좋은 코드
public List<int[]> getFlaggedCells() {
List<int[]> flaggedCells = new ArrayList<int[]>();
for (int[] cell : gameBoard) {
if (cell[STATUS_VALUE == FLAGGED) {
flaggedCells.add(cell);
}
return flaggedCells;
}
}
// FALGGED와 같은 값은 상수로 정의하거나, enum을 생성해서 사용한다.