gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 20:02:34 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com