19

First, I couldn't find an answer to this in PEP 8. That doesn't mean it isn't in there. Feel free to point me at it.

Which style do you prefer?

The first one:

if spam:
    # Do stuff.
    return eggs
else:
    # Maybe do other stuff.
    return parrots

or the second one:

if spam:
    # Do stuff.
    return eggs
# Maybe do other stuff.
return parrots

closed as not constructive by corsiKa, BlueRaja - Danny Pflughoeft, Helen, Ben Voigt, marcog Feb 19 '11 at 19:02

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 13
    @closers: I understand the subjective, though I did try not to be and argumentative. But off topic? A coding style question? – nmichaels Feb 17 '11 at 20:48
  • If there was a "bikeshed problem" category, I'd vote to close your question as such. Lacking that, I felt compelled to answer it instead :) – Rafał Dowgird Feb 17 '11 at 21:02
  • I personally use the 2nd style if the else block is long. It keeps the code flatter and easier to read. – Ondra Žižka Sep 29 '12 at 10:21
12

It depends, if you would return parrots, when you do not return eggs, then the first one is more clear. But if you are trying to catch an error or something like that, the second one is better.

-8

I don't know python, but I do :

return spam if eggs else parrots

Edit : I didn't notice the Do stuff part. In that case, I would create another method or function (whatever it is called in python) for each branch.

  • 4
    It's spelled: return eggs if spam else parrots in Python. – S.Lott Feb 17 '11 at 20:03
  • 11
    You left out the #do stuffs. Plus, you're right about not knowing python. – Wooble Feb 17 '11 at 20:03
0

I think it depends on the "do stuff" part. Both are acceptable.

5

The second one. The same applies not only to return but also to break and continue statements. The else and the consequent indentation are eye-irritating and a waste of space.

3

Neither!

if spam:
    # Do Stuff
    result= eggs
else:
    # Maybe do other stuff
    result= parrots
return result
  • 13
    Symptom of excessive exposure to Pascal in infancy :-) – John Machin Feb 17 '11 at 20:15
  • 3
    @John Machin: Guilty as charged. And too much formal theory. It's easier to add logging and formal assertions before the return. – S.Lott Feb 17 '11 at 20:16
  • 8
    I've never liked One Entry, One Exit style of programming. If you are done with your method and have exactly what you need to return... RETURN! I feel the stack plumps up with wasted result variables. – Corey Ogburn Feb 17 '11 at 20:45
  • 1
    Some logging and assertions can be put in a decorator. Others not so easily, so good point. – Rafał Dowgird Feb 18 '11 at 8:17
5

It doesn't really matter. If you really need reasons for picking one, use the one that best conveys the reasons behind the code. The first one if both cases are somehow similar or equally important. The second one if you usually return parrots but eggs is a special case.

7

The second one! Less is more, and more is better.

8

The first one!

Explicit is better than implicit.

Not the answer you're looking for? Browse other questions tagged or ask your own question.