0

I've been trying to get better at styling my code and making sure it's as readable as possible.

Would it be considered bad practice to skip the else and just have a return ?

For example with this:

if x == 1:
    return True
return False

Instead of:

if x == 1:
    return True
else:
    return False
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • 1
    This is often a local decision within your work group. If you want to improve your style, I recommend that you read through the PEP-8 guidelines. – Prune Jun 30 '17 at 00:44
  • 1
    PEP-8 seems to be agnostic about this. There's one example with the first style, another with the second. – Barmar Jun 30 '17 at 00:45
  • 1
    This is very opinion based, but I would just say `return x == 1` so that it's obvious and readable that we're returning whether or not `x == 1` or not – Davy M Jun 30 '17 at 00:46
  • 1
    @DavyM I suspect this is a contrived example and is really part of multiple if/elif statements with a generalized fallthrough handler at the end. However, I've seen both ways in Pep8-linted code, so I would just be consistent within your team. – Alex Huszagh Jun 30 '17 at 00:48
  • 1
    @Prune There is a thread about this very topic [over at Software Engineering](https://softwareengineering.stackexchange.com/questions/157407/best-practice-on-if-return). Wish we had cross site dupe closing. – Christian Dean Jun 30 '17 at 01:30
  • @ChristianDean note that question uses C syntax, it could be that due to syntax / other language features, the answer could be different for different languages. – Baldrickk Jun 30 '17 at 01:46
  • Closely related: https://stackoverflow.com/questions/44608516/what-is-the-difference-between-else-return-true-and-just-return-true/44608693#44608693. I point out a negligible difference in the byte code generated for each of the two in an answer there. – chepner Jun 30 '17 at 03:06

2 Answers2

0

Both styles are valid, and the python style guide (PEP 8) does not specify a preference for either.

Whichever you decide to use, be consistent with it. This will result in code that is easier to read.

from PEP 8:

[…] code is read much more often than it is written. […] As PEP 20 says, "Readability counts".

A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important.

Under Programming recommendations:

Yes:

def foo(x):
    if x >= 0:
        return math.sqrt(x)
    else:
        return None

def bar(x):
    if x < 0:
        return None
    return math.sqrt(x)
Baldrickk
  • 4,291
  • 1
  • 15
  • 27
0

I always returns directly when it does not impact.

I did not find reference about that, but here is my vision

Because some time you could have to do stuff like

for spam in bacon:
    if spam == egg:
        return spam

But if you want to avoid returning within the if statement you will have to do:

return_value = None

from spam in bacon:
    if spam == egg:
        return_value = spam
        break

 return return_value

Which result to the exact same result & execution but only add 3 non relevant lines so I finally think when this happen I always use the same rule: return directly.

The thing is to not go too deep with for / if / return, but that is not really the question here.

Arount
  • 9,853
  • 1
  • 30
  • 43