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":
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):
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 (I had to change their extensions to .txt) 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.
| Attachment | Size |
|---|---|
| Checkstyle configuration for regular Java sources | 6.32 KB |
| Checkstyle configuration for test Java sources | 6.19 KB |
Comments
CheckStyle isn't just about style
June 26, 2008 by Anonymous (not verified), 2 years 9 weeks ago
Comment id: 1618
You've been misled by the name ... CheckStyle may have begun as purely a style checker, but since under the hood it has antlr2 and a decent java grammar, it's able to do much more, most everything PMD can do, and can be extended for your own checks.
For example:
CheckStyle isn't just about style
June 26, 2008 by pbielicki, 2 years 9 weeks ago
Comment id: 1619
Yes - you're right about that what Checkstyle can do. And I'm not mislead by the name :) Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard. Style or coding standard/convention is pretty much the same for me.
Anyway I didn't write that Checkstyle is better or worse than PMD. I don't want to compare these tools - I just recommend to use them both.
Checkstyle and Eclipse Formatter
November 15, 2008 by Anonymous (not verified), 1 year 41 weeks ago
Comment id: 1998
Hi,
First of all, thanks for the awesome article. I have tried CheckStyle sometime back with Eclipse, and I believe it would be better if it is possible to configure the Eclipse Code Formatter with a profile which would match the checkstyle configuration. I googled about this, but couldn't find any formatter profile for this purpose. How do you guys handle this? Do you use Eclipse formatter ? If so, have you created a formatter profile manually ?.
Specially, this becomes a problem when adding checkstyle to existing code bases. If there's an eclipse formatter profile, it is much easier to re-format existing code to match with checkstyle.
Thanks again.
Hi, I totally agree with you
November 15, 2008 by pbielicki, 1 year 41 weeks ago
Comment id: 1999
Hi,
I totally agree with you but unfortunately I don't know how to do this. On the bright side I will tell you that it has never been an issue for me. Remember that you should have only ONE checkstyle (CS) configuration in your company and you could configure Eclipse Formatter (EF) accordingly (you may also share EF configuration globally in your organization).
It's true that EF sometimes screws up the code but I can assure you that even in big projects with hundreds (or thousands) of Java classes when you format all the code using EF you will have little work to do to make it compliant with CS rules. As far as I remember there is only one or maybe two discrepancies between CS and EF that cannot be configured. All the other stuff is easily changeable in the EF preferences page and you can share your configuration in all your projects.
And one more advice regarding EF - don't format the whole class - you may hurt yourself. You should format only a selected fragment(s) of code (and to be honest I format my Java code manually quite often because EF can make it unintelligible). So, if you format the whole Java class you will damage what somebody else was working on for quite a while.
I hope it helps.
Cheers!
uggs on sale
September 1, 2010 by uggs on sale2010, 1 day 15 hours ago
Comment id: 11389
1.Attention please! There are various UGGS On Sale in our website. UGG Sale offers large discount and free shipping for all customers throughout the world. welcome to choose freely from all the UGG Boots Sale.|UGG Adirondack II|UGG Adirondack Tall|UGG Bomber Jacket|UGG Broome|UGG Chrystie|UGG Elsey wedge|UGG Felicity|UGG Gissella|UGG Highkoo|UGG Kensington|UGG Knightsbridge|UGG Liberty|UGG Locarno|UGG Mayfaire|UGG Payton|UGG Roseberry|UGG Roxy Tall|UGG Sandra|UGG seline|UGG Stella |Ugg Stripe Cable Knit Tall|UGG Suede|UGG upside|UGG Bailey Button|UGG Bailey Button Fancy|UGG Bailey Button Triplet|UGG Langley|UGG Corinth|UGG Ashur|UGG Cove|UGG Delaine.
2.ugg uk|tiffany
Snow boots
September 1, 2010 by Snow boots, 1 day 12 hours ago
Comment id: 11602
Our website-- 2ugg is making a great promotion now. There are many preferential ugg boots . At present , hot sale ugg classic boots such as:
Snow Boots
| UGGs On Sale
| UGG Australia Boots
| UGG Classic Boots
| UGG Classic Short Boots II
| UGG Gissella Boots
| UGG Payton Boots
| UGG Sandra Boots
| UGG Bailey Button Triplet Boots
| UGG Kensington Boots
| UGG Classic Tall Stripe Cable Knit Boots
| UGG Felicity Boots
| UGG Classic Tall Boots 5885
| UGG Chrystie Boots
| UGG Broome Boots
| UGG Bailey Button Fancy Boots
| UGG Adirondack Boots II
| UGG Kid's Boots
| UGG Kid's Bailey Button Boots
| UGG Kid's Classic Tall Boots
| UGG Kid's Classic Short Boots
| UGG Men 's Classic Short Boots
| UGG Gaviota Boots
| UGG Swell Tall Boots
| UGG Roseberry Boots
| UGG Brookfield Tall Boots
| UGG Stella Boots
| UGG Caroline Boots
| UGG High-Heel Tall Boots
| UGG Tess Boots
| UGG Desoto Boots
| UGG Brookfield Boots
| UGG Smithfield Boots
| UGG Shoreline Boots
| UGG Ashur Boots
| UGG Cove Boots
| UGG Men's Brookfield Boots
| UGG Bailey Button Boots
| UGG Argyle Knit Boots
| UGG Cardy Boots
| UGG Crochet Boots
| UGG Flower Boots
| UGG Mini Boots
| UGG Paisley Boots
| UGG Short Boots
| UGG Tall Boots
| UGG Elsey Wedge Boots
| UGG Infants Erin Boots
| UGG Langley Boots
| UGG Lo Pro Boots
| UGG Locarno Boots
| UGG Mayfaire Boots
| UGG Nightfall Boots
| UGG Rainier Eskimo Boots
| UGG Sundance II Boots
| UGG Ultimate Bind Boots
| UGG Ultra Short Boots
| UGG Ultra Tall Boots
| UGG Suede Boots
| UGG Upside Boots
| UGG Roxy Boots
| UGG Seline Boots
| UGG Corinth Wedge Boots
| UGG Liberty Boots
| UGG Highkoo Boots
| UGG Knightsbridge Boots
| UGG Bomber Jacket Boots
| UGG Adirondack Boots
| UGG Suburb Crochet Boots
| UGG Boots
| UGGs On Sale
| Cheap UGG Boots Boots
| UGG Boots
| UGGs On Sale
| Cheap UGG Boots
| UGG Boots
| UGGs On Sale
| Cheap UGG Boots
| UGG Boots
| UGGs On Sale
| Cheap UGG Boots
| UGG Size Guide
| UGG Boots
| UGGs On Sale
| Cheap ugg boots
| UGG Bailey Button Boots
| UGG Classic Argyle Knit Boots
| UGG Classic Cardy Boots
| UGG Classic Crochet Boots
| UGG Classic Flower boots
| UGG Classic Tall Boots 5684 Leopard
| UGG Elsey wedge Boots
| UGG Elsey wedge boots Black
| UGG Infant's Erin Baby Boots
| UGG Langley Boots Black
| UGG Lo Pro Button Boots
| UGG Locarno Boots
| UGG Mayfaire boots
| UGG Mayfaire boots black
| UGG Mayfaire boots Sand
| UGG Nightfall Boots
| UGG Rainier Eskimo Boots
| UGG Sundance II Boots
| UGG Ultimate Bind Boots
| UGG Ultra Short Boots
| UGG Suede Boots
| UGG upside Boots
| UGG upside Boots black
| UGG Roxy Tall Boots
| Ugg Roxy Boots black
| Ugg Roxy Boots Sand
| UGG seline Boots
| UGG seline Boots black
| UGG Women's Corinth Boots in Cocoa
| UGG Liberty Boots
| UGG Highkoo Boots
| UGG Knightsbridge Boots
| UGG Knightsbridge Boots black
| UGG Bomber Jacket Boots
| UGG Adirondack Tall Boot
| UGG Suburb Crochet Boots
| Tiffany
| Tiffany Necklaces
| Tiffany Jewellery
| Timberland boots
| mbt shoes
| ugg boots sale
| ugg uk
| NBA Shoes
| Puma Shoes
| MBT Shoes
Please believe us , we will use passionate service attitude to receive you , until you buy satisfied goods.
Post new comment