137

Java allows marking variables (fields / locals / parameters) as final, to prevent re-assigning into them. I find it very useful with fields, as it helps me quickly see whether some attributes - or an entire class - are meant to be immutable.

On the other hand, I find it a lot less useful with locals and parameters, and usually I avoid marking them as final even if they will never be re-assigned into (with the obvious exception when they need to be used in an inner class). Lately, however, I've came upon code which used final whenever it can, which I guess technically provides more information.

No longer confident about my programming style, I wonder what are other advantages and disadvantages of applying final anywhere, what is the most common industry style, and why.

gnat
  • 20,543
  • 29
  • 115
  • 306
Oak
  • 5,315

5 Answers5

90

I use final the same way as you. To me it looks superfluous on local variables and method parameters, and it doesn't convey useful extra information.

One important thing is that strive to keep my methods short and clean, each doing a single task. Thus my local variables and parameters have a very limited scope, and are used only for a single purpose. This minimizes the chances of reassigning them inadvertently.

Moreover, as you surely know, final doesn't guarantee that you can't change the value/state of a (nonprimitive) variable. Only that you can't reassign the reference to that object once initialized. In other words, it works seamlessly only with variables of primitive or immutable types. Consider

final String s = "forever";
final int i = 1;
final Map<String, Integer> m = new HashMap<String, Integer>();

s = "never"; // compilation error!
i++; // compilation error!
m.put(s, i); // fine

This means that in many cases it still doesn't make it easier to understand what happens inside the code, and misunderstanding this may in fact cause subtle bugs which are hard to detect.

85

Your Java programming style and thoughts are fine - don't need to doubt yourself there.

On the other hand, I find it a lot less useful with locals and parameters, and usually I avoid marking them as final even if they will never be re-assigned into (with the obvious exception when they need to be used in an inner class).

This is exactly why you should use the final keyword. You state that YOU know it'll never be re-assigned, but no one else knows that. Using final immediately disambiguates your code that tiny bit more.

J.K.
  • 13,113
41

One advantage of using final / const wherever possible is that it reduces the mental load for readers of your code.

Readers are assured that the value / reference is never altered later on. So developers need not pay attention to modifications in order to understand the computation.

I've have changed my mind regarding this after learning pure-functional programming languages. It's a relief knowing you can trust that a "variable" always holds its initial value.

marstato
  • 4,638
28

I consider final in method parameters and local variables to be code noise. Java method declarations can be quite long (especially with generics) - there's no need to make them any longer.

Unit tests can cover that

If unit tests are written properly, assigning to parameters that is "harmful" will be picked up, so it should never actually be a problem. Visual clarity is more important than avoiding a possible bug that isn't picked up because your unit tests have insufficient coverage.

Static code analysis can help

Tools like Sonar, FindBugs and CheckStyle can be configured to break the build if assignment is made to parameters or local variables, if you deeply care about such things.

Use in anonymous class

Of course, if you need to make them final, for example because you're using the value in an anonymous class, then no problem - that's the simplest cleanest solution.

Strive for readable code that is simple

Apart from the obvious effect of adding extra keywords to your parameters, and thereby IMHO camouflaging them, adding final to method parameters can often make the code in the method body become less readable, which makes the code worse - to be "good", code must be as readable and as simple as possible. For a contrived example, say I have a method that needs to work case insensitively.

Without final:

public void doSomething(String input) {
    input = input.toLowerCase();
    // do a few things with input
}

Simple. Clean. Everybody knows what's going on.

Now with 'final', option 1:

public void doSomething(final String input) {
    final String lowercaseInput = input.toLowerCase();
    // do a few things with lowercaseInput
}

While making the parameters final stops the coder adding code further down from thinking he's working with the original value, there's an equal risk that code further down may use input instead of lowercaseInput, which it shouldn't and which can't protected against, because you can't take it out of scope (or even assign null to input if that would even help anyway).

With 'final', option 2:

public void doSomething(final String input) {
    // do a few things with input.toLowerCase()
}

Now we're just created even more code noise and introduced a performance hit of having to invoke toLowerCase() n times.

With 'final', option 3:

public void doSomething(final String input) {
    doSomethingPrivate(input.toLowerCase());
}

/** @throws IllegalArgumentException if input not all lower case */ private void doSomethingPrivate(final String input) { if (!input.equals(input.toLowerCase())) { throw new IllegalArgumentException("input not lowercase"); } // do a few things with input }

Code Noise

Talk about code noise. This is a train wreck. We've got a new method, a required exception block, because other code may invoke it incorrectly. More unit tests to cover the exception. All to avoid one simple, and IMHO preferable and harmless, line.

There's also the issue that methods should not be so long that you can't easily visually take it in and know at a glance that an assignment to parameter has taken place.

I do think it is good practice/style that if you assign to a parameter you do it every early in the method, preferably first line or straight after basic input checking, effectively replacing it for the entire method, which has a consistent effect within the method. Readers know to expect any assignment to be obvious (near the signature declaration) and in a consistent place, which greatly mitigates the problem that adding final is trying to avoid. Actually I rarely assign to parameters, but if I do I always do it at the top of a method.


Note also that final doesn't actually protect you like it may at first seem:

public void foo(final Date date) {
    date.setTime(0); 
    // code that uses date
}

final doesn't completely protect you unless the parameter type is primitive or immutable.

hc_dev
  • 141
Bohemian
  • 2,066
8

I let eclipse put final before each local variable, since I consider it to make the program easier to read. I don't make it with parameters, since I want to keep the parameter list as short as possible, ideally it should fit in one line.

maaartinus
  • 2,713