GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] New rule: VariableDeclarationUsageDistance by zbynek · Pull Request #6640 · pmd/pmd

Conversation

@zbynek

@zbynek zbynek commented May 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Describe the PR

Similar to PrematureDeclaration, but instead of checking whether the declaration can be moved after a return/throw, it checks whether it can be moved by a significant number of statements.

Violation of this rule suggests that a method is doing many unrelated things.

Relates to Google style guide paragraph 4.8.2.2 https://checkstyle.sourceforge.io/styleguides/google-java-style-20250426/javaguide.html#s4.8.2.2-variables-limited-scope

Equivalent to Checkstyle's VariableDeclarationUsageDistance

Related issues

  • Fix #

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pmd-actions-helper

pmd-actions-helper Bot commented May 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 833 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-05-09 15:00:43+00:00 for af55b15)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zbynek
zbynek force-pushed the distance branch from 553b436 to 70ebfbd Compare May 3, 2026 17:15
@zbynek
zbynek marked this pull request as ready for review May 3, 2026 18:34
@zbynek
zbynek force-pushed the distance branch from 70ebfbd to 0a29c04 Compare May 3, 2026 23:36
@zbynek
[java] new rule: VariableDeclarationDistance
f29f51f
@zbynek
zbynek force-pushed the distance branch from 0a29c04 to f29f51f Compare May 4, 2026 18:40
UncleOwen
UncleOwen reviewed May 4, 2026
View reviewed changes
Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen
UncleOwen reviewed May 4, 2026
View reviewed changes
Comment thread ...t/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/VariableDeclarationDistance.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@zbynek @UncleOwen
Set since version
3a31976
Co-authored-by: UncleOwen <[email protected]>
@zbynek zbynek changed the title [java] new rule: VariableDeclarationDistance [java] new rule: VariableDeclarationUsageDistance May 5, 2026
@zbynek
Add more tests, more descriptive name
23c724c
@zbynek
zbynek force-pushed the distance branch from e15292e to 23c724c Compare May 5, 2026 21:42
@zbynek zbynek added the a:new-rule Proposal to add a new built-in rule label May 6, 2026
@adangel adangel changed the title [java] new rule: VariableDeclarationUsageDistance [java] New rule: VariableDeclarationUsageDistance May 8, 2026
adangel
adangel requested changes May 8, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I'd appreciate a sentence for the release notes (something like https://github.com/pmd/pmd/blob/e6e7f330476570623147bfe8805fcf89bde29c7d/docs/pages/release_notes_old.md?plain=1#L24)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread .../java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableDeclarationUsageDistanceRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableDeclarationUsageDistanceRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableDeclarationUsageDistanceRule.java Outdated
}
}
if (!referencedEarly) {
asCtx(data).addViolation(node, id.getName());

@adangel adangel May 8, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we report in the message the max_distance value as well? For the rules that are related to metrics, we had a feature request to always report the actual value and the configured limit.

I think, we can't report the actual distance from declaration to first usage, can we? Would this be too expensive?

E.g. something like "Declaration of ''{0}'' is ''{1}'' statements away from its first usage. It can be moved closer. The max distance allowed is ''{2}''".

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread pmd-java/src/main/resources/category/java/codestyle.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableDeclarationUsageDistanceTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...ources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/VariableDeclarationUsageDistance.xml Outdated
public class Bar {
public int lengthSumOf(String[] strings) {
int sum = 0; // premature
final int offset = 42; // this is final: not a problem

@adangel adangel May 8, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the reasoning behind why final local vars are excluded? I think, we should put this into the rule description.

Atm I don't see a reason, why offset shouldn't be moved right before the return statement...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@zbynek zbynek May 9, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the main reason is that sometimes you want to make a "snapshot" of some object's state before that object is changed and use that snapshot after. Checkstyle rule has a setting for this, by default distances for final variables are ignored there (I didn't find any additional reasoning in sevntu-checkstyle/sevntu.checkstyle#21 where that rule was introduced).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@adangel adangel added the needs:user-input Maintainers are waiting for feedback from author label May 8, 2026
zbynek and others added 2 commits May 8, 2026 21:30
@zbynek @adangel
Specify version
e9153a5
Co-authored-by: Andreas Dangel <[email protected]>
@zbynek @adangel
Test class should not be public.
f79b426
Co-authored-by: Andreas Dangel <[email protected]>
@zbynek
zbynek force-pushed the distance branch from f79b426 to aa603dd Compare May 9, 2026 13:15
zbynek added 2 commits May 9, 2026 16:08
@zbynek
Compute distance
da6891b
@zbynek
Add more examples, better error message and description
087a2dd
@zbynek
zbynek force-pushed the distance branch from aa603dd to 087a2dd Compare May 9, 2026 14:10
@zbynek
Merge branch 'main' of https://github.com/pmd/pmd into distance
af55b15
@zbynek
zbynek requested a review from adangel May 13, 2026 22:50
@zbynek zbynek removed the needs:user-input Maintainers are waiting for feedback from author label May 13, 2026
@adangel adangel added this to the 7.25.0 milestone May 14, 2026
adangel
adangel approved these changes May 14, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I'll do the minor cleanups myself.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread docs/pages/release_notes.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/pages/release_notes.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableDeclarationUsageDistanceRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableDeclarationUsageDistanceTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

adangel added a commit that referenced this pull request May 14, 2026
@adangel
[doc] Update release notes (#6640)
7b4897f
adangel added a commit that referenced this pull request May 14, 2026
@adangel
[java] New rule: VariableDeclarationUsageDistance (#6640)
f38a53b
@adangel
adangel merged commit f0bbe27 into pmd:main May 14, 2026
11 checks passed

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@UncleOwen UncleOwen UncleOwen left review comments
@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

a:new-rule Proposal to add a new built-in rule

Projects

None yet

Milestone

7.25.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@zbynek @adangel @UncleOwen
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.