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

[New Rule] Don't check list length is > 0 before iterating over it #160

Open
rpdelaney opened this issue Nov 3, 2022 · 1 comment
Open
Assignees
Labels
enhancement New feature or request

Comments

@rpdelaney
Copy link

rpdelaney commented Nov 3, 2022

Explanation

Looking at one's own old, bad code can be a source of great cringe.

  • I remember prepending if incidents is not None because if incidents is None, then a TypeError is raised on the second condition. This line can be reduced to if incidents: without any change in behavior.

  • Checking the list length before iterating over it is a no-op.

Example

# Bad
incidents = get_ticket_ids(cell.value)
if incidents is not None and len(incidents) > 0:
    for incident in incidents:
        print(incident)

# Good
incidents = get_ticket_ids(cell.value)
if incidents:
    for incident in incidents:
        print(incident)

# Good
if incidents := get_ticket_ids(cell.value):
    for incident in incidents:
        print(incident)
@rpdelaney rpdelaney added the enhancement New feature or request label Nov 3, 2022
@rpdelaney
Copy link
Author

Prior art:

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

No branches or pull requests

2 participants