This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PMD - Customize ShortVariable rule

 
Joachim Rohde
Ranch Hand
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Joachim Rohde
Ranch Hand
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34974
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joachim Rohde
Ranch Hand
Posts: 433
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic