This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I was using Checkstyle to check the style and coding conventions. It spit out all the parameters that are not declared as final and was complaining that it better be as the object or data type is not changed.
I was wondering if this is a better practice, as for me atleast, it is all over the place. Or should I just leave it as it is?
SCJP 1.6, SCJD, SCWCD, SCBCD.
Be nice to people on the way up cos, you'll need 'em on your way down - From somewhere I can't remember!
I am not familiar with Checkstyle, but apparantly this is a tool that suggests that all method parameters which are not modified in the method should be declared as final?
I certainly would not appreciate it if my coworkers would declare every variable thatcould be final as final*. It's a matter of style of course, but I cannot recall any code examples where this was done extensively, so I guess it isn't a general practice.
Therefore I would advise not to follow Checkstyle's suggestions in this case. You could argue (if you wanted to) that it would be confusing for the junior programmers that Sun is referring to in the instructions.
* I estimate that 99% of all method parameters is never modified. So if we followed Checkstyle's suggestions, that would mean that all but a few parameters would have to be declared as final! Not a readibility improvement as far as I am concerned!
I am not familiar with Checkstyle also but I am use a lot of final in my code. A do this to keep the code easy to read/uderstand/maintain my code. Each variable musts have a single scope and most of the times it caries a single value - there are also exceptions from this roule (by examble on iterations).
I estimate that 99% of all method parameters is never modified. So if we followed Checkstyle's suggestions, that would mean that all but a few parameters would have to be declared as final! Not a readibility improvement as far as I am concerned!
I second that. I happily use "private static final" and "public static final" for class level constants (and sometimes to make the methods and classes final), but to inject that to every local reference declaration or a method parameter is abuse. Just for curiosity, I ran a search against all java.* classes in JDK1.4 (that's about 1200 classes) and found very few lines of code where final was used as a qualifier with a local variable or method parameter.