Clean Code : Keep code clean :: Meaningful Names
Developers are authors. And one thing about authors is that they have readers.
Meaningful Names |
- Names are everywhere in software.
- We name our variables, our functions, our arguments, classes, and packages. We name and name and name.
- It is easy to say that names should reveal(display) intent. What we want to impress upon you is that we are serious about this.
- Choosing good names takes time but saves more than it takes.
- Use Pronounceable Names
Everyone who reads your code (including you) will be happier if you do.
Example
int d; // elapsed time in days
The name d reveals nothing. It does not evoke a sense of elapsed time, nor of days.
We should choose a name that specifies what is being measured and
the unit of that measurement:
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
int fileAgeInDays;
Improve your Code
What is the purpose of this code?
public List getThem() {
List list1 = new ArrayList();
for (int[] x : theList)
if (x[0] == 4)
list1.add(x);
return list1;
}
we can improve the code considerably:
public List%lt;Cell> getFlaggedCells() {
List%lt;Cell> flaggedCells = new ArrayList%lt;Cell>();
for (Cell cell : gameBoard)
if (cell.isFlagged())
flaggedCells.add(cell);
return flaggedCells;
}
Compare and decide yourself
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private final String pszqint = "102";
/* ... */
};
TO
class Customer {
private Date generationTimestamp;
private Date modificationTimestamp;;
private final String recordId = "102";
/* ... */
};
Suggestions
- Use Searchable Names
- Single-letter names and numeric constants have a particular problem in that they are not easy to locate across a body of text.
- The length of a name should correspond to the size of its scope.
- Avoid Encodings
- Avoid Hungarian Notation
- Avoid Member Prefixes
Recent Comments