"Names are everywhere in software". "The hardest thing about choosing good names is that it requires good descriptive skills"[1]. In my previous post, I emphasized that we should take care our code, so now let's start with naming.
References:
Original code
|
Revised code
|
|
Reveals nothing
|
int d; //elapsed
time in days
|
int elapsedTimeInDays;
|
Difficult to understand
|
public List<Cell> getThem()
|
public List<Cell> getFlaggedCells()
|
Specific to programmers
|
accountList
|
accounts
|
Number-series
|
public void copyChars(char
a1[], char a2[])
|
public void copyChars(char source[], char destination[])
|
Noise words (indistinguishable)
|
customerInfo
vs customer;
accountData
vs account
|
customer,
account //distinguish names in such a way that the reader knows what the differences offer
|
Silly made-up words
|
class DtaRcrd02{
private Date genymdhms;
}
|
class Customer{
private Date generationTimestamp;
}
|
Poor name
|
for(int j=0 ; j<34 ; j++){
s += (t[j]*4)/5;
}
|
int realDaysPerIdealDay = 4;
const int WORK_DAYS_PER_WEEK
= 5;
const int NUMBER_OF_TASK = 34;
for(…)
|
Member prefixes
|
private String m_dsc;
|
private String description;
|
Interfaces prefixes
|
IShapeFactory
|
ShapeFactory
|
Constructors with args
|
Complex point = new
Complex(23.0)
|
Complex point = Complex.FromRealNumber(23.0)
//use
static factory that describe the args
|
Form of colloquialisms or slang
|
eatMyShort()
|
abort()
|
Different methods name of different classes with same code base
|
fetch(), retrieve(), get()
|
get() //pick
one word for one abstract concept and stick with it
|
Different class name in the same code base
|
Controller,
manager, driver
|
Controller
|
The variable names are not clear when they are used alone in a
method
|
street, houseNumber, city, state
|
//Solution 1: prefix addressStreet, addressHouseNumber,
addressCity, addressState
//Solution 2: create a class Address
|
References:
Comments
Post a Comment