Pmd: [java] ShortVariable: Add property with list of exclusions

Created on 1 Apr 2020  路  4Comments  路  Source: pmd/pmd

Is your feature request related to a problem? Please describe.
The rule ShortVariable says that variable names should be at least _minimum_ (default: 3) characters. In general, that's great. There are a few situations where a shorter name is acceptable, for example, in for an InputStream and id for an identifier of an entity.

Describe the solution you'd like
It would, therefore, be great if ShortVariable had an additional property called exclusions. It could be used like this, which probably also is a reasonable default:

<rule ref="category/java/codestyle.xml/ShortVariable">
    <properties>
        <property name="minimum" value="3" />
        <property name="exclusions" value="id,in" />
    </properties>
</rule>

In that case, code like InputStream in or String id would no longer be reported, as they are shorter than the declared minimum of 3 characters but listed in the list of exclusions.

Describe alternatives you've considered
The alternatives that I have considered are:

  • Disabling ShortVariable. I don't think this is a good alternative at all.
  • Changing the minimum to 2. This weakens the ShortVariable check too much.
  • @SuppressWarnings("PMD.ShortVariable") when the code does a lot of I/O and thus reasonably uses in a lot, there would be too many warning suppressions

Additional context
No additional context required.

enhancement

All 4 comments

You can use the violationSuppressRegex property to add exclusions based on the violation message. For example:

<property name="violationSuppressRegex" value="Avoid variables with short names like (in|out)"/>

This applies to every rule, and I think it's better than introducing special exclusion properties for this rule. Does this already suit your use case?

Do I conclude correctly from this that PMD either does not support localization of its messages or if it does, it will produce the English message internally and match the violationSuppressRegex against the English message?

Messages aren't localized. Maybe this could be a separate feature request. But for now, all messages are produced in English, yes

What violationSupprexRegex actually does could be better explained, see also #893.

And yes, PMD doesn't support i18n. There seems to be some usages, e.g. https://github.com/XenoAmess/p3c/tree/xenoamess_maintain_fork/p3c-pmd implemented for their custom rules localized messages.

Was this page helpful?
0 / 5 - 0 ratings