Skip to content

[java] Cognitive Complexity Metric - #3293

Merged
oowekyala merged 25 commits into
pmd:masterfrom
borovikovd:java-cognitive-complexity-rule
May 28, 2021
Merged

oowekyala merged 25 commits into
pmd:masterfrom
borovikovd:java-cognitive-complexity-rule

Conversation

@borovikovd

@borovikovd borovikovd commented May 23, 2021 •

Copy link
Copy Markdown
Contributor

Describe the PR

This PR implements a Cognitive Complexity metric for Java. The work is based on a similar metric for Apex language: https://pmd.github.io/pmd/pmd_rules_apex_design.html#cognitivecomplexity. Tests and implementation have been ported + some additional modification have been done:

  • Added handling for anonymous classes
  • Added handling for lambdas

Accordingly to the spec, we should increase nesting level for such structures but structural complexity should stay the same (see state.increaseNestingLevelWithoutStructuralComplexity method).

Notes

  • ASTIfStatement handling seems to be a bit convoluted but it's due to the way how java AST parser handles else if - it's both one of the blocks of the parent if and a new ASTIfStatement at the same time.

  • Also, a method call handling is a bit tricky since there is no separate java node for this case and I had to do a bunch of checks in ASTPrimaryPrefix callback.

  • I hope this PR can be integrated quickly since we really want to start using this metric in our Java projects on regular basis :)

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)

@borovikovd
borovikovd force-pushed the java-cognitive-complexity-rule branch 3 times, most recently from 5ab2f0f to 1e3c4a3 Compare May 23, 2021 18:59
@oowekyala
oowekyala self-requested a review May 23, 2021 18:59
@borovikovd
borovikovd force-pushed the java-cognitive-complexity-rule branch from 1e3c4a3 to 3de7481 Compare May 23, 2021 19:00

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

Hi, thanks for your contribution! The metric looks fine, however, it should also have tests and documentation.

For tests:

For documentation, please add a section to this file

@oowekyala oowekyala added the a:new-rule Proposal to add a new built-in rule label May 24, 2021
@ghost

ghost commented May 26, 2021 •

Copy link
Copy Markdown
1 Message
📖 This changeset changes 0 violations,
introduces 758 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report
✅ This changeset changes 0 violations,
introduces 765 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Full report

Generated by 🚫 Danger

@borovikovd

Copy link
Copy Markdown
Contributor Author

@oowekyala any more comments?:)

@oowekyala

Copy link
Copy Markdown
Member

@borovikovd No I think this looks fine. Thanks for your diligence on this!

If @adangel doesn't have any objections, I'll fix the outstanding conversation and merge this tomorrow, to include it in pmd 6.35.0.

@oowekyala oowekyala added this to the 6.35.0 milestone May 27, 2021

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

@borovikovd Thanks for working on this, looks great!

@oowekyala oowekyala self-assigned this May 28, 2021
oowekyala added a commit that referenced this pull request May 28, 2021
oowekyala added a commit that referenced this pull request May 28, 2021
@oowekyala
oowekyala merged commit f06fd8e into pmd:master May 28, 2021
oowekyala added a commit that referenced this pull request May 28, 2021
@oowekyala

Copy link
Copy Markdown
Member

merged, thanks again @borovikovd

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.

[java] Cognitive Complexity Rule for Java

3 participants