Monday, September 13, 2010

ch2 meaningful names

p48

Introduction
Names are everywhere in software.

(p 49)
Use Intention-Revealing Names

It is easy to say that names should reveal intent

The name of a variable, function, or class, should answer all the big questions.

bad code:
public List getThem() {
List list1 = new ArrayList();
for (int[] x : theList)
if (x[0] == 4)
list1.add(x);
return list1;
}

good code:
public List getFlaggedCells() {
List flaggedCells = new ArrayList();
for (int[] cell : gameBoard)
if (cell[STATUS_VALUE] == FLAGGED)
flaggedCells.add(cell);
return flaggedCells;
}

even better code:
public List getFlaggedCells() {
List flaggedCells = new ArrayList();
for (Cell cell : gameBoard)
if (cell.isFlagged())
flaggedCells.add(cell);
return flaggedCells;
}

Avoid Disinformation
(p50)
Programmers must avoid leaving false clues that obscure the meaning of code.
(p51)