java 你对方法作用域常量有什么看法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/244191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:36:53  来源:igfitidea点击:

What are your thoughts on method scoped constants?

javaconstants

提问by Liggy

For example:

例如:

public void doSomething() {

    final double MIN_INTEREST = 0.0;

    // ...  
}

Personally, I would rather see these substitution constants declared statically at the class level. I suppose I'm looking for an "industry viewpoint" on the matter.

就个人而言,我更愿意看到这些替换常量在类级别静态声明。我想我正在寻找关于此事的“行业观点”。

采纳答案by Chris Cudmore

My starting position is that every variable or constant should be declared/initialized as close to it's first use as possible/practical (i.e. don't break a logical block of code in half, just to declare a few lines closer), and scoped as tightly as possible. -- Unless you can give me a damn good reason why it should be different.

我的起始位置是,每个变量或常量都应该在尽可能接近其第一次使用/实际的情况下声明/初始化(即不要将逻辑代码块分成两半,只是为了声明更近的几行),并将范围限定为尽可能紧。——除非你能给我一个该死的很好的理由为什么它应该不同。

For example, a method scoped final won't be visible in the public API. Sometimes this bit of information could be quit useful to the users of your class, and should be moved up.

例如,范围为 final 的方法在公共 API 中将不可见。有时,这一点信息可能对您班级的用户非常有用,应该向上移动。

In the example you gave in the question, I would say that MIN_INTEREST is probably one of those pieces of information that a user would like to get their hands on, and it should be scoped to the class, not the method. (Although, there is no context to the example code, and my assumption could be completely wrong.)

在您在问题中给出的示例中,我会说 MIN_INTEREST 可能是用户想要获得的信息之一,并且它的范围应限定为类,而不是方法。(虽然,示例代码没有上下文,我的假设可能完全错误。)

回答by chills42

I would think that you should only put them at the class level if they are used by multiple methods. If it is only used in that method then that looks fine to me.

我认为如果它们被多种方法使用,你应该只将它们放在类级别。如果它仅用于该方法,那么对我来说看起来不错。

回答by Chris Cudmore

Technically, there is no such thing as a "method scoped constant" in Java. What you are referring to is simply a final local variable; it is created an destroyed with each method invocation.

从技术上讲,Java 中没有“方法范围常量”这样的东西。你所指的只是一个最终的局部变量;每次方法调用都会创建和销毁它。

http://www.java-tips.org/java-se-tips/java.lang/how-do-i-declare-a-constant-in-java.html

http://www.java-tips.org/java-se-tips/java.lang/how-do-i-declare-a-constant-in-java.html

回答by dongilmore

Information hiding and modularity are key principles, and narrow scoping is better information hiding. If the constant is only needed by the method, the hiding is good. If and when the constant is useful elsewhere, bring it out to a broader scope, but only as broadly as needed.

信息隐藏和模块化是关键原则,窄范围是更好的信息隐藏。如果方法只需要常量,那么隐藏是好的。如果常量在其他地方有用,请将其扩展到更广泛的范围,但仅限于需要的范围。

You are probably concerned because this is a constant, and therefore, it may seem to belong to some global properties table. Maybe it does. Maybe it doesn't. Your concern is valid, but there is no one best place for all constants.

您可能会担心,因为这是一个常量,因此,它似乎属于某个全局属性表。也许确实如此。也许它没有。您的担忧是有道理的,但没有一个适合所有常量的最佳位置。

回答by anjanb

I've used this method scoped constants myself but every so often a colleague will down mod it during code reviews. Again, these colleagues are not into reading/writing open source but they are used to enterprise software.

我自己使用过这种方法作用域常量,但每隔一段时间,同事就会在代码期间修改它。同样,这些同事不喜欢读/写开源,但他们习惯于企业软件。

I tell them that it does NOT make sense to use a class level constant if it is used within a single method but I've found more than 1 colleague insisting that it be moved UP. I usually comply since I'm not so rigid unless it affects readability and/or performance.

我告诉他们,如果在单个方法中使用类级别常量,则使用类级别常量是没有意义的,但我发现有超过 1 位同事坚持将其向上移动。我通常会遵守,因为我不是那么死板,除非它影响可读性和/或性能。

回答by Gautam

I have a different take on this: IMHO its better to place them at file/class scope especially if you are working in teams for this reason: say you start with a small piece of code...

我对此有不同的看法:恕我直言,最好将它们放在文件/类范围内,特别是如果您因此而在团队中工作:假设您从一小段代码开始......

public void doSomething() {

  final double MIN_INTEREST = 0.0;

  // ...  
}

and other members of you team expand the class with whole bunch of methods and now the class is a wonderful 500 lines/50 methodsgiant class. Imagine the experience of an engineer who is trying to adding a new method with a constant, they will have to 1scan the whole class looking for constants that match their need, 2move the constant to class scope hoping that there are no conflicts with existing code and 3also add their method.

和你们团队的其他成员用一大堆方法扩展了这个类,现在这个类是一个很棒的500 lines/50 methods巨大的类。想象一下工程师的经验,他试图添加一个带有常量的新方法,他们将不得不1扫描整个类寻找符合他们需要2的常量,将常量移动到类范围,希望与现有代码没有冲突,并且3还添加了他们的方法。

If you instead add all constants at file/class scope to begin with, engineers have a 1single place to look for existing constants and, 2derive some constants from others where it makes sense. (for example if you have a constant for piyou may also want to define a new constant with a value of pi/2).

如果您开始在文件/类范围内添加所有常量,工程师可以在1一个地方查找现有常量,并2在有意义的地方从其他常量中派生出一些常量。(例如,如果您有一个常量,pi您可能还想定义一个值为 的新常量pi/2)。

回答by sakana

The reason why you can define a final variable at a class level or method (local) level it's because you can override the global static constant inside the (local) method.

之所以可以在类级别或方法(本地)级别定义最终变量,是因为您可以覆盖(本地)方法内的全局静态常量。

Example:

例子:

public class Test {

    final double MIN_INTEREST = 0.0;

    /**
     * @param args
     */
    public static void main(String[] args) {


        Test test = new Test();

        test.doSomethingLocal();
        test.doSomethingGlobal();

    }

    public void doSomethingGlobal() {

        System.out.println("Global-> " + MIN_INTEREST);

    }

    public void doSomethingLocal() {

        final double MIN_INTEREST = 0.1;

        System.out.println("Local-> " + MIN_INTEREST);

    }
}

The output will be:

输出将是:

Local-> 0.1
Global-> 0.0

So your question doesn't make any sense.

所以你的问题没有任何意义。