Repository navigation
Conversation
|
Compared to main: (comment created at 2026-08-11 20:20:42+00:00 for aeb27cc) |
adangel
left a comment
There was a problem hiding this comment.
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
|
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). |
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
sortByproperty).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?
./mvnw clean verifypasses (checked automatically by github actions)