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

EC69 : Irrelevant rule for Python code #276

Closed
Paul-Wassermann-CA opened this issue Feb 12, 2024 · 2 comments
Closed

EC69 : Irrelevant rule for Python code #276

Paul-Wassermann-CA opened this issue Feb 12, 2024 · 2 comments

Comments

@Paul-Wassermann-CA
Copy link

Paul-Wassermann-CA commented Feb 12, 2024

Rule EC69 is irrelevant for Python. The rule seems to imply that calling a function in a for statement results in calling this function multiple times, which is not true.

See the SonarQube screenshot below :

The two following syntaxes are strictly equivalent regarding the number of calls to the range builtin function :

for num in range(10):
    pass

iterable = range(10)
for num in iterable:
    pass

A simple way to prove see this is using a counter :

NUM_CALLS = 0


def func_to_iterable():
    global NUM_CALLS
    NUM_CALLS += 1
    return range(10)


def supposedly_ok_function():
    iterable = func_to_iterable()
    for num in iterable:
        pass


def supposedly_bad_function():
    for num in func_to_iterable():
        pass


if __name__ == '__main__':
    supposedly_bad_function()
    supposedly_ok_function()

    print(NUM_CALLS)

This prints 2 since the function is called only twice.

Therefore, I think the rule should be dropped since it's confusing and doesn't make much sense from an eco-design perspective, at least for Python, but it should be investigated for other languages too.

@dedece35
Copy link
Member

dedece35 commented Mar 9, 2024

Hello @Paul-Wassermann-CA
thank you for issue but could you create your issue on the right "ecoCode-python" repository please ? here https://github.com/green-code-initiative/ecoCode-python
and then close this issue.

@Paul-Wassermann-CA Paul-Wassermann-CA closed this as not planned Won't fix, can't repro, duplicate, stale Mar 11, 2024
@Paul-Wassermann-CA
Copy link
Author

Re-opened in green-code-initiative/creedengo-python#21

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

2 participants