代码不要冗余之道-有意义的命名

  1. 用I、l 、1、0 、O会让人摸不到头脑,但最让人苦恼的是这几个字符的组合;
  • Bad
    int IO=0; int l0=0; int I1=1; int l1=1;
  1. 变量中不要有数据类型,NameString,AgeNumber完全是多余的用现实世界中的概念来命名Name,Age 完全OK;
Bad Good
NameSTring Name
AgeNumber Age
AccountInfo Account
UserData User
  1. 别造单词,让变量看起来通顺
Bad Good
Equip Equipment
Gen Generation
Ts Timestamp
Genymdms GenerationTimestamp
  1. 让变量名可搜索,
  • 尤其在使用常量时,别用魔数
  • 别用单个字符
Bad Good
7 WORK_DAYS_PER_WEEK
posted @ 2023-01-05 09:37  喜爱糖葫芦  阅读(13)  评论(0编辑  收藏  举报