Can excessive use of final hurt more than do good?

2019-04-06 04:58发布

问题:

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?

回答1:

  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.



回答2:

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.



回答3:

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.



回答4:

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



回答5:

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.



回答6:

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.



回答7:

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.



回答8:

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.