Skip to content

[java] VariableDeclarationUsageDistance: False positive with variables grouped at the top of a block #6846

Closed

Description

@numeralnathan

Affects PMD Version: 7.26.0

Rule:

VariableDeclarationUsageDistance

Description:

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

Code Sample demonstrating the issue:

MyReport firstReport;
MyTree   firstTree;                  // VariableDeclarationUsageDistance
MyFrame  frame;
MyReport secondReport;
MyFrame  secondTree;

firstReport  = new MyReport();
secondReport = new MyReport();
frame        = createFrame(0, "current", 1, 2);

MyReport.addFrame(frame);

firstTree  = firstReport.getTree();

Expected outcome:

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.

Running PMD through: Eclipse Plugin

Activity

  1. added
    a:false-positive
    on Jul 7, 2026
  2. added a commit that references this issue on Jul 10, 2026
    a939a03
  3. UncleOwen commented on Jul 10, 2026

    @UncleOwen
    Member

    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?

  4. numeralnathan commented on Jul 10, 2026

    @numeralnathan
    Author

    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.

  5. UncleOwen commented on Jul 11, 2026

    @UncleOwen
    Member

    Could you please expand on why we need to do this? Why is the rule VariableDeclarationUsageDistance better with this proposed change than without it?

  6. numeralnathan commented on Jul 11, 2026

    @numeralnathan
    Author

    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.

  7. onetuks commented on Jul 13, 2026

    @onetuks
    Contributor

    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.

  8. UncleOwen commented on Jul 13, 2026

    @UncleOwen
    Member

    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.

  9. numeralnathan commented on Jul 13, 2026

    @numeralnathan
    Author

    VariableDeclarationNotAtTopOfBlock? Yes! That would be a very good rule to have.

    For teams with mixed code styles, the ignoreVariablesAtTopOfBlock parameter in VariableDeclarationUsageDistance would be very helpful too.

  10. zbynek commented on Jul 14, 2026

    @zbynek
    Contributor

    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

    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.

  11. onetuks commented on Jul 15, 2026

    @onetuks
    Contributor

    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.

  12. UncleOwen commented on Jul 15, 2026

    @UncleOwen
    Member

    If maintainers agree, I'm happy to rework #6860 to change the distance calculation instead of adding the opt-in property.

    Yes, please do that. That's a small change in spirit with the existing rule.

  13. numeralnathan commented on Jul 15, 2026

    @numeralnathan
    Author

    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.

  14. added 2 commits that reference this issue on Jul 20, 2026
  15. added this to the 7.27.0 milestone on Jul 25, 2026
  16. added a commit that references this issue on Jul 25, 2026
    1ac594b
  17. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions