aspose file tools*
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
Author

PMD - Customize ShortVariable rule

Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 423

Hi,
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: 16145
    
  21

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".


Customer surveys are for companies who didn't pay proper attention to begin with.
Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 423

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
Marshal

Joined: May 26, 2003
Posts: 30780
    
157

Joachim,
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::ForInit)]
[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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 423

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: PMD - Customize ShortVariable rule