5 highly critical yet rarely used PMD ru...

PMD-Tocea-Scertify-Java

5 highly critical yet rarely used PMD rules

Posted by Armel GOURIOU in Coding Rules and Guidelines on September 3, 2012 with Comments Off









Going through the statistics of rules’ usage on Techdebt.org I was surprised to find critical PMD rules that seems to be disregarded. Here is a presentation of 5 of them, with an explanation on what they check. Those rules all have two things in common:

  • their non-respect can bring huge problems into your application ;
  • their usage statistic is surprisingly low.

For each rule you will also find its usage percentage, the PMD’s ruleset where you can find it and the criticity recommended by Tocea. You may not learn anything new, but at least don’t forget to check your quality profile to make sur you use them ;)

ReturnFromFinallyBlock

Criticity : Blocker Usage : 7.75% PMD ruleset : Basic

This rule finds return in finally blocks. This is definitely a critical mistake since it can discard exceptions. If an exception, checked or unchecked, is thrown in the try block, a return in the finally block will discard it.

Example : the following code only prints “Finally”.

DoNotThrowExceptionInFinally

Criticity : Blocker Usage : 7.51% PMD ruleset : Strict Exceptions

Throwing an exception in a finally block can be really confusing and makes the code hard to debug. It will also discard any previous exception raised. For example, if an unexpected exception, like a NullPointerException, is thrown in the try block, it will be discarded. This can hide bugs, resulting in painful debugging tasks…

Example : the following code prints “Gotcha!” when you would like it to crash with a NPE.

DontCallThreadRun

Criticity : Blocker Usage : 1.08% PMD ruleset : Basic

The run() method is executed in the current thread. To create a new thread, the method start() must be used, which is usually the intended behavior. This rule is also present in Findbugs with the name “RU_INVOKE_RUN”. One of this rule should be activated.

Example : this code illustrate the behavior of thread.run() and thread.start().

AvoidStringBufferField

Criticity : Blocker Usage : 7.51% PMD ruleset :  String and StringBuffer

The use of a StringBuffer as a class’ field should be avoided. A StringBuffer can use a lot of memory. If the class has a long time life, this can lead to memory overflow. As long as it is possible, prefer to use local variables.

Example : don’t do that

FinalizeShouldBeProtected

Criticity : Blocker Usage : 7.57% PMD ruleset : Finalizer

Finalize() is called by the garbage collector when an object is collected. You should not rely on finalize to perform tasks other than freeing resources. Finalize is not meant to be called by anyone other than the garbage collector. For this reason, finalize() should not be public but protected. This rule is also in Findbugs, with the name “FI_PUBLIC_SHOULD_BE_PROTECTED”. At least one of this rule should be used.

Example : again, don’t do that

That’s the end of this article, as we have seen, violating these rules can bring some trouble. I was surprised to see that they are rarely used, so tell me : did you know about these rules? Do you think they’re worth checking?

Posted by
Armel GOURIOU (See other posts)

Armel Gouriou has a Master's degree in computer science and is a huge Java addict. He originally worked for Tocea's R&D team where he took part in the development of Tocea's audit and refactoring softwares. He is now a member of the professional services team where his mission is to provide companies with solutions to tackle their technical debt.

Tags: , , , , , ,

Comments are closed.

Run the Code Refactory
Scertify™ Professional is an Eclipse plugin dedicated to analyze, control and correction of code quality defects, through computer-assisted code refactoring. It includes +1,600 coding rule controls and several rules for automated-refactoring).

Your cart

  • No products in the cart.

Who we are?

Tocea provides Development Teams with innovative solutions enabling them to continuously control and improve the quality and integrity of their applications. We support developers, architects and project managers in their permanent effort to tackle IT debt and costs, by providing them with unique information on Code & Architecture Quality and some exclusive computer-aided refactoring features.

LoadingRetrieving latest tweet...

Back to Top

2013 © All rights reserved. Tocea SA. Legal Notice