java 尽可能将变量声明为“final”是一种好习惯吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10690440/
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
Is it a good practice to declare variables "final" wherever possible?
提问by Landon Kuhn
Possible Duplicate:
When should one use final?
可能的重复:
什么时候应该使用 final?
I tend to declare all variables final
unless necessary. I consider this to be a good practice because it allows the compiler to check that the identifier is used as I expect (e.g. it is not mutated). On the other hand it clutters up the code, and perhaps this is not "the Java way".
final
除非必要,我倾向于声明所有变量。我认为这是一个很好的做法,因为它允许编译器检查标识符是否按我的预期使用(例如它没有发生变异)。另一方面,它使代码变得混乱,也许这不是“Java 方式”。
I am wondering if there is a generally accepted best practice regarding the non-required use of final variables, and if there are other tradeoffs or aspects to this discussion that should be made aware of.
我想知道关于最终变量的非必需使用是否有普遍接受的最佳实践,以及本讨论是否还有其他权衡或方面需要注意。
回答by Dave Newton
The "Java way" is inherently, intrinsically cluttery.
“Java 方式”本质上是混乱的。
I say it's a good practice, but not one I follow.
我说这是一种很好的做法,但不是我遵循的做法。
Tests generally ensure I'm doing what I intend, and it's toocluttery for my aesthetics.
测试通常确保我在做我想做的事情,而且这对我的审美来说太混乱了。
回答by Marko Topolnik
I routinely apply final
to local variables in my code and additionally find readingcode much easier after first having marked all effictively finalvariables with the final
keyword. I regard this as a bona fide enhancement to the code, and commit it right thereafter.
我经常final
在我的代码中应用局部变量,并且在首先用关键字标记所有有效的最终变量后发现阅读代码更容易。我认为这是对代码的真正改进,并在此后立即提交。final
As for the concern of code clutter, when applied to local variables I don't find it damaging—in fact it makes me spot all declarations more easily due to syntax coloring.
至于代码混乱的问题,当应用于局部变量时,我不觉得它有害——事实上,由于语法着色,它让我更容易发现所有声明。
I must admit, though, that I dofind it unbearably cluttery when final
is used on parameters, catch blocks, enhanced-for loops and all other places except local variables in the narrow sense. This is quite unfortunate because a reassignment in these cases is even more confusing and they should really have been final by default.
不过,我必须承认,当用于参数、catch 块、增强的 for 循环和除狭义局部变量之外的所有其他地方时,我确实发现它令人难以忍受的混乱final
。这是非常不幸的,因为在这些情况下重新分配更令人困惑,并且默认情况下它们真的应该是最终的。
回答by Carl Manaster
I consider it good practice, more for maintenance programmers (including me!) than for the compiler. It's easier to think about a method if I don't need to worry about which variables might be changing inside it.
我认为这是一个很好的做法,对于维护程序员(包括我!)而不是编译器。如果我不需要担心其中的哪些变量可能会发生变化,那么考虑一个方法会更容易。
回答by Bohemian
Yes, it's a very good idea, because it clearly shows what fields must be provided at object construction.
是的,这是一个非常好的主意,因为它清楚地显示了在对象构建时必须提供哪些字段。
I strongly disagree that it creates "code clutter"; it's a good and powerful aspect of the language.
我强烈不同意它造成“代码混乱”;这是该语言的一个很好且强大的方面。
As a design principle, you should make your classes immutable(all final fields) if you can, because they may be safely published (ie freely passed around without fear they will be corrupted). Although note that the fields themselves need to be immutable objects too.
作为设计原则,如果可以,您应该使您的类不可变(所有 final 字段),因为它们可以安全地发布(即自由传递而不必担心它们会被破坏)。尽管请注意,字段本身也需要是不可变的对象。
回答by mprabhat
It definitely gives you a better code, easy to see which all variables are going to be changed.
它绝对为您提供了更好的代码,易于查看所有变量将被更改。
It also informs the compiler that it is not going to change which can result to better optimization.
它还通知编译器它不会改变,这可以导致更好的优化。
Along side it allows your IDE to give you compile time notification if you tend to do any mistake.
此外,如果您倾向于犯任何错误,它允许您的 IDE 给您编译时通知。
回答by richarbernal
Some good analysis tools, like PMD, advices to put always final
unless necessary. So the convention in that tools says it's a good practice
一些好的分析工具,如 PMD,建议final
除非必要,否则始终放置。所以那个工具中的约定说这是一个很好的做法
But I think that so many final tokens in code may get it less human-friendly.
但我认为代码中的这么多最终标记可能会使其不那么人性化。
回答by yair
You pretty much summed up the pros and cons...
你几乎总结了优点和缺点......
I can just add another con:
我可以添加另一个骗局:
the reader of the code need not to reason at all about the value of a final variable (except for rare bad-code cases).
代码的读者根本不需要推理最终变量的值(除了罕见的坏代码情况)。
So, yes, it's a good practice.
所以,是的,这是一个很好的做法。
And the clutter isn't that bad, after you get used to it (like unix :-P). Plus, typical IDEs do it automatically for ya...
在你习惯之后,杂乱并没有那么糟糕(比如 unix :-P)。此外,典型的 IDE 会自动为您完成...
回答by Jakub Zaverka
I would say yes, not so much for the compiler optimisation, but rather for readibility.
我会说是的,不是为了编译器优化,而是为了可读性。
But personally I don't use it. Java is quite verbose by itself, and if we followed everything considered "good practice", the code would be unredable from all the boilerplate. It's a matter of preference, though.
但我个人不使用它。Java 本身非常冗长,如果我们遵循所有被认为是“良好实践”的内容,那么代码将无法从所有样板中删除。不过,这是一个偏好问题。