过度使用 Final 会弊大于利吗?

发布于 2024-08-21 07:35:18 字数 151 浏览 10 评论 0原文

为什么人们如此强调将类中的每个变量设为“最终”?我不认为将 Final 添加到私有局部变量有任何真正的好处,或者真正将 Final 用于除常量和将变量传递到匿名内部类之外的任何内容。

我不想引发任何形式的口水战,我只是真诚地想知道为什么这对某些人如此重要。我错过了什么吗?

Why are people so emphatic about making every variable within a class "final"? I don't believe that there is any true benefit to adding final to private local variables, or really to use final for anything other than constants and passing variables into anonymous inner classes.

I'm not looking to start any sort of flame war, I just honestly want to know why this is so important to some people. Am I missing something?

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

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

发布评论

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

评论(8

丢了幸福的猪 2024-08-28 07:35:18
  1. 意图。修改您的代码的其他人不会更改他们不应该更改的值。

  2. 如果编译器知道字段的值永远不会改变,则可以进行编译器优化。

另外,如果类中的每个变量都是最终变量(正如您在帖子中提到的那样),那么您就有一个不可变的类(只要您不公开对可变属性的引用),这是实现线程安全的绝佳方法。

  1. Intent. Other people modifying your code won't change values they aren't supposed to change.

  2. Compiler optimizations can be made if the compiler knows a field's value will never change.

Also, if EVERY variable in a class is final (as you refer to in your post), then you have an immutable class (as long as you don't expose references to mutable properties) which is an excellent way to achieve thread-safety.

人生戏 2024-08-28 07:35:18

缺点是,

annoy it is hard
annoy to read
annoy code or anything
annoy else when it all 
annoy starts in the 
annoy same way

除了创建常量和防止子类化/覆盖的明显用途之外,在大多数情况下这是个人偏好,因为许多人认为“显示程序员意图”的好处被实际代码的可读性所抵消。许多人喜欢少一点冗长。

至于优化,这是使用它的一个糟糕的理由(在许多情况下毫无意义)。这是最糟糕的微观优化形式,在 JIT 时代毫无意义。

如果您愿意,我建议您使用它,如果您愿意,则不要使用它。由于在很多情况下这都会归结为宗教争论,所以不用担心。

The downside, is that

annoy it is hard
annoy to read
annoy code or anything
annoy else when it all 
annoy starts in the 
annoy same way

Other than the obvious usage for creating constants and preventing subclassing/overriding, it is a personal preference in most cases since many believe the benefits of "showing programmer intent" are outweighed by the actual code readability. Many prefer a little less verbosity.

As for optimisations, that is a poor reason for using it (meaningless in many cases). It is the worst form of micro optimisation and in the days of JIT serves no purpose.

I would suggest to use it if you prefer, don't if you that is what you prefer. Since it will all come down to religious arguments in many cases, don't worry about it.

若水般的淡然安静女子 2024-08-28 07:35:18

这标志着我不希望该值发生变化,这是免费文档。这样做是因为它清楚地传达了该变量的意图并强制编译器进行验证。除此之外,它还允许编译器进行优化。

It marks that I'm not expecting that value to change, which is free documentation. The practice is because it clearly communicates the intent of that variable and forces the compiler to verify that. Beyond that, it allows the compiler to make optimizations.

夜访吸血鬼 2024-08-28 07:35:18

这很重要,因为不变性非常重要,尤其是在处理共享内存模型时。如果某些东西是不可变的,那么它就是线程安全的,这使得它成为一个足够好的论据,可以作为最佳实践来遵循。

http://www.artima.com/intv/blochP.html

It's important because immutability is important particularly when dealing with a shared memory model. If something is immutable then it's thread safe, that makes it good enough an argument to follow as a best practice.

http://www.artima.com/intv/blochP.html

无敌元气妹 2024-08-28 07:35:18

我目前正在处理的一个项目的设置方式是,每当在 Eclipse 中按下“保存”时,final 修饰符就会添加到代码中未更改的每个变量或字段中。而且它还没有伤害任何人。

A project I'm currently working on is setup in a way that whenever one presses "save" in Eclipse, the final modifier is added to every variable or field that is not changed in the code. And it hasn't yet hurt anybody.

我们的影子 2024-08-28 07:35:18

并发编程的一个好处尚未提及:

当构造函数执行完成时,保证 Final 字段被初始化。

One benefit for concurrent programming which hasn't been mentioned yet:

Final fields are guaranteed to be initialized when the execution of the constructor is completed.

猫弦 2024-08-28 07:35:18

正如其他地方所述,使用 Final 有很多充分的理由。在我看来,一个不值得的地方是方法的参数。严格来说,关键字在这里增加了价值,但价值还不足以承受丑陋的语法。我更愿意通过单元测试来表达此类信息。

There are many good reasons to use final, as noted elsewhere. One place where it is not worth it, IMO, is on parameters to a method. Strictly speaking, the keyword adds value here, but the value is not high enough to withstand the ugly syntax. I'd prefer to express that kind of information through unit tests.

旧瑾黎汐 2024-08-28 07:35:18

我认为对类内部的值使用 Final 是一种矫枉过正,除非该类可能会被继承。唯一的优点是编译器优化,这肯定会受益。

I think use of final over values that are inner to a class is an overkill unless the class is probably going to be inherited. The only advantage is around the compiler optimizations, which surely may benefit.

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