方法和变量命名的最佳实践

https://dzone.com/articles/best-practices-variable-and

最近在做代码review的工作,为了让大家的写出来的代码的可读性更强,因此做了一些研究,刚好在网上找到一篇不错的文章,于是笔者就不再重新发明轮子了,同时为了表示对原作者版权的尊重,因此把其翻译出来供大家参考!!!


方法和变量命名的最佳实践

@使用合适长度的变量名称

一般来说,对于循环语句的计数器,一个字符即可;对于循环的条件变量一个单词即可,对于方法一般1到2个单词,对于类一般2~3个单词;对于全局变量,一般3~4个单词

@ 对于一些特殊的单词,最好不要加上,以免画蛇添足,比如,“value”,“”equals”,“data”,“bean”....

@ 使用有意义的名称变量,变量的名字必须足够解释其用途或者内涵

@不要在变量的开通使用o_, obj_, m_ 等,变量不需要标签去表明其是一个变量

@遵守公司的命令规范同时保持命令规范的一致性, 比如UserName, UserName, USER_NAME, m_userName, username, ...

   # 为类使用驼峰命名法,比如VelocityResponseWriter

   # 对于包名全部使用小写字符:com.company.project.ui

   #对于变量名混合大小写:studentName

   # 对于常量,全部使用大写:MAX_PARAMETER_COUNT = 100

   # 对于枚举类使用驼峰命名法,对于枚举的值使用大写

 @不要在任何地方使用 '_',除非描述的常量或者枚举值。

 @ 不要在同一个类中的不同上下文中多次使用同一个名称,比如在方法,构造函数或者类中。尽量提供简单并维护性比较强的名称

@ 对于不同的目的,不要在使用同一个名称,比如方法或者条件判断中;创建一个新的有不同意义的名称去代替,尽量提供简单并维护性比较强的名称。

@不用使用非ASCII字符; 因为程序是你写的,你需要在你自己的平台上去部署和维护。
Don't use too long variable names (e.g. 50 chars). Long names will bring ugly and hard-to-read code, also may not run on some compilers because of character limit.

@ 不要使用长变量名称(比如50字符),长变量名称非常的丑陋而且很难阅读,而且在一些编译器上也不支持。

@ 最好使用同一门自然语言,千万不用混合不同的语言,比如德语和英语,否则会造成代码非常的难以阅读。

@ 使用有意义的名称去描述方法。方法的名称必须恰如其分的描述方法的目的和动作且大部分的情况下都应该以动词开头,createPasswordHash

@ 遵守编程语言本身的规范,在整个项目中需要统一风格,在各个地方都要保持大写规则一致,比如getUserName, GetUserName, getusername, ...

@ 对于Java来说,

    #方法的首字母小写,getStudentSchoolType 

   #方法参数名首字符小写,setSchoolName(String schoolName)

   # 对于方法名称,使用有意义的方法参数,这样方法名称就能自我描述方法,而不需要额外的文档去维护。


具体原文内容如下:

Best Practices for Variable and Method Naming
Use short enough and long enough variable names in each scope of code. Generally length may be 1 char for loop counters, 1 word for condition/loop variables, 1-2 words for methods, 2-3 words for classes, 3-4 words for globals.
Use specific names for variables, for example "value", "equals", "data", ... are not valid names for any case.
Use meaningful names for variables. Variable name must define the exact explanation of its content.
Don't start variables with o_, obj_, m_ etc. A variable does not need tags which states it is a variable.
Obey company naming standards and write variable names consistently in application: e.g. txtUserName, lblUserName, cmbSchoolType, ... Otherwise readability will reduce and find/replace tools will be unusable.
Obey programming language standards and don't use lowercase/uppercase characters inconsistently: e.g. userName, UserName, USER_NAME, m_userName, username, ...
 
use Camel Case (aka Upper Camel Case) for classes: VelocityResponseWriter
use Lower Case for packages: com.company.project.ui
use Mixed Case (aka Lower Camel Case) for variables: studentName
use Upper Case for constants : MAX_PARAMETER_COUNT = 100
use Camel Case for enum class names and Upper Case for enum values.
don't use '_' anywhere except constants and enum values (which are constants).
For example for Java, 
Don't reuse same variable name in the same class in different contexts: e.g. in method, constructor, class. So you can provide more simplicity for understandability and maintainability.
Don't use same variable for different purposes in a method, conditional etc. Create a new and different named variable instead. This is also important for maintainability and readability.
Don't use non-ASCII chars in variable names. Those may run on your platform but may not on others.
Don't use too long variable names (e.g. 50 chars). Long names will bring ugly and hard-to-read code, also may not run on some compilers because of character limit.
Decide and use one natural language for naming, e.g. using mixed English and German names will be inconsistent and unreadable.
Use meaningful names for methods. The name must specify the exact action of the method and for most cases must start with a verb. (e.g. createPasswordHash)
Obey company naming standards and write method names consistently in application: e.g. getTxtUserName(), getLblUserName(), isStudentApproved(), ... Otherwise readability will reduce and find/replace tools will be unusable.
Obey programming language standards and don't use lowercase/uppercase characters inconsistently: e.g. getUserName, GetUserName, getusername, ...
For example for Java, 
use  Mixed Case  for method names: getStudentSchoolType 
use  Mixed Case  for method parameters: setSchoolName(String schoolName)
Use meaningful names for method parameters, so it can documentate itself in case of no documentation.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值