-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Vincent,

Vincent Hennebert schrieb:
>> 18000 PMD violations is just sick. Things like rule [1] doesn't really help
>> the source code. We can do that if we get a budget for 
>> nuclear-power-plant-grade
>> software.
> Same here I guess. Now may be the right time to launch the debate,
> actually. I’ll try to gather some energy in the next days for that.

PMD contains many different check sets. The main reason for the large
number of violations is that I've enabled many of the check-sets, among
those "optimizations" and "design", which are responsible for the large
number of error messages. Maybe we should start with the "basic" set and
go from there?

>> [1] 
>> http://pmd.sourceforge.net/rules/optimizations.html#MethodArgumentCouldBeFinal

> +1
> I must say, I’ve never really grasped the benefit of doing this. I’d be
> happy to be enlightened, though.

Sure:

Declaring a parameter / variable as final makes it immutable in the
method where it is used. This is:

- - required if the variable is used in an anonymous inner class (as a way
of passing "parameters" into one)

- - good coding practice on all methods: If a parameter is re-assigned,
the value may not be what the programmer actually intended it to be.

Example:

void someMethod(List l)
{
  if (l==null) l = new LinkedList();
  l.add("test");
}

or even worse, this attempt to fix it:

List someMethod(List l)
{
  if (l==null) l = new LinkedList();
  l.add("test");
  return l;
}

Here we have a very subtle code bug, which is triggered only in a few cases:

List l1;
List l2;
...
l1 = someMethod(l1); // does not trigger
l1 = someMethod(l2); // does trigger

This rule is listed under "optimizations", because final can also be
used as a hint for hotspot, and many people use it mostly for
optimization (although the performance advantage is debatable).


Max
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIUlXI+Gr+4pk71JwRAsf+AJsEKJgPlO528ArRQ2/QO05Xn0MBvwCfeRAY
VP8QJHJoHsccbd3b40xS7pU=
=/b+s
-----END PGP SIGNATURE-----

Reply via email to