<< SSO for RoundCube Webmail with Atlassian Crowd | Home | Book Review: Modular Java >>

Code Style: Final Arguments

Don't mark arguments as final as it clutters your code

Java allows you to make arguments final by declaring them as final in the argument list of the method declaration:

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

This means that inside the method you cannot change what the argument reference points to, i.e. it prevents you from doing things like this:

  foo = "bar";

It is bad style to change the reference an argument points to. You should treat all arguments as if they were marked final. It would have even been a good idea to make this a language feature and have Java treat all arguments as final by default.

However does this justify to declare all arguments as final? Some people suggest this though I haven't seen this in the wild very often.

The pros:

  • enforces not changing the reference an argument points to

    The cons:

    • makes method signatures longer and harder to read
    • takes longer to write, being lazy is a virtue

    There is one thing to note: Changing the reference the argument points to does not actually change the value of the caller's variable passed to the method:

    public void testDoSomething()
    {
      String foo = "foo";
      doSomething(foo);
      System.out.println(foo); // still prints "foo"
    }
    
    public void doSomething(String foo)
    {
      foo = "bar";
    }
    

    However if you pass an object that is not immutable and you change the state inside the called method you actually do change the caller's data. This is nothing a final modifier can prevent you from doing though it may be a source of trouble if not properly stated in the contract of the method.

    So to sum it up: You should not change the reference an argument points to as it causes confusion. You can prevent this by adding the final modifer. Doing so however clutters your code and thus shouldn't be done (except when needed to use the argument in an inner class). Pay attention to not change the state of an object passed to a method if that's not part of the method's contract.

    The same applies to some extend to final local variables. Use them where they make your code easier to understand but not everywhere you could. If you are a fan of final have a look at Scala's val keyword.

    Tags :


Re: Code Style: Final Arguments

Nevertheless I think it's good practice if you think of the educational aspect for novice developers. Those who are not too familiar with general best practices yet are made to think about why they are to code the "final" modifier and that way they might for the first time get in touch with good coding style. For me making developers think about what they code and why they code it the way they do is the only "right to live" for most of the PMD, findbugs and checkstyle rules.

I think after a while every good developer should have learned by heart not to reassign parameters and that's the time when the virtue of lazyness may prevail. ;-)

So, just do it for the kids!

Cheers
Alex

Add a comment Send a TrackBack