过度使用 Final 会弊大于利吗?
为什么人们如此强调将类中的每个变量设为“最终”?我不认为将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
意图。修改您的代码的其他人不会更改他们不应该更改的值。
如果编译器知道字段的值永远不会改变,则可以进行编译器优化。
另外,如果类中的每个变量都是最终变量(正如您在帖子中提到的那样),那么您就有一个不可变的类(只要您不公开对可变属性的引用),这是实现线程安全的绝佳方法。
Intent. Other people modifying your code won't change values they aren't supposed to change.
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.
缺点是,
除了创建常量和防止子类化/覆盖的明显用途之外,在大多数情况下这是个人偏好,因为许多人认为“显示程序员意图”的好处被实际代码的可读性所抵消。许多人喜欢少一点冗长。
至于优化,这是使用它的一个糟糕的理由(在许多情况下毫无意义)。这是最糟糕的微观优化形式,在 JIT 时代毫无意义。
如果您愿意,我建议您使用它,如果您愿意,则不要使用它。由于在很多情况下这都会归结为宗教争论,所以不用担心。
The downside, is that
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.
这标志着我不希望该值发生变化,这是免费文档。这样做是因为它清楚地传达了该变量的意图并强制编译器进行验证。除此之外,它还允许编译器进行优化。
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.
这很重要,因为不变性非常重要,尤其是在处理共享内存模型时。如果某些东西是不可变的,那么它就是线程安全的,这使得它成为一个足够好的论据,可以作为最佳实践来遵循。
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
我目前正在处理的一个项目的设置方式是,每当在 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.并发编程的一个好处尚未提及:
当构造函数执行完成时,保证 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.
正如其他地方所述,使用 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.
我认为对类内部的值使用 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.