- Use intention revealing names
example:
int d; // elapsed time in days - BAD;
other options:
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
int fileAgeInDays;
Method Example:
public Listint[] getThem() vs.
public Listint[] getFlaggedCells()
Listint[] list1 vs Listint[] flaggedCells
if (cell[0] == 4) vs if (cell[STATUS_VALUE] == FLAGGED)
even better:
ListCell flaggedCells = new ArrayListCell();
if (cell.isFlagged()) flaggedCells.add(cell);
- Avoid words whose "entrenched" meaning vary from the intended meaning
- avoid multiple names which vary in tiny/small ways (XYZControllerHandlingStrings vs
XYZControllerStorageStrings)
- avoid numeric variables (a1[i] = a1[i] vs. destination[i] = source[i])
- use pronounceable names (Date genymdhms vs. generationTimestamp)
- use searchable names
the length of a name should correspond to the size of its scope
- avoid encodings
encoding type or scope information to names adds extra burden of deciphering
(e.g. hungarian notation, member prefixes (your class should be small enough
that you dont need them!))
- interfaces and implementations
use ClassType - ClassTypeImpl instead of IClassType - ClassType
- avoid mental mapping
clarity is king!
- class names should be a noun (since they are an object, after all)
- methods should have a verb or verb phrase names, and use getThing, setThing, isStatus
(accessors, mutators, and predicates)
- use one word per concept (e.g. dont have three fetch, retrieve, get or
controller, manager, driver)
- add meaningful context (e.g. it should be clear what a variable applies to based on its namespace/
class its a part of etc.)
Our goal is to make the code as easy as possible to understand
bj
2019-09-22