Archive for category Metrics

Java Code Style and some Handy Plugins for Eclipse

Here are some handy plugins for Eclipse that I came across via an IBM Developerworks article by Paul Duvall.

CheckStyle: For coding standards
PMD's CPD: Enables discovering code duplication
Coverlipse: Measures code coverage
JDepend: Provides dependency analysis
Eclipse Metrics plugin: Effectively spots complexity

I have used the Checkstyle plugin for a few years now and found it extremely useful.  In a nutshell, checkstyle will check your code against coding standards and issue a warning if you code something that is not in the correct format.   This has always been a real bone of contention with fellow developers.  One developer I worked with in the past felt that everyone should be free to code in whatever style they prefer.  I would not agree with this at all.  It can lead to code that is very hard to maintain and read.  

With a plugin like Checkstyle you don't need "Code Police" checking the code and informing people that they are using the wrong style.  If you want to push it one step further you can set up your automatic build (We used Cruisecontrol in the past) to mark a build as broken when it comes across a Checkstyle error. This ensures that a code base is readable and maintainable.   Here is an example of bad coding style:

if (testBoolean) myNum++;

Many developers leave out the parenthesis as the compiler will not complain, this can often lead to confusion when you expect a line below to run when the statement is true but of course it won't because the myNum++ is going to run. The correct way to write this would be as follows:

if (testBoolean)

A new developer that comes across this code will know exactly what statement is run when the if statement is true. It is also possible to change the change the Checkstyle configuration for situations where a company has their own coding convention.

 I have not used the other plugins yet but have a look at the article for how to install them.  I am interested to see how good the code duplication plugin is.   I hate when I come across "Cut and paste" coding because someone is too lazy to abstract the functionality into a base class.