int d;// what is this variable used for?
d
reveals nothing. If d is for elapsed time in days, you should name it.
int elapsedTimeInDays;
List<int[]> result = new ArrayList();
for(int[] cell : cells){
if(cell[STATUS_VALUE] == FLAGGED){
result.add(cell);
}
}
as you can see, all variable names have their respectful meanings, But there are two points that can be modified: We can package int[]
into a class Cell
, and package cell[STATUS_VALUE] == FLAGGED
as a function isFlagged()
List<Cell> result = new ArrayList();
for(Cell cell : cells){
if(cell.isFlagged()){
result.add(cell);
}
}
Do not leave false clue.
xxxList
unless it is a List ( The List has a specific meaning to programmers). If not, it might be better to use xxxs
or xxxGroup
data
and date
; I
, l
and 1
; 0
, o
and O
; thisIsALongVariable
and thisIsaLongVariable
When it comes to the constant variable, you can name it, too! It is easier to recognize FRIDAY
than 5
in a code
if(day == 5)
const int FRIDAY = 5;
if(day == FRIDAY)
xxxData
and xxxInfo
mean no distinction even you’ve made their names different. And some stop words (noise words) like a an the
are distinct words, too.
More examples:
variable
should never appeas in a variable namenameString
is not better than name
. Would a name ever a floating number?getAccount() getAccounts() getAccountInfo()
are indistinguishable.Even if you can understand genymhms
means generate year month day hour minute and second, you should make it pronouncable like generateTimestamp
for(int i = 0; i < 34; ++i){
s += t[i] *4 / 5;
}
There codes are harder to be found than the ones below
const int TASK_NUMBER = 34;
const int WORK_DAYS_OF_A_WEEK = 5;
int sum = 0;
for(int i = 0; i < TASK_NUMBER; ++i){
int realTaskDays = taskEstimate[i] * realDaysPerIdealDay;
int realTaskWeeks = realdays / WORK_DAYS_OF_A_WEEK;
sum += realTaskWeeks;
}
strXXX
intXXX
you don’t have to prefix m_
with member variables, and lp_
with long pointers
It is suggested to use xxx
as interface and xxxImpl
as implement, instead of Ixxx
as interface and xxx
as implement.
avoid use single-letter variable to juggle
methods should have verb or verb phrase names like save
getName
If there is overloaded constructor, it is supposed to use static factory methods with name that describes arguments;
Complex.fromRealNumber(23.0);
new Complex(23.0);
avoid colloquialism, just mean what you say
get retrieve fetch
are basically equivalent methods
controller maneger driver
Using the same term for two different ideas is a pun.
For example, when you has a method that puts a variable to a collection, should you use add
? Maybe it is better to use insert
or append
to avoid pun.
(使用解决方案领域名称,使用源自所涉问题领域的名称)
public enum SellerApplyStatusEnum {
APPLY_STATUS_DEFAULT(0, "待审核"),
APPLY_STATUS_ENABLE(1, "通过审核"),
APPLY_STATUS_IGNORE(2, "忽略的申请"),
APPLY_STATUS_MODIFY(3, "退回修改"),
APPLY_STATUS_BRAND(4, "等待商家添加品牌"),
APPLY_STATUS_REJECT(5, "暂不合作");
}
The Class name has shown it is a status, so the prefixes are redundant.