You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] VariableDeclarationUsageDistance: False positive with variables grouped at the top of a block #6846
VariableDeclarationUsageDistance is a good rule when the local variable declarations are mixed with executable code. It makes sense to move the declaration close to the usage. However...
The rule needs to ignore local variables declared at the top of a block (e.g., method, loop).
I find declaring all local variables at the top and alphabetizing them makes it very fast to figure out what type is the variable. With the variable declarations mixed in the code, it makes it hard to find the declarations. It also makes each line more complex and slower to read. But, this is for another future rule to enforce.
I declare variable at the beginning of the block. Others declare variables all over the place. So, we need the rule to flag their problems and ignore my code.
The rule is still useful if it targets declarations that are separated from first use by executable code. Declarations that are grouped at the top of a block are a different style, and in that style the distance is intentional rather than a readability smell.
Without this exception, the rule reports a false positive on code that is already easy for some teams to scan. That reduces trust in the rule and makes people less likely to keep it enabled. With the exception, the rule stays focused on the cases it can actually improve: declarations mixed into the body of the block and buried far from use.
So the benefit of the change is not that top-of-block declarations are inherently better. It is that the rule becomes more precise and more actionable by avoiding noise on a deliberate, common local style.
Picking up on @UncleOwen's question — I don't think the ask is "the rule shouldn't apply when declarations are at the top," but rather that grouped-at-top declarations are a distinct style with a different tradeoff than scattered declarations.
VariableDeclarationUsageDistance earns its value by catching declarations that drift away from their usage inside a block's body — that's a real readability problem regardless of style. But when a team deliberately groups all declarations at the top (e.g. alphabetized, Java-style "declare then execute"), the distance between declaration and first use isn't a smell — it's the intended shape of the code. Flagging it there doesn't point to anything actionable; the "fix" would just be to move the declaration next to its usage, which defeats the style the team chose in the first place.
That's why I opened #6860 as an opt-in property (ignoreVariablesAtTopOfBlock, default false) rather than a change to default behavior — teams that want the rule to also flag top-of-block groupings (like the existing test case) keep today's behavior unchanged. Teams using the grouped style can opt in to stop getting false positives without losing the rule's value for code where declarations really are scattered mid-block.
Happy to adjust the implementation once we agree on the direction — just wanted to make sure the design question itself has an answer before I keep iterating on #6860.
grouped-at-top declarations are a distinct style with a different tradeoff than scattered declarations.
Exactly.
But when a team deliberately groups all declarations at the top
... wouldn't this team benefit more from a new rule that actually enforces their coding style? I'd much rather see a rule VariableDeclarationNotAtTopOfBlock than allowing two different, incompatible code styles in one rule that is supposed to enforce one code style.
I think "variables at top of block" are the exact opposite of what this rule is trying to enforce.
I'm not sure if a the mixed style that allows either variables at the start of a block or close to usage is a common preference -- I'd assume people either want to declare as close to usage as possible, or at the top of a block.
int a1 = 1;
int a2 = 2;
int a3 = 3;
int a4 = 4;
int a5 = 5;
int a6 = 6;
int a7 = 7;
int a8 = 8;
System.out.println(a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8);
(since consecutive declartations do not count towards distance), but in PMD it's considered a violation.
Having an option to toggle this behavior would IMHO make sense and it would resolve the violation in the particular example that you shared.
Thanks for digging into this, @zbynek — I think you've identified the actual root cause. The false positive isn't really about "declared at the top of the block" specifically; it's that PMD's distance calculation counts other variable declarations in between, even when they're consecutive with no executable code interleaved. Your a1..a8 example makes that clear — it has nothing to do with top-of-block, it's the same underlying issue.
That's a more general (and probably better) fix than the ignoreVariablesAtTopOfBlock property I proposed in #6860 — instead of special-casing "top of block," we could simply stop counting other variable declaration statements toward the distance, similar to how Checkstyle's equivalent check handles it. That would resolve both the original example in this issue and the a1..a8 case, without introducing a style-specific opt-in. @UncleOwen's VariableDeclarationNotAtTopOfBlock idea sounds like a good, separate rule for teams that want to enforce a top-of-block style — but I'd treat that as orthogonal to this issue, since it's about enforcing a style rather than fixing a false positive in the existing distance calculation.
If maintainers agree, I'm happy to rework #6860 to change the distance calculation instead of adding the opt-in property. Let me know which direction you'd prefer before I update the PR.
VariableDeclarationNotAtTopOfBlock idea sounds like a good, separate rule for teams that want to enforce a top-of-block style — but I'd treat that as orthogonal to this issue, since it's about enforcing a style rather than fixing a false positive in the existing distance calculation.
As for VariableDeclarationNotAtTopOfBlock, I will create a separate issue.
changed the title [-][java] VariableDeclarationUsageDistance Shall Ignore Declarations at the Top of the Block[/-][+][java] VariableDeclarationUsageDistance: False positive with variables grouped at the top of a block[/+]on Aug 11, 2026
Activity
[java] Fix pmd#6846: VariableDeclarationUsageDistance: false positive…
UncleOwen commentedon Jul 10, 2026
No, that doesn't make sense. If you always declare variables at the beginning of a block, why are you even enabling this rule?
What am I missing here?
numeralnathan commentedon Jul 10, 2026
I declare variable at the beginning of the block. Others declare variables all over the place. So, we need the rule to flag their problems and ignore my code.
UncleOwen commentedon Jul 11, 2026
Could you please expand on why we need to do this? Why is the rule
VariableDeclarationUsageDistancebetter with this proposed change than without it?numeralnathan commentedon Jul 11, 2026
The rule is still useful if it targets declarations that are separated from first use by executable code. Declarations that are grouped at the top of a block are a different style, and in that style the distance is intentional rather than a readability smell.
Without this exception, the rule reports a false positive on code that is already easy for some teams to scan. That reduces trust in the rule and makes people less likely to keep it enabled. With the exception, the rule stays focused on the cases it can actually improve: declarations mixed into the body of the block and buried far from use.
So the benefit of the change is not that top-of-block declarations are inherently better. It is that the rule becomes more precise and more actionable by avoiding noise on a deliberate, common local style.
onetuks commentedon Jul 13, 2026
Picking up on @UncleOwen's question — I don't think the ask is "the rule shouldn't apply when declarations are at the top," but rather that grouped-at-top declarations are a distinct style with a different tradeoff than scattered declarations.
VariableDeclarationUsageDistance earns its value by catching declarations that drift away from their usage inside a block's body — that's a real readability problem regardless of style. But when a team deliberately groups all declarations at the top (e.g. alphabetized, Java-style "declare then execute"), the distance between declaration and first use isn't a smell — it's the intended shape of the code. Flagging it there doesn't point to anything actionable; the "fix" would just be to move the declaration next to its usage, which defeats the style the team chose in the first place.
That's why I opened #6860 as an opt-in property (ignoreVariablesAtTopOfBlock, default false) rather than a change to default behavior — teams that want the rule to also flag top-of-block groupings (like the existing test case) keep today's behavior unchanged. Teams using the grouped style can opt in to stop getting false positives without losing the rule's value for code where declarations really are scattered mid-block.
Happy to adjust the implementation once we agree on the direction — just wanted to make sure the design question itself has an answer before I keep iterating on #6860.
UncleOwen commentedon Jul 13, 2026
Exactly.
... wouldn't this team benefit more from a new rule that actually enforces their coding style? I'd much rather see a rule
VariableDeclarationNotAtTopOfBlockthan allowing two different, incompatible code styles in one rule that is supposed to enforce one code style.numeralnathan commentedon Jul 13, 2026
VariableDeclarationNotAtTopOfBlock? Yes! That would be a very good rule to have.For teams with mixed code styles, the
ignoreVariablesAtTopOfBlockparameter inVariableDeclarationUsageDistancewould be very helpful too.zbynek commentedon Jul 14, 2026
I think "variables at top of block" are the exact opposite of what this rule is trying to enforce.
I'm not sure if a the mixed style that allows either variables at the start of a block or close to usage is a common preference -- I'd assume people either want to declare as close to usage as possible, or at the top of a block.
That being said, the rule is similar to Checkstyle's https://checkstyle.sourceforge.io/checks/coding/variabledeclarationusagedistance.html , but counts variable declaration blocks differently. In Checkstyle this is OK
(since consecutive declartations do not count towards distance), but in PMD it's considered a violation.
Having an option to toggle this behavior would IMHO make sense and it would resolve the violation in the particular example that you shared.
onetuks commentedon Jul 15, 2026
Thanks for digging into this, @zbynek — I think you've identified the actual root cause. The false positive isn't really about "declared at the top of the block" specifically; it's that PMD's distance calculation counts other variable declarations in between, even when they're consecutive with no executable code interleaved. Your a1..a8 example makes that clear — it has nothing to do with top-of-block, it's the same underlying issue.
That's a more general (and probably better) fix than the ignoreVariablesAtTopOfBlock property I proposed in #6860 — instead of special-casing "top of block," we could simply stop counting other variable declaration statements toward the distance, similar to how Checkstyle's equivalent check handles it. That would resolve both the original example in this issue and the a1..a8 case, without introducing a style-specific opt-in.
@UncleOwen's VariableDeclarationNotAtTopOfBlock idea sounds like a good, separate rule for teams that want to enforce a top-of-block style — but I'd treat that as orthogonal to this issue, since it's about enforcing a style rather than fixing a false positive in the existing distance calculation.
If maintainers agree, I'm happy to rework #6860 to change the distance calculation instead of adding the opt-in property. Let me know which direction you'd prefer before I update the PR.
UncleOwen commentedon Jul 15, 2026
Yes, please do that. That's a small change in spirit with the existing rule.
numeralnathan commentedon Jul 15, 2026
As for
VariableDeclarationNotAtTopOfBlock, I will create a separate issue.[java] Refactor pmd#6846: doesn't count when local variables declarat…
[java] Test pmd#6846: fix test file to verify local variable declarat…
[java] Fix pmd#6846: VariableDeclarationUsageDistance: false positive…
[-][java] VariableDeclarationUsageDistance Shall Ignore Declarations at the Top of the Block[/-][+][java] VariableDeclarationUsageDistance: False positive with variables grouped at the top of a block[/+]