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 — [apex] Cognitive complexity metrics by gwilymatgearset · Pull Request #2297 · pmd/pmd

Conversation

@gwilymatgearset

@gwilymatgearset gwilymatgearset commented Feb 18, 2020 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Collaborator

Before submitting a PR, please check that:

  • The PR is submitted against master. The PMD team will merge back to support branches as needed.
  • ./mvnw clean verify passes. This will build and test PMD, execute PMD and checkstyle rules. Check this for more info

PR Description:

Fixes #2162

Adds a cognitive complexity metric for apex allowing you to use either cyclomatic complexity or cognitive complexity (or both). This is work in progress. I still need to run it against some real apex code (not just my tests) to ensure that something reasonable happens here.

I've created the PR early to get some early feedback in case people feel like I'm going in the wrong direction.

Still TODO:

  • Whole class rule.
  • Fill in the TODO in the design.xml file.
  • Test against real apex classes.

Not done:

  • Mutual recursion. This is probably quite hard so unless it is really wanted, I won't do this yet.
  • Switch statements. It seems like these don't parse in the current version of PMD?
  • Boolean expressions. The way I've done it here doesn't 100% line up with the version in the paper - this uses the AST rather then as it's written so a && b || c && d gets complexity 2 rather than 3 because it parses as (a && b) || (c && d) which gives traversing order of ||, &&, && and therefore complexity 2.

Request for help:

  • There is a TODO in design.xml for what to fill in as the description and an example. I'm not quite sure what to put there.

Some questions:

  • I've put the visitor state in a State object because that's what the other visitors do, but it seems like I can make it part of the visitor itself which may be nicer (saves the State state = (State) data at the beginning of every line).

Sorry, something went wrong.

Uh oh!

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

rsoesemann reacted with hooray emoji rsoesemann reacted with heart emoji adangel reacted with rocket emoji
All reactions
  • 🎉 1 reaction
  • ❤️ 1 reaction
  • 🚀 1 reaction
gwilymatgearset added 25 commits February 18, 2020 10:47
@gwilymatgearset
Skeleton implementation of CognativeComplexityVisitor
9c09303
@gwilymatgearset
Register the cognitive complexity measure
69c239e
@gwilymatgearset
Add class level cognitive complexity measures
0967e43
@gwilymatgearset
Create design rule for cognitive complexity
a8234af
@gwilymatgearset
Add design rule for CognitiveComplexity
01da573
@gwilymatgearset
Add tests for CognitiveComplexity
8c357fb
@gwilymatgearset
Implement complexity for if
38ea3d1
@gwilymatgearset
Cognitive complexity for nested if statements
cea2081
@gwilymatgearset
Add test for non-nested if statements
9492ca0
@gwilymatgearset
Handle if-else correctly
1b93be5
@gwilymatgearset
Cognitive complexity for for loops
50d2bcb
@gwilymatgearset
When increasing nesting level we always get structure and nesting com…
7d3a5ef
…plexity
@gwilymatgearset
For each statements incur cognitive complexity
d9e7928
@gwilymatgearset
Continue statements incur complexity
7d3bb60
@gwilymatgearset
Break statements incur complexity
b2a42bf
@gwilymatgearset
While statements incur nesting complexity
ec87022
@gwilymatgearset
Catch blocks incur nesting complexity
559c1d9
@gwilymatgearset
Add do-while loop nesting complexity
5ea6953
@gwilymatgearset
Ternary operators increase nesting complexity
4b26d22
@gwilymatgearset
Any boolean expression causes increases in cognative complexity
a4db0ea
@gwilymatgearset
Handle boolean expressions on different statements
0231350
@gwilymatgearset
Increase cognitive complexity on recursion
9d0fb82
@gwilymatgearset
We don't actually support switch statements yet
acfbb13
@gwilymatgearset
Fix checkstyle errors
5d55b84
@gwilymatgearset
Use correct message for cognitive complexity rule
de87817
@gwilymatgearset gwilymatgearset changed the title [apex] Cognative complexity metrics (WIP) [apex] Cognitive complexity metrics (WIP) Feb 18, 2020
@ghost

ghost commented Feb 18, 2020

Copy link
Copy Markdown
1 Message
📖 No java rules are changed!

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

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

adangel
adangel reviewed Feb 18, 2020
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.

Cool, looks promising!

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pmd-apex/src/main/resources/category/apex/design.xml Resolved

Uh oh!

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

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

Uh oh!

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

gwilymatgearset added 2 commits February 19, 2020 10:24
@gwilymatgearset
Optimistically say that cognitive complexity is in the next release
a22d6f6
@gwilymatgearset
Increase default values for cognitive complexity based on authors opi…
413d64f
…nions
@rsoesemann

rsoesemann commented Feb 19, 2020

Copy link
Copy Markdown
Member

@gwilymatgearset just skimmed through the comments from @adangel. Did all your open points get resolved?

If you want some shitty classes that should have a high CC value let me know.

And, will you add the "Whole file rule" before you submit this PR?

All reactions

Sorry, something went wrong.

Uh oh!

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

@gwilymatgearset

gwilymatgearset commented Feb 19, 2020 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Collaborator Author

@rsoesemann

And, will you add the "Whole file rule" before you submit this PR?

It seems I did the whole class rule by accident without noticing I had done it :P.

I've written a draft for the TODO which I've sent over to some of the wordsmiths at Gearset (the marketing team) to see if they can come up with something better.

If you want some shitty classes that should have a high CC value let me know.

Yes please, the more the merrier :)

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

@gwilymatgearset
Fill in TODO in documentation for coginitive complexity
68e4f52
@rsoesemann

rsoesemann commented Feb 19, 2020

Copy link
Copy Markdown
Member

@gwilymatgearset for testing with real classes I recommend famous open source libs which often have quite convoluted classes that should have high values in CC:

  • https://github.com/apex-enterprise-patterns/fflib-apex-common/tree/master/fflib/src/classes
  • https://github.com/SalesforceLabs/Milestones-PM/tree/master/src/classes

compared to a greenfield TDD project which should have low values:

https://github.com/rsoesemann/sobject-work-queue

All reactions

Sorry, something went wrong.

Uh oh!

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

@gwilymatgearset
gwilymatgearset marked this pull request as ready for review February 20, 2020 17:05
@gwilymatgearset
gwilymatgearset requested review from adangel and rsoesemann February 20, 2020 17:06
@gwilymatgearset gwilymatgearset changed the title [apex] Cognitive complexity metrics (WIP) [apex] Cognitive complexity metrics Feb 20, 2020
rsoesemann
rsoesemann approved these changes Feb 20, 2020
View reviewed changes
@rsoesemann

rsoesemann commented Feb 21, 2020

Copy link
Copy Markdown
Member

@gwilymatgearset If the Java experts @adangel dont have anything to comment please feel free to merge.

All reactions

Sorry, something went wrong.

Uh oh!

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

@rsoesemann

rsoesemann commented Feb 21, 2020

Copy link
Copy Markdown
Member

@adangel I just reread this and saw that Apex switch statement still not seem to be supported. I am sure we added such a contribution long ago. Can you recall if that's true and already released?

All reactions

Sorry, something went wrong.

Uh oh!

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

@adangel adangel mentioned this pull request Feb 22, 2020
[apex] Switch statements are not parsed/supported #2306
Closed
adangel
adangel approved these changes Feb 22, 2020
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.

Thanks, looks good!

Sorry, something went wrong.

Uh oh!

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

rsoesemann reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@adangel adangel added this to the 6.22.0 milestone Feb 22, 2020
adangel added a commit that referenced this pull request Feb 22, 2020
@adangel
[doc] Update release notes, refs #2297, fixes #2162
ddd338d
@adangel
adangel merged commit 68e4f52 into pmd:master Feb 22, 2020
@gwilymatgearset
gwilymatgearset deleted the apex-cognative-complexity branch February 25, 2020 11:02
@gwilymatgearset gwilymatgearset mentioned this pull request Mar 2, 2020
[apex] Support switch statements correctly in Cognitive Complexity #2321
Merged
1 task done
@rsoesemann rsoesemann mentioned this pull request Mar 17, 2020
[apex] Invalid Apex in Cognitive Complexity tests #2358
Closed
@rsoesemann rsoesemann mentioned this pull request Sep 22, 2022
[apex] Type declaration/casts/constructors have correct capitalization #4112
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel approved these changes
@rsoesemann rsoesemann rsoesemann approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

6.22.0

Development

Successfully merging this pull request may close these issues.

[apex] Cognitive Complexity rule

Uh oh!

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

3 participants

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