您对方法作用域常量有何看法?

发布于 2024-07-08 04:55:19 字数 177 浏览 4 评论 0原文

例如:就

public void doSomething() {

    final double MIN_INTEREST = 0.0;

    // ...  
}

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

安人多梦 2024-07-15 04:55:19

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

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.

|煩躁 2024-07-15 04:55:19

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

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

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

My starting position is that every variable or constant should be declared/initialized as close to its 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 quite useful to the users of your class, and should be moved up.

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.)

罗罗贝儿 2024-07-15 04:55:19

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

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

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.

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

分分钟 2024-07-15 04:55:19

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

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

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.

牵强ㄟ 2024-07-15 04:55:19

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

您可能会担心,因为这是一个常量,因此,它可能看起来属于某些全局属性表。 也许确实如此。 也许事实并非如此。 您的担忧是有道理的,但没有一个地方可以容纳所有常量。

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.

一曲爱恨情仇 2024-07-15 04:55:19

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

public void doSomething() {

  final double MIN_INTEREST = 0.0;

  // ...  
}

而团队的其他成员则扩展了具有大量方法的类,现在该类是一个精彩的 500 行/50 个方法 巨型类。 想象一下工程师尝试添加带有常量的新方法的经历,他们必须 1 扫描整个类,寻找符合其需求的常量,2将常量移动到类范围,希望与现有代码不发生冲突,并且 3 还添加了它们的方法。

如果您首先在文件/类范围内添加所有常量,工程师就有一个 1 单一位置来查找现有常量,并且 2 从其他位置派生一些常量说得通。 (例如,如果您有一个 pi 常量,您可能还想定义一个值为 pi/2 的新常量)。

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 methods giant class. Imagine the experience of an engineer who is trying to adding a new method with a constant, they will have to 1 scan the whole class looking for constants that match their need, 2 move the constant to class scope hoping that there are no conflicts with existing code and 3 also add their method.

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

墨小沫ゞ 2024-07-15 04:55:19

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

示例:

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);

    }
}

输出将是:

Local-> 0.1
Global-> 0.0

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文