What is the reason for these PMD rules?

  • DD and DU anomalies (if I remember correctly—I use FindBugs and the messages are a little different) refer to assigning a value to a local variable that is never read, usually because it is reassigned another value before ever being read. A typical case would be initializing some variable with null when it is declared. Don’t declare the variable until it’s needed.

  • Assigning null to a local variable in order to “assist” the garbage collector is a myth. PMD is letting you know this is just counter-productive clutter.

  • Specifying final on a local variable should be very useful to an optimizer, but I don’t have any concrete examples of current JITs taking advantage of this hint. I have found it useful in reasoning about the correctness of my own code.

  • Specifying interfaces in terms of… well, interfaces is a great design practice. You can easily change implementations of the collection without impacting the caller at all. That’s what interfaces are all about.

  • I can’t think of many cases where a caller would require a LinkedList, since it doesn’t expose any API that isn’t declared by some interface. If the client relies on that API, it’s available through the correct interface.

  • Block level synchronization allows the critical section to be smaller, which allows as much work to be done concurrently as possible. Perhaps more importantly, it allows the use of a lock object that is privately controlled by the enclosing object. This way, you can guarantee that no deadlock can occur. Using the instance itself as a lock, anyone can synchronize on it incorrectly, causing deadlock.

  • Operands of type short are promoted to int in any operations. This rule is letting you know that this promotion is occurring, and you might as well use an int. However, using the short type can save memory, so if it is an instance member, I’d probably ignore that rule.

Leave a Comment