java方法局部常量应该如何命名?
如果常量仅在方法的上下文中使用,则应在方法内部声明它(作为 final
变量)。但是应该使用什么语法呢?对我来说,最可读的方式是使用 ALL_CAPS
,但 checkstyle 似乎不同意:
int compute(int n) {
final int FACTOR = 27;
return FACTOR * n;
}
将产生 checkstyle 警告:
Name 'FACTOR' must match pattern '^[a-z][a-zA-Z0-9]*$'.
最佳答案
Google Java Style ,当前流行的风格指南,说:
Local variable names are written in lowerCamelCase [...] Even when final and immutable, local variables are not considered to be constants, and should not be styled as constants.
这也是 Checkstyle 的 LocalFinalVariableName 检查采用的约定。
关于java - checkstyle 中方法局部常量的命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25846174/