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

Complexity in list comprehensions #69

Open
Peque opened this issue Dec 3, 2018 · 2 comments
Open

Complexity in list comprehensions #69

Peque opened this issue Dec 3, 2018 · 2 comments

Comments

@Peque
Copy link

Peque commented Dec 3, 2018

Having asdf.py:

elements = [0, 1, 2, 3, 4]
ignore = [1, 2]


def loop():
    for x in elements:
        if x in ignore:
            continue
        print(x)


def comprehension():
    filtered = [x for x in elements if x not in ignore]
    for x in filtered:
        print(x)


loop()
print('----')
comprehension()

The McCabe complexity in loop() seems to be higher than in comprehension():

$ python -m mccabe asdf.py 
5:0: 'loop' 3
12:0: 'comprehension' 2

Is that really expected? Should not fors and ifs in list comprehensions count towards total complexity?

@ssbarnea
Copy link
Member

To be hones I seen a lot of abuse related to list comprehensions, with nesting and other stuff that reduced the readability and maintainability of the code considerably.

Shorter is no always better. I would be very happy if mccabe would not promote people to trick complexity-score by using list comprehensions.

@sigmavirus24
Copy link
Member

I would be very happy if mccabe would not promote people to trick complexity-score by using list comprehensions.

This assumes bad intent on the part of the author and maintainers which is unwelcome.

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