• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

use of local final variables

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

If any of you have ever used PMD to analyze your source code, you may have found that it flags all local variables that are never reassigned a value to be declared final. I have never done this in my years of coding Java. Their argument is that it signifies intent to the reader of your code and also provides hints to the compiler to do some minor optimization.

I would argue that it only clutters the code and does not give any more optimization. Aren't todays compilers smart enough to see that a variable is not reassigned a value ( even if it's not been declared final ), and perform this optimization anyway? This style of coding is also not in the Java Coding Conventions document.

What do you folks think? How many of you declare your local variables as final and why?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as you are not using the local variables in an inner class, declaring them as final sounds dumb to me. I would like to see the Java byte codes that show any any advantage to this technique.
Bill
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. The additional keywords clutter the code and make reading it harder.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a school of thought that says that this helps with code understanding. If a variable is final, then it's obvious that the value never changes; once you know the value, you know it's never going to change. This didn't start with Java, but in other languages; in particular, it's been translated from C and C++ where "const" is commonly used for this sort of thing.

If you've got a 100 lines of procedural code, a few well-placed "const"s can really make you more confident in your understanding. But this kind of code is strongly discouraged in Java. A local "final" is indeed just clutter in a five-line method.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
There's a school of thought that says that this helps with code understanding. If a variable is final, then it's obvious that the value never changes; once you know the value, you know it's never going to change.


I belong to that school of thought I used to never use final, but started doing so about six months ago. It makes understanding the code at a later date slightly faster/easier. I know on first read of a method (even a short one) that the variable will not be modified. Otherwise, I would have to read it an extra time. And that does add up.

More importantly, using final to declare your intent allows for static code analyzers (like PMD) and the compiler to be your friend. If you usually declare local variables final and don't somewhere, you re-examine your code when the static analyzer reminds you about it. Since bugs cluster, this gets you looking at a piece of code that is more likely to have a bug in it. At the opposite end of the spectrum, the compiler will stop you from accidently modifying that variable in the future when you are maintaining the code. The original author may have made some assumptions about the variable not changing.

Granted this is more important in long methods, but it is still useful in shorter ones. And many of us (myself included) aren't at the point where all our methods are five lines!
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm an advocate of always using final for variables which won't change, including method parameters. I use final purely for code understandability, any compiler efficiency is a useful side effect. The way I write my code, I can instantly tell whether a variable is going to be changed. I don't write that many five-line methods, but if I use final in one of them, is there a real problem?

I review an awful lot of code, and I can tell you that not using final when you could (this applies not just to variables but also to classes and methods) makes the code much harder to understand.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I chime in as a Hired Hand who has spent a LOT of time maintaining code written by some guy who is no longer around?

I like to see const/final. It sometimes helps decipher code.

I sometimes just stick it on to a member in code I'm deciphering to see if the compiler complains and, if not, I know the value doesn't change and I leave it there for posterity.

(Which made me remember another one I like to do: comment out imports to see if they're really needed. You'd be surprised at how many are not.)

Edited to add: ok, I can see how it seems dumb on a local in a 2 line method.

Mark
[ May 30, 2005: Message edited by: Mark Wuest ]
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic