简体   繁体   English

将常量放在哪里:标准和最佳实践

[英]Where to put constants in a class: standards and best practice

While coding some custom stream reader for a script result where quite a lot of constants were present in the class (mainly for expected tags and keywords), I was wondering if there was any kind of standards, conventions or best practice for where to put constants (read here static final fields) inside a class? 在为某个类中存在很多常量(主要是预期的标记和关键字)的脚本结果编码一些自定义流读取器时,我想知道是否存在用于将常量放在何处的任何标准,约定或最佳实践(在此处阅读static final字段)在类中?

More specifically, is it considered best to put every constant in the top of the class, or to group them up in the area of the class where they are useful, and group the commons at the top? 更具体地说,是否最好将每个常量都放在班级的顶部,或者将它们分组在班级中它们有用的区域中,并将公用区放在顶部?

By putting everything in the top, it seems to me that this might be easier to find everything you're looking for at the same place, but it can get overwhelming if this area is getting bigger: 通过将所有内容放在顶部,在我看来,这可能更容易在同一位置找到您要查找的所有内容,但是如果这个区域越来越大,它可能会变得不知所措:

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!");
    }
}

By grouping them up, it makes more sense to the constants that are only used in specific areas of the class, but it may break the nice symmetrical effect grouping by types adds to a class, and might make it look messy : 通过将它们分组,对仅在类的特定区域中使用的常量更有意义,但是它可能破坏按类型添加到类的分组所产生的对称效果,并且可能使它看起来杂乱

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!");
    }
}

Output: 输出:

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

I know this might be the subjective kind of question, but I'm mainly interested in finding out if there is any (un)official document stating this, or which of the above scenarios is considered best practice and more appealing to work with for the average programmer. 我知道这可能是主观的问题,但我主要是想了解是否有(非正式)官方文件说明此问题,或者上述哪种情况被认为是最佳做法,并且更适合与普通程序员。

Take note that the Javadoc was discarded to lighten up the above example code. 请注意,为了简化上面的示例代码,Javadoc被丢弃了。

There is a standard set of conventions on Oracle's site. Oracle网站上有一组标准约定。 There's a reference to how to layout the source code that places static variables ahead of instance variables, but there's nothing particular about constants. 关于如何布局将静态变量放在实例变量之前的源代码参考 ,但是常量没有什么特别的。

Oracle's style guide has not been updated since 1999,and has a warning on it that the information may no longer be valid. Oracle的样式指南自1999年以来未进行过更新,并在其上发出警告,指出该信息可能不再有效。 Google has a more up-to-date style guide , it says: Google拥有更新的样式指南 ,它说:

3.4.2 Class member ordering 3.4.2班级成员订购

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. 例如,新方法不只是习惯性地添加到类的末尾,因为这会产生“按日期排序的时间顺序”排序,这不是逻辑排序。

Common practice is to put constants at the top ahead of everything else, but there's no absolute mandate. 通常的做法是将常量放在所有其他事物的顶部,但是没有绝对的要求。 If you have a lot of constants and need to organize them, grouping them in enums would be one alternative. 如果您有很多常量并且需要对其进行组织,则将它们分组为枚举将是一种替代方法。

Idiomatically, constants go at the top, and are named in upper case. 习惯上,常量位于顶部,并以大写字母命名。 As @AlexisLeclerc says in the comments, you should only use constants when they are used in more than once place, otherwise you're just making the code harder to read. 就像@AlexisLeclerc在注释中所说的那样,只应在多个地方使用常量时才使用常量,否则只会使代码更难阅读。 However, this is idiomatic, not standard. 但是,这是惯用的,不是标准的。

If you're grouping things in your class as described, it may indicate that you should actually have two or more classes. 如果按照说明将类分组,则可能表明您实际上应该有两个或多个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM