Skip to content

[java] Fix #6882: New Rule: Enforce that local variabe declarations at the start of a block are sorted - #6924

Closed
Will-6543 wants to merge 7 commits into
pmd:mainfrom
Will-6543:issue-6882-2
Closed

Will-6543 wants to merge 7 commits into
pmd:mainfrom
Will-6543:issue-6882-2

Conversation

@Will-6543

@Will-6543 Will-6543 commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

Creates a new rule SortVariableDeclarationsAtStartOfBlock.
This rule enforces that variable declarations which come before anything else in the block should be sorted by either type or variable name (as selected with the sortBy property).
The sort can be case sensitive or not.
The rule can be configured to apply to only field declarations, only local variable declarations, both, (or neither).
Potential concern - currently nothing stops someone from disabling both local and field declarations and although their descriptions state mutual exclusivity this is not enforced.

Related issues

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)

@UncleOwen UncleOwen added the a:new-rule Proposal to add a new built-in rule label Aug 9, 2026
@pmd-actions-helper

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 17 violations,
introduces 38358 new violations, 0 new errors and 0 new configuration errors,
removes 880 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-08-11 20:20:42+00:00 for aeb27cc)

@adangel adangel changed the title [java] New Rule #6882: Enforce that local variabe declarations at the start of a block are sorted [java] Fix #6882: New Rule: Enforce that local variabe declarations at the start of a block are sorted Aug 15, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given, that this new rule not only checks variables, maybe it should be named "SortDeclarations"? Then it could be used to sort not only local variables and fields, but also methods, nested classes, anything (maybe later). The name of the rule should be open ("Don’t limit the rule name to strictly what the rule can do today", see https://docs.pmd-code.org/latest/pmd_devdocs_major_rule_guidelines.html)

Potential concern - currently nothing stops someone from disabling both local and field declarations and although their descriptions state mutual exclusivity this is not enforced.

You can implement the method "dysfunctionReason()" if there is a way, to disable the rule by properties...

If we support not only local variables and fields, but other declarations, we could also have a single property "declarationsToCheck" - which is a List of enum values and one can select any combinations of declaration types. There would still be the possibility to configure an empty list here - which would make the rule dysfunctional.

I'm not entirely sure about the if (!isAtStartOfBlock) condition - this sounds really like it belongs to the other rule. Would need to think more about whether this makes sense - e.g. you could enable this rule, but never declare your variables at the start of a block (or only one variable, which does not need to be sorted). So - it sounds like, this rule only would make sense together with "LocalVariableDeclarationsShouldBeAtStartOfBlock"...

Another note to think about: Other rules, that care about the position of declarations: PrematureDeclaration, VariableDeclarationUsageDistance, FieldDeclarationsShouldBeAtStartOfClass. Maybe also OneDeclarationPerLine

Checkstyle also has some related checks: InnerTypeLast, ConstructorsDeclarationGrouping, and DeclarationOrder

@adangel

adangel commented Oct 9, 2026

Copy link
Copy Markdown
Member

I'm going to close this PR, as this has nothing to do anymore with the original requirement in #6882. And the title is misleading.

If you want such a rule, start with specifying it in a new issue. On the apex side, there is already a very similar request: #5431

My general feeling with sorting stuff alphabetically is: It's not helpful when reading. Ideally you write your code on several abstraction layers. When you read a class from top to bottom, you might start with the high level method, which calls other (lower level) methods. Reading only the high-level method gives you a understanding, what is being done. When you want to know how it is being done, then you read on. In the file, after this high-level method, I'd expect that the lower-level methods are declared - in the same order as they are used in the higher level method. That allows you to read the whole class as a "story" without jumping or searching methods. That's the theory at least. And it won't always be possible (e.g. when the lower level methods are being used at several places).
When you use an IDE to read code, then the IDE can sort the declarations for you anyway and this rule is not needed.

@adangel adangel closed this Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants