jQuery in Action, 3rd edition
The moose likes IDEs, Version Control and other tools and the fly likes PMD - Customize ShortVariable rule Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "PMD - Customize ShortVariable rule" Watch "PMD - Customize ShortVariable rule" New topic

PMD - Customize ShortVariable rule

Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 430

during my nightly builds I also let PMD have a look at my code. Now the PMD rule ShortVariable complains about all my "id"-fields in my domain-objects that the variable name is too short. Is there any (easy) way to tell PMD to ignore any variable name which is called "id"?
I don't want to exclude the rule per se and I'm also too lazy to write after every id-field a "//NOPMD". In the official PMD documentation I couldn't find any information about altering the rule. The only thing I can think of is writing my own rule. But isn't there any easier way? (If not, has someone wrote such a rule already? ;-)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

This wouldn't be the first time I missed the obvious, but what is "PMD"?

You might want to rethink your short names, though. Some ORM systems focus on variables whose named end with "id" and attempt to identify them as primary keys. The convention is for something like "delinquentsId" to be the key for the "delinquents" table. When you have a lot of foreign keys, this can be a big help in avoiding confusion.

Some things like loop variables don't really need long descriptive names, but items of larger scope do benefit. For one thing, it's easier to track down how they're being used when using an editor search if the name is both unique and long enough that you won't get false hits because it's part of a common larger usage - like naming a variable "ng" in a class full of String methods.

Of course, who wants to type long windy names? Fortunately the refactoring that most modern IDEs provides means that you can write your code with a variable named "ix" and then refactor it to "boyWhoDoesntKnowWhatAHrungIsOrWhyItShouldCollapseOnHisPlanet". And not accidentally zap the variable named "sixPence".

An IDE is no substitute for an Intelligent Developer.
Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 430

Mea culpa. PMD is a static code analyzer (http://pmd.sourceforge.net/) similar to FindBugs (http://findbugs.sourceforge.net/).
I am aware of the shortcomings of short variable names but it was a conscious decision that the variable name "id" is allowed in our code. Thanks for the reply anyway, Tim.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

I think id is a valid name in a short method. Say we have an object that contains a name and id. It is obvious what id refers to.

According to the PMD documentation, short rule is an XPath rule:
//VariableDeclaratorId[string-length(@Image) < 3]
[not((ancestor::FormalParameter) and (ancestor::TryStatement))]

As a developer, this makes it changeable. Or more specifically to not run their rule and use your own similar rule. One that is basically what is here with an additional condition. Not the easiest change in the world if you aren't familiar with extending PMD rules (or XPath), but likely easier than adding //NOPMD all over.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 430

Hm, so I guess I have to dig into XPath (and extending PMD) when I find some time. Something I wanted to avoid but if there's no other way...
Thanks for your feedback.
I agree. Here's the link: http://aspose.com/file-tools
subject: PMD - Customize ShortVariable rule
It's not a secret anymore!