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

cognitive complexity calculation is incorrect #10

Open
tjorda01 opened this issue Jan 19, 2023 · 2 comments
Open

cognitive complexity calculation is incorrect #10

tjorda01 opened this issue Jan 19, 2023 · 2 comments

Comments

@tjorda01
Copy link

tjorda01 commented Jan 19, 2023

Computed Cognitive complexity is 12 (I believe correct) for this:

class GildedRose:
    def update_quality(self):
        for item in self.items:  # + 1
            if (
                item.name != "Aged Brie"
                and item.name != "Backstage passes to a TAFKAL80ETC concert"
             ):  # + 2
             if item.quality > 0:  # + 3
                 if item.name != "Sulfuras, Hand of Ragnaros":  # + 4
                     item.quality = item.quality - 1
            else:
                pass

However, 10 is incorrect (less than before???) for following code:
Note complexity should be higher than before since code is the same with an added 'if' condition after the 'else'):

class GildedRose:
    def update_quality(self):
        for item in self.items:  # + 1
            if (
                item.name != "Aged Brie"
                and item.name != "Backstage passes to a TAFKAL80ETC concert"
             ):  # + 2
             if item.quality > 0:  # + 3
                 if item.name != "Sulfuras, Hand of Ragnaros":  # + 4
                     item.quality = item.quality - 1
            else:
                #pass
                if item.quality < 50:  # + 3
                    item.quality = item.quality

@waweber
Copy link

waweber commented Jun 1, 2023

The algorithm in this plugin is implemented in https://github.com/Melevir/cognitive_complexity, you might get more attention opening an issue there

@Mockapapella
Copy link

When I run the functions through the complexity checker as a part of the tests, I get 16 and 15 respectively:

def test_else_complexity_ignored():
    assert get_code_snippet_compexity("""
    class GildedRose:
        def update_quality(self):                                                   # + 0 (nesting = 0)
            for item in self.items:                                                 # + 2 (1 for nesting, 1 for the loop) (nesting = 1)
                if (
                    item.name != "Aged Brie"
                    and item.name != "Backstage passes to a TAFKAL80ETC concert"
                ):                                                                  # + 4 (2 for nesting, 1 for the if, 1 for the bool operator sequence) (nesting = 2)
                    if item.quality > 0:                                            # + 4 (3 for the nesting, 1 for the if) (nesting = 3)
                        if item.name != "Sulfuras, Hand of Ragnaros":               # + 5 (4 for the nesting, 1 for the if) (nesting = 4)
                            item.quality = item.quality - 1                         # + 0 (nesting = 5)
                else:                                                               # + 3 (2 for the nesting, 1 for the else) (nesting = 2)
                    pass                                                            # + 0 (nesting = 3)
    """) == 16 # Total should be 18


def test_else_complexity_considered():
    assert get_code_snippet_compexity("""
    class GildedRose:
        def update_quality(self):                                                   # + 0 (nesting = 0)
            for item in self.items:                                                 # + 2 (1 for nesting, 1 for the loop) (nesting = 1)
                if (
                    item.name != "Aged Brie"
                    and item.name != "Backstage passes to a TAFKAL80ETC concert"
                ):                                                                  # + 4 (2 for nesting, 1 for the if, 1 for the bool operator sequence) (nesting = 2)
                    if item.quality > 0:                                            # + 4 (3 for the nesting, 1 for the if) (nesting = 3)
                        if item.name != "Sulfuras, Hand of Ragnaros":               # + 5 (4 for the nesting, 1 for the if) (nesting = 4)
                            item.quality = item.quality - 1                         # + 0 (nesting = 5)
                else:                                                               # + 3 (2 for the nesting, 1 for the else) (nesting = 2)
                    #pass
                    if item.quality < 50:                                           # + 4 (3 for the nesting, 1 for the if) (nesting = 3)
                        item.quality = item.quality
    """) == 15 # Total should be 22

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

3 participants