The ‘final’ Verdict…

After receiving 14 responses to final, final, final: Your Thoughts?, I have decided I am comfortable with my current coding style and will *not* start declaring method parameters and local variables final.

For Example

Some people were just plain confused by the question. I was talking about method parameters and local variables:

public class Dog {
  public void speak(final String command) {
    ...
    final int someLocalVar = 12;
    ...
  }
}

I was clearly not (well, I thought it was clear) talking about final fields or methods, which are not open for debate because they have very specific cases where they are needed:

public class Cat {
  private final String name;

  ...

  public final void doSomething() {
    ...
  }
}

I use final fields frequently, in particular for thread safety reasons. I use final methods to prevent overriding. These are not coding convention or style issues. They are simply features you sometimes need.

Back to the Original Question

Among people who understood the question, the general — although not unanimous — consensus is that final adds more noise than benefit. I tend to agree. I think this code is more readable:

public class Dog {
  public void speak(String command) {
    ...
    int someLocalVar = 12;
    ...
  }
}

That’s what I’ll keep on doing.


Laird Nelson Says:

I respect your choice, but respectfully disagree. For small projects, I do agree with you, but on the large hairy messes I tend to get airlifted into nothing beats declaring a local variable “final” when you can. Seven- and eight-thousand-line long methods (yes, methods) are not uncommon to stumble across in financial services and using the compiler to help keep you honest later on is a no-brainer in such cases.

I also find “final” too noisy. But Sun should have made all parameters final by default, as all methods in interfaces are public by default. Perhaps in Java 1.7 with a compatibility switch. I think it’s never a good idea to treat params non-final.

Peace
-stephan


Stephan Schmidt :: stephan@reposita.org
Reposita Open Source – Monitor your software development
http://www.reposita.org
Blog at http://stephan.reposita.org – No signal. No noise.

Eric Burke Says:

If confronted with a method like that, I’d declare local vars final. A kitten just died, I think. I doubt that has anything to do with “financial services”. That’s just shitty code.

Michael Says:

Preach it, Eric. There is no good reason for a method that long. If you run into something like that and leave it that way, a nun should wack your knuckles with a ruler. ;)