代码不要冗余之道-有意义的命名
- 用I、l 、1、0 、O会让人摸不到头脑,但最让人苦恼的是这几个字符的组合;
- Bad
int IO=0; int l0=0; int I1=1; int l1=1;
- 变量中不要有数据类型,NameString,AgeNumber完全是多余的用现实世界中的概念来命名Name,Age 完全OK;
Bad | Good |
---|---|
NameSTring | Name |
AgeNumber | Age |
AccountInfo | Account |
UserData | User |
- 别造单词,让变量看起来通顺
Bad | Good |
---|---|
Equip | Equipment |
Gen | Generation |
Ts | Timestamp |
Genymdms | GenerationTimestamp |
- 让变量名可搜索,
- 尤其在使用常量时,别用魔数
- 别用单个字符
Bad | Good |
---|---|
7 | WORK_DAYS_PER_WEEK |