6

If I look at the java source source code in the OpenJDK or Hibernate or Apache I have yet to see any local variables declared final.

This suggests that the developers of some of the most widely used java software libraries:

  • do not believe the final keyword improves readablity.

  • do not believe it significantly improves performance.

Why do the majority of contrbuters on stackoverflow believe it it should be used (based on the highest voted responses)?

user695654
  • 103
  • 5
  • Related: http://stackoverflow.com/questions/316352/do-you-finalize-local-variables-and-method-parameters-in-java – Rob Hruska Apr 06 '11 at 21:54
  • Related: http://stackoverflow.com/questions/137868/using-final-modifier-whenever-applicable-in-java – Rob Hruska Apr 06 '11 at 21:55
  • Regarding the performance bullet, I've been led to believe that it makes no difference, and that the compiler will recognize it and optimize accordingly. At best, it's a micro-optimization, and we all know what people say about that! :) – Rob Hruska Apr 06 '11 at 21:57
  • Are you claiming that final does "significantly improve performance"? This seems to indicate that it doesn't: http://stackoverflow.com/questions/266806/266981#266981 – Tim Sylvester Apr 06 '11 at 21:58
  • @Rob: Yeah I thought so too... it's not too hard for a compiler to figure out a variable is never assigned to in most cases, so it shouldn't make a difference. – user541686 Apr 06 '11 at 21:58
  • You may not want to make the local variables final in case of loop variables, formal parameters whose values needs to be adjusted. – Piyush Mattoo Apr 06 '11 at 22:05
  • 1
    @Tim: No, I wasn't claiming that it significantly improves performance. As rlibby points out the the compiler can work out most of the possible performance improvements. Possibly more significant is the JVM, it optimizes the bytecode and may choose a mode of execution that no (sane) human would have thought of! – user695654 Apr 06 '11 at 22:11
  • Sun published a code convension standard more than 10 years ago. Even basic, trivial suggestions in the standard, such as using spaces instead of tabs are not followed consistently in any released version. I mean trivial because it can be fixed across the entire code base with a single command in my IDE. – Peter Lawrey Apr 06 '11 at 22:34
  • @user695654, are you sure that `final` local variables produces different byte code? – Steve Kuo Apr 07 '11 at 00:24

5 Answers5

5

Probably because it's a hassle to type in the five LONG letters in the word final... why would they go through the pain of writing

final int x;

when it's twice as much typing as

int x;

?

We developers are lazy, you know... :P

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 1
    That's definitely true. ;P And final can be automatically added to method parameters by IDEs, which is not so easy for local variables. – Thomas Apr 06 '11 at 21:58
  • If the editor can tell you it could be final - and it can - then the compiler, too, can determine a finalable variable. So it could optimize so that there's no *performance* benefit in explicitly declaring final. That doesn't address the readability question, however. – Carl Manaster Apr 06 '11 at 22:01
  • The question wasn't whether an IDE can easily add the keyword, it is why the projects mentioned (OpenJDK, Hibernate, Apache) seem to have coding standards or commiters who conciously choose not to use it! – user695654 Apr 06 '11 at 22:03
  • @user695654 The features used in a language are rather related to *how easy* such features are to use. In Scala, for instance, using `val` ("final") has the same "complexity" as using `var` (arguments default to `val`). The obvious reason is that the keywords are the same length. Less obvious, but equally important reason: Scala code is (idiomatically) written in a "functional style" -- which is allowed by the syntax -- that can minimize side-effects (much less need to "mutate" values) and let the values "flow out" of the control structures. This is (quite sadly, IMOHO) not the case with Java. –  Apr 06 '11 at 22:11
  • @pst Yes I think the reference to Scala is relevant(ish). Maybe I should have rephrased the question to 'should we adopt the same coding standards that are implicit at popular open source projects?' – user695654 Apr 06 '11 at 22:14
  • @Thomas this is not so, IDEs can cope with making local variables final automatically just fine. – mark-cs Apr 07 '11 at 08:49
  • @Tnem Yes, they can do so, but I'd say that it's not always easy to really know whether the variable should be final, for example if you're in the process of incrementally developing a method and if on save/commit the IDE would add the `final` keyword, you might have to remove it again. For parameters it's generally easier to set up a rule like "parameters are always final" in order to enforce some coding style. – Thomas Apr 07 '11 at 09:31
4

do not believe the final keyword improves readablity.

Some people (such as myself) find excessive finals decreases readability.

do not believe it significantly improves performance.

final local variables does not improve performance.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
3

As far as I'm aware, the final keyword has no impact on the runtime performance of your variables.

I believe it's primary purpose is to assist you in the catching of bugs. If you know something is never going to change, you mark it as such. Similar to why we use annotations where we can, any time we can trade a runtime bug for a compile time error, we do. Finding an error when you're working on it, and it's fresh in your mind, and it hasn't gone and corrupted someone's data causing you to lose customers, yeah that's a very good thing. You get the compile error, you fix it, you move on, you don't break the nightly build, yeah those are good things.

corsiKa
  • 81,495
  • 25
  • 153
  • 204
2

The final keyword has two uses:

  1. declare a class or method as final in order to prevent subclassing/overrding
  2. declare a variable as final in order to prevent changing it (assigning a new value)

Case 2 is normally applied to member variables in order to make the object immutable (at least partly) or to method parameters in order to prevent accidential assignments.

In case of a local variable (i.e. method scoped and not a parameter), that's normally not necessary or wanted, since those variables are likely to be changed within the method (otherwise you might not need them, except to cache a reference for method scope).

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • The question is regarding local variables and does not ask the theoretical question about how to use the final keyword, rather specifically it asks why open source projects is made up of code that does not use the final key word. – user695654 Apr 06 '11 at 22:03
  • As already said, programmers are lazy and why use a keyword on local variables that provides little benefit if any? For parameters or class members final is fine, but for local variables it has little practical use. – Thomas Apr 06 '11 at 22:08
  • They may be lazy but judging by the quality of their software we might think of them as good. So my question is, should we follow their example? – user695654 Apr 06 '11 at 22:21
  • 'should we follow their example?' - This depends on you coding style. Besides the "this won't ever be changed again during this execution of a method" final has little use. If readability is you concern, I'd go for other options first, as already suggested. ... Oh, and I'd say that the best programmers are lazy programmers, since they often tend to keep things simple yet reusable (and use well tested libraries when appropriate instead of rolling their own code). – Thomas Apr 06 '11 at 22:27
  • I definitely agree with your justification of why lazy programmers are generally the best programmers. – user695654 Apr 06 '11 at 22:39
2

I doubt declaring a local variable final ever improves performance. By virtue of the existence of final, Java compilers are already required to be able to tell if a variable might be assigned more than once, or might not be initialized. Therefore, actually declaring a local as final doesn't tell the compiler anything it didn't already know--it's only for the benefit of the reader.

Now whether it sometimes improves readability, that's more subjective. In a complicated piece of code it can be nice to promise (to yourself, or to future readers) that a variable is only written once. But it might be nicer to simplify the code so that is readily apparent anyway.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
rlibby
  • 5,931
  • 20
  • 25