Tuesday, July 08, 2008

How to ensure code quality?

I'm co-developing big Java project that does what it should do but lacks the style and conventions. The code is hard to read, understand and maintain - and it is Java! I'd like to write what I discovered in the code, project configuration and what are my recommendations on ensuring high(er) code quality.

In my previous post I mentioned tool called FindBugs that finds common Java problems that can occur during runtime and will be difficult to find and debug. One of the comments to this post opened my eyes to what I'm doing currently and how I can fix current status quo. And the comment was about PMD tool.

PMD
There is a tool called PMD which scans Java source code and looks for potential problems like: Possible bugs, Dead code, Suboptimal code, Overcomplicated expressions, Duplicate code.
PMD is configured in the project I currently contribute to but I was somewhat shocked that although the code is totally, absolutely crappy PMD doesn't show any problems. Hmmmmmmm. I was shortsighted because I didn't see the fragment invented probably by some kind of "genius":

public class ... extends ... { // NOPMD


Well, the fragment in question is // NOPMD which tells PMD: "Don't check this class". It's not funny!
Fortunately this was not the case for most of the classes. Nevertheless I still didn't know why my PMD plugin is not alarming me about the problems. I checked whether it is enabled for my current project in Eclipse IDE. Voila! It was not! The guy who helped me configuring the project disabled it and when I asked why he said that if I enable it I will be probably unable to compile the project. It's really not funny :) Or.... is it?
Anyway - he was right! When I enabled the PMD for the project I was really unable to compile it...

No comments!

PMD problems
I've never used PMD before and I think that was a good choice. Why? I enabled it for some classes I worked on to see why people disable it - I hoped to find some clues. I didn't wait long - actually I didn't wait at all. Consider this snippet (class' method - context is unimportant):

String value = (String) entry.getValue();


What warning could you get for this piece of code? I got this one: Local variable 'value' could be declared final. WTF?! OK - I added final keyword before value variable declaration to see what will happen. #$%&@! - I got another warning: Avoid using final local variables, turn them into fields. So, should I declare my local variables as final or not?!
Who configured this PMD? Or is it default configuration? I don't care - with such warnings I really understand people who simply disable this tool - it's insane!

Of course, everything is a matter of configuration but I'm already pissed off with PMD as a Eclipse plugin and am not going to use it anymore. On the other hand I'm always using PMD as a Maven2 plugin and it works fine - I used to get quite reasonable comments there (I'm not getting them anymore because they are already fixed ;)

Checkstyle
I've never used PMD before but I used Checkstyle instead (currently I'm using version 4.4.1). I was always very satisfied with this tool and it helped me keep many projects nice and clean (yep - I was the fascist in many projects kicking asses of other developers if they were writing ugly code :). I always used Checkstyle as a Eclipse plugin but recently I've also started generating Checkstyle Maven2 reports for our mavenized projects.
I'm attaching two XML files (see the original post) with Checkstyle configuration. One is for ordinary Java source files and the second one is more "liberal" for test classes. I worked on this configuration with couple of my colleagues from my previous company and we really think it's the best you can get from Checkstyle (however de gustibus non est disputandum).
When you incorporate Checkstyle configuration in question in your projects be sure that your code will be beautiful - it can still do nothing ;) but at least it will be well formatted and structured.

Recommendation
My very private recommendation and advice is to use.... both tools because they are complementary. Checkstyle only ensures the style of your Java code is standardized and "nice". It checks white spaces, new lines, formatting, etc. (i.e. it looks on the code line by line). On the other hand there is PMD which not necessarily checks the style of your code but it checks the structure of the whole code i.e. it looks on the code from the "higher altitude".
I would advise using Checkstyle as an Eclipse IDE as well as Maven2 plugin and PMD only as Maven2 plugin. In addition I would also advise using FindBugs as an Eclipse IDE plugin but I mentioned it already in the previous post.

Tools like PMD and Checkstyle will not ensure your code does what it should do (not even 0.1%) but they will help your team produce well structured quality code that will be much more easily understandable than before. Ensuring one code convention in your organization (whatever it will be) is always a good thing to do. And tools like PMD and Checkstyle help you make it agile way i.e. fully automated.
Belive me or not but in other companies I was able even to teach real Java-beginners how Java code should look like without spending with them any single minute. And they were producing pretty good Java code from day one. I was amazed!

PS. I'm using Eclipse 3.3.x IDE and some of the problems I encountered can be dead-issues on other IDEs.

Originally published on AgileSoftwareDevelopment.com

No comments: