A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/3570 below:

should provide an option to ignore multiple declarations in a for loop initializer · Issue #3570 · pmd/pmd · GitHub

Affects PMD Version: 6.39.0

Rule: OneDeclarationPerLine

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd-6.39.0/pmd_rules_apex_codestyle.html#onedeclarationperline

Description:

While I wholeheartedly agree with the concept of this rule, there is one scenario where (IMO) it doesn't quite apply simply because of language grammar limitations, specifically for classic for loop initializers where it's not possible to split into multiple distinct declarations. And there are very good use cases for having multiple declarations in in a for loop initializer, most commonly when part of the loop's condition logic can be expensive to calculate on each iteration and doesn't ever change, e.g.:

for (Integer i = 0, numIterations = calculateSomethingExpensive(); i < numIterations; i++) {
    ...
}

Yes, you could break that up to be:

Integer numIterations = calculateSomethingExpensive();
for (Integer i = 0; i < numIterations; i++) {
    ...
}

or even configure this rule so that the following isn't flagged:

for (Integer i = 0,
     numIterations = calculateSomethingExpensive();
    i < numIterations;
    i++) {
    ...
}

but those options are both forced by the fact that there's not currently a way to configure this rule to treat multiple declarations in a for initializer as an allowed exception.

Expected outcome:

I imagine for backward-compatibility, a new option would be added that defaults to the current behavior if unspecified, e.g.:

   <rule ref="category/apex/documentation.xml/OneDeclarationPerLine" message="...">
      <priority>3</priority>
      <properties>
         <property name="reportForLoopInitializer" value="false" />
      </properties>
   </rule>

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
CLI and direct API-based integration


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4