GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Cognitive Complexity Metric by borovikovd · Pull Request #3293 · pmd/pmd

Conversation

@borovikovd

@borovikovd borovikovd commented May 23, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

  • Fixes [java] Cognitive Complexity Rule for Java #2329

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)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@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
Java Cognitive Complexity rule
3de7481
@borovikovd
borovikovd force-pushed the java-cognitive-complexity-rule branch from 1e3c4a3 to 3de7481 Compare May 23, 2021 19:00
oowekyala
oowekyala reviewed May 24, 2021
View reviewed changes

@oowekyala oowekyala left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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:

  • create a test rule on the model of eg LocTestRule
  • register it in the test ruleset and the test class
  • Create a test file in this directory, eg you can start with the test file for your rule

For documentation, please add a section to this file

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

borovikovd reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread ...java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/CognitiveComplexityBaseVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/CognitiveComplexityBaseVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/api/JavaOperationMetricKey.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala oowekyala added the a:new-rule Proposal to add a new built-in rule label May 24, 2021
oowekyala
oowekyala reviewed May 24, 2021
View reviewed changes
Comment thread ...java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/CognitiveComplexityBaseVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@borovikovd
Refactor classes accordingly to review comments
ad9a422
@oowekyala oowekyala mentioned this pull request May 24, 2021
[java] Deprecate AvoidDeeplyNestedIfStmts #3295
Open
borovikovd added 7 commits May 24, 2021 20:30
@borovikovd
Re-arrange tests
c29ff91
@borovikovd
Fix test
ec22c2f
@borovikovd
Add docs
f509276
@borovikovd
Numerous bugfixes
7ab0bbe
@borovikovd
Refactor code to make it closet to the spec
bbf8463
@borovikovd
Remove forgotten file
2a4b535
@borovikovd
Fix violation
ec60e3e
oowekyala
oowekyala reviewed May 26, 2021
View reviewed changes
Comment thread pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/design/CognitiveComplexityTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

borovikovd and others added 6 commits May 26, 2021 11:41
@borovikovd
Reformat file
992ef3b
@borovikovd
Revert "Remove forgotten file"
cc512ac
This reverts commit 2a4b535.
@borovikovd
Add rule test
d1a0a2f
@borovikovd
Rename flag to make it more consistent
31e8e58
@borovikovd
Simplify condition
e0e4e7c
@oowekyala
Simplify handling of if statements
eadbadd
@ghost

ghost commented May 26, 2021 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

oowekyala
oowekyala reviewed May 26, 2021
View reviewed changes
Comment thread ...ain/java/net/sourceforge/pmd/lang/java/metrics/impl/internal/CognitiveComplexityVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@borovikovd
Use qualified names for methods
0e49ec8
oowekyala
oowekyala reviewed May 26, 2021
View reviewed changes
Comment thread ...ain/java/net/sourceforge/pmd/lang/java/metrics/impl/internal/CognitiveComplexityVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...ain/java/net/sourceforge/pmd/lang/java/metrics/impl/internal/CognitiveComplexityVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@borovikovd
Store the node directly instead of qualified name
68b5cfb
borovikovd added 4 commits May 26, 2021 19:00
@borovikovd
Handle nested method for recursion case
4967e95
@borovikovd
Fix imports
0eaf9b7
@borovikovd
Add newlines
d2af01f
@borovikovd
Refactor return result
25b3504
@borovikovd

borovikovd commented May 27, 2021

Copy link
Copy Markdown
Contributor Author

@oowekyala any more comments?:)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala

oowekyala commented May 27, 2021

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.

borovikovd reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@oowekyala oowekyala added this to the 6.35.0 milestone May 27, 2021
adangel
adangel approved these changes May 27, 2021
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

borovikovd reacted with laugh emoji
All reactions
  • 😄 1 reaction
Comment thread docs/pages/pmd/languages/java_metrics_index.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread docs/pages/pmd/languages/java_metrics_index.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-java/src/main/resources/category/java/design.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

borovikovd and others added 4 commits May 27, 2021 22:36
@borovikovd @adangel
Update docs/pages/pmd/languages/java_metrics_index.md
5028eaa
Co-authored-by: Andreas Dangel <[email protected]>
@borovikovd @adangel
Update docs/pages/pmd/languages/java_metrics_index.md
aa3f64d
Co-authored-by: Andreas Dangel <[email protected]>
@borovikovd @adangel
Update pmd-java/src/main/resources/category/java/design.xml
fa65ac9
Co-authored-by: Andreas Dangel <[email protected]>
@borovikovd
Fixing the name of the rule
b39ec05
@oowekyala oowekyala self-assigned this May 28, 2021
oowekyala added a commit that referenced this pull request May 28, 2021
@oowekyala
Update release notes, refs #3293
3b81ff5
oowekyala added a commit that referenced this pull request May 28, 2021
@oowekyala
Fix cdata in rule description
6e4a0ef
refs #3293
@oowekyala
oowekyala merged commit f06fd8e into pmd:master May 28, 2021
oowekyala added a commit that referenced this pull request May 28, 2021
@oowekyala
Update contribution list #3293
62c70ae
@oowekyala

oowekyala commented May 28, 2021

Copy link
Copy Markdown
Member

merged, thanks again @borovikovd

borovikovd reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@oowekyala oowekyala oowekyala left review comments
@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@oowekyala oowekyala

Labels

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

Projects

None yet

Milestone

6.35.0

Development

Successfully merging this pull request may close these issues.

[java] Cognitive Complexity Rule for Java

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@borovikovd @oowekyala @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.