java - 将常量放在类中的位置 : standards and best practice

标签 java coding-style constants standards conventions

在为脚本结果编写一些自定义流阅读器时,类中存在相当多的常量(主要用于预期的标签和关键字),我想知道是否有任何类型的标准、约定或最佳实践将常量(在此处阅读 static final 字段)放入类中?

更具体地说,是将每个常量放在类的顶部,还是将它们分组在类中有用的区域,并将公共(public)项放在顶部?

通过将所有内容都放在顶部,在我看来,这可能更容易在同一位置找到您要查找的所有内容,但如果该区域变大,它可能会让人不知所措:

public class Test {
    // Constants.
    private static final String CLASSNAME = Test.class.getSimpleName();
    private static final String COMMON = " = ";
    private static final String CONSTRUCTOR = "#constructor";
    private static final String METHOD_1 = "#method1";
    private static final String METHOD_2 = "#method2";

    public Test(String message) {
        System.out.println(CLASSNAME + CONSTRUCTOR + COMMON + message);
        method1(message);
        method2(message);
    }

    private void method1(String message) {
        System.out.println(CLASSNAME + METHOD_1 + COMMON + message);
    }

    private void method2(String message) {
        System.out.println(CLASSNAME + METHOD_2 + COMMON + message);
    }

    public static void main(String[] args) {
        new Test("Hello world!");
    }
}

通过将它们分组,对于仅在类的特定区域中使用的常量更有意义,但它可能会破坏按类型分组添加到类中的良好对称效果,并且可能会使它看起来凌乱:

public class Test {
    // Common constants.
    private static final String CLASSNAME = Test.class.getSimpleName();
    private static final String COMMON = " = ";

    // Constructor constants.
    private static final String CONSTRUCTOR = "#constructor";

    public Test(String message) {
        System.out.println(CLASSNAME + CONSTRUCTOR + COMMON + message);
        method1(message);
        method2(message);
    }

    // Constant proper to method1(...).
    private static final String METHOD_1 = "#method1";

    private void method1(String message) {
        System.out.println(CLASSNAME + METHOD_1 + COMMON + message);
    }

    // Constant proper to method2(...).
    private static final String METHOD_2 = "#method2";

    private void method2(String message) {
        System.out.println(CLASSNAME + METHOD_2 + COMMON + message);
    }

    public static void main(String[] args) {
        new Test("Hello world!");
    }
}

输出:

Test#constructor = Hello world!
Test#method1 = Hello world!
Test#method2 = Hello world!

我知道这可能是一个主观的问题,但我主要想知道是否有任何(非)官方文件说明这一点,或者上述哪些场景被认为是最佳实践并且对工作更有吸引力对于普通程序员。

请注意,Javadoc 已被丢弃以简化上述示例代码。

最佳答案

Oracle 站点上有一组标准约定。有一个 reference to how to layout the source code将静态变量放在实例变量之前,但常量没有什么特别之处。

Oracle 的风格指南自 1999 年以来就没有更新过,并且上面有一个信息可能不再有效的警告。 Google has a more up-to-date style guide ,它说:

3.4.2 Class member ordering

The ordering of the members of a class can have a great effect on learnability, but there is no single correct recipe for how to do it. Different classes may order their members differently.

What is important is that each class order its members in some logical order, which its maintainer could explain if asked. For example, new methods are not just habitually added to the end of the class, as that would yield "chronological by date added" ordering, which is not a logical ordering.

通常的做法是将常量放在最前面,但没有绝对的要求。如果您有很多常量并且需要组织它们,将它们分组到枚举中是一种替代方法。

关于java - 将常量放在类中的位置 : standards and best practice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22413301/

相关文章:

java - 使用 JDOM 解析带有未在 XML 文件中声明的外部 DTD 的 XML 文件

php - 代码风格名称

f# - 有风格地学习 F#

coding-style - 风格问题 - 成员变量与 get(path)

c++ - 我应该将关键字 `extern` 添加到常量的定义中以在源文件之间共享吗?

java - 返回单个数组值,每个方法调用从索引 0 开始,每次调用递增 1。

java - 使用 Java 反射仅获取类的公共(public)方法

java - 与使用静态初始化器作为伪入口点相比,main(...) 有什么好处?

c++ - 观察者模式中的 Const-correct 通知程序

iPhone Mach-O 二进制文件,字符串存储,__TEXT/__DATA