Use Intention-Revealing Names
names should reveal intent
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);
}
}
Avoid Disinformation
Do not leave false clue.
- Do not name a variable as
xxxListunless it is a List ( The List has a specific meaning to programmers). If not, it might be better to usexxxsorxxxGroup - Be aware of the variable names which vary in small ways, like
dataanddate;I,land1;0,oandO;thisIsALongVariableandthisIsaLongVariable
Meaningful Distinctions
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)
disctions
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:
variableshould never appeas in a variable namenameStringis not better thanname. Would a name ever a floating number?getAccount() getAccounts() getAccountInfo()are indistinguishable.
Pronounceable Names
Even if you can understand genymhms means generate year month day hour minute and second, you should make it pronouncable like generateTimestamp
Searchable Name
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;
}
Avoid Encoding
Hungarian Notation
- There is no need to add the type of the variable in the front of the name like
strXXXintXXX
Memebr Prefix
you don’t have to prefix m_ with member variables, and lp_ with long pointers
Interface and Implement
It is suggested to use xxx as interface and xxxImpl as implement, instead of Ixxx as interface and xxx as implement.
Avoid Mental Mapping
avoid use single-letter variable to juggle
Class Name
- class names should be a noun rather than a verb
- avoid using manager, Processor, data, info
Method Name
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);
Don’t be cute
avoid colloquialism, just mean what you say
Pick one work per Concept
get retrieve fetch are basically equivalent methods
controller maneger driver
Don’t pun
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.
Use Solution/Problem Domain Name
(使用解决方案领域名称,使用源自所涉问题领域的名称)
Add Meaningful Context
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.
Don’t add gratuitous context
- Like unified prefix to make you hard for IDE to help you
- or add redundant or irrevelant words in a variable name;
本文强调了在编程中使用具有明确意图、避免误导和冗余的变量名的重要性,提倡使用类名和方法名时遵循一致性原则,以及避免编码技巧如匈牙利标记法。同时,建议使用描述性强的术语和上下文,提高代码的可搜索性和可理解性。
2891

被折叠的 条评论
为什么被折叠?



