Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Add Cognitive Complexity as new option #112

Open
mikeweltevrede opened this issue Jun 25, 2024 · 0 comments
Open

[Proposal] Add Cognitive Complexity as new option #112

mikeweltevrede opened this issue Jun 25, 2024 · 0 comments

Comments

@mikeweltevrede
Copy link

Introduction

I would like to request to add "Cognitive Complexity" to this library as an option to select in the CLI and through hooks. This is a common quality check in the SonarQube application, used by many for static code checking.

Please see this document written by Sonar: https://www.sonarsource.com/docs/CognitiveComplexity.pdf. Importantly: "Unlike Cyclomatic Complexity, Cognitive Complexity adds a fundamental increment for each method in a recursion cycle, whether direct or indirect".

I am curious as to the opinions of others :)

Example 1

I like the example in the PDF, where the Cyclomatic Complexity is the same, but the Cognitive Complexity is vastly different:
image

image

Example 2

Consider the following code:

for _ in [i for i in range(5)]:
    for _ in [i for i in range(5)]:
        for _ in [i for i in range(5)]:
            for j in [i for i in range(5)]:
                if j == 2:
                    ...
                else:
                    if j > 3:
                        ...

The Cyclomatic Complexity, also the one currently reported bymccabe, is 7:
image

The Cognitive Complexity, as reported by Sonar, is 22 due to all of the nesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant