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

Forbid initializing 2 or more variables on one line #1901

Open
orsinium opened this issue Feb 23, 2021 · 4 comments
Open

Forbid initializing 2 or more variables on one line #1901

orsinium opened this issue Feb 23, 2021 · 4 comments
Labels
rule request Adding a new rule

Comments

@orsinium
Copy link
Collaborator

Rule request

Thesis

# bad
ab, cd = [], []

# good
ab = []
cd = []

Reasoning

Decrease unnecessary code line complexity.

@orsinium orsinium added the rule request Adding a new rule label Feb 23, 2021
@sobolevn
Copy link
Member

One more thing, unpacking is a good thing and should be allowed: a, b = some_tuple()
The thing about this example is that [], [] does not look like a real tuple. But, for example: x, y = (1, 2) looks pretty readable to me.

So, we need to find a balance between an obviously good idea - one assignment per line - and some real-world usages, when this rule is not applicable.

@orsinium
Copy link
Collaborator Author

One more thing, unpacking is a good thing and should be allowed: a, b = some_tuple()

Exactly 👍🏿

But, for example: x, y = (1, 2) looks pretty readable to me.

I don't think it will drop readability if it is on separate lines. Moreover, now I work on a codebase that has plenty of it and I always miss where the initialization of the second variable (y in your example) happens. So, I don't have cases in mind when one-line initialization is useful.

Ok, there is one case: swapping variables (x, y = y, x). However, I can't recall any case when I actually used it. Non-recursive GCD, maybe.

@sobolevn
Copy link
Member

swapping variables

Yes, sometimes I use this when doing non-recursive things like tree traversal.

@dreamflasher
Copy link
Contributor

I disagree that x, y = (1, 2) is particularly good style. I vouch for forbidding assigning more than 2 variables on one line. On the rare occasion that someone needs to implement GCD they can use noqa.

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

No branches or pull requests

3 participants