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
Fixed #28858 -- Removed unnecessary "else" statements. #9498
Conversation
+1 I like this programming style and follow always. I am also interested in hearing out opinions. |
I also like this, it seems cleaner and reduces indentation which is a plus. Is there a flake8 plugin we can enable on the linter for this? |
+¾ - I largely agree with what Adam said in the ticket on this - where it doesn't impact readability, which is fine in the majority of cases.
I couldn't find one with these searches, but I only had a quick look:
I also think that removing redundant if condition0:
return 0
-elif condition1:
+if condition1:
return 1
-else:
- return 2
+return 2 If only Python had PERL's |
I think this is a disimprovement when Reason 1: code structure
The closest you can get in Python is if a(foo):
...
elif b(foo):
...
elif c(foo):
...
else:
... Compare this to: if a(foo):
...
if b(foo):
...
if c(foo):
...
... It dedents the Reason 2: Additional reasoning complexity
Reason 3: No advantages?Not sure here, but I don't see any advantages of using |
Yes, that was along the lines of my thoughts. Not using elif doesn't make it obvious that the conditions are mutually exclusive - you have to check each branch for a return statement. See https://stackoverflow.com/a/22783232 for an example. |
But it makes sense to raise exceptions with stacked if conditions.
I find the above pattern much better than
|
Just my personal opinion, I prefer the style proposed in this PR for the following cases:
I do not prefer this style for:
if some_condition():
return 42
else:
return None |
Is the point of #2 to have the return statements lined up? |
Right. Although, I'd probably write that example as |
I'm not going to spend more time on this. |
Opinions on this patch are welcome. I'm not sure if it's an improvement.
https://code.djangoproject.com/ticket/28858