4

These two patterns produce the same results. Does it matter which one is used? Why?

I prefer the second, it has less indentation and just looks cleaner to me, but I haven't seen it used much (in the places I've been). I don't want to settle on something and use it all over if it's ill advised for some reason.

IF...ELSE

if not packages:
    help('download')
else:        
    for p in packages:
        do_download(p)
        verify_md5(p)
        etc(p)

IF...RETURN; IMPLICIT ELSE

if not packages:
    help('download')
    return

for p in packages:            
    do_download(p)
    verify_md5(p)
    etc(p)
Jonas
  • 97,987
  • 90
  • 271
  • 355
matt wilkie
  • 14,126
  • 21
  • 68
  • 99
  • 2
    It doesn't much matter either way. It's up to you. – David Heffernan Nov 11 '14 at 08:26
  • 1
    there are some historical arguments for keeping to a single return statement from any function (it can make maintenance of long methods easier) but I think it's largely considered a preference thing these days – Matt Coubrough Nov 11 '14 at 08:31
  • There are tools for measuring code coverage of Python programs which work by instrumenting Python code. In order to measure what's commonly called "branch coverage", they'll have to count how often a conditional block was *not* executed (i.e.how often the `else` branch was hit). Having an explicit `else:` branch greatly simplifies instrumenting the source code for this purpose. – Frerich Raabe Nov 11 '14 at 08:46
  • I use the implicit else pattern when enforcing expectations that might not be particularly related to the code in the else block, but if and else where the else is tightly related to the if condition(s). – trognanders Nov 11 '14 at 08:47
  • there are 4 votes to close, but no explanation of what is unsatisfactory about the Q. Please elaborate that I might learn. Thanks. – matt wilkie Nov 11 '14 at 08:53
  • It's because it is a matter of personal style. We have contradictory opinions that are both valid. It's going to come down to pick one and stick with it :) – Leon Nov 11 '14 at 09:37
  • @DavidHeffernan, you should make that an answer, since it's pretty clear it's a matter of opinion and preference. – matt wilkie Nov 11 '14 at 20:06
  • 1
    @MattCoubrough: there's also historical arguments for the exact opposite :) and of course, there's this almost but not quite duplicate: http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement I always prefered the flattened style because you read sequentially, and the early return makes it clear you don't have to follow that branch of execution anymore. good. one thing less to keep in your head. that's good.... but others think differently, so unfortunately I have to concede, it's a matter of *style*... – Karoly Horvath Nov 29 '14 at 10:42
  • @mattwilkie: that's what "closed as primarily opinion-based" means. no need to post an answer. – Karoly Horvath Nov 29 '14 at 10:47

2 Answers2

7

From The Zen of Python:

Flat is better than nested.

So the second approach is more Pythonic.

I personally find flat code easier to read than nested code, and less error-prone as well (having e.g. an else statement not properly lined up can be hard to debug). Of course, these are subjective judgments.

twasbrillig
  • 12,313
  • 7
  • 37
  • 61
7

It's a style thing... But I always prefer using else. As you clearly indicate in your question caption not having an else makes it implicit and I strongly believe in explicit code that is easy to read and understand.

Also from The Zen of Python

Explicit is better than implicit.
Leon
  • 10,717
  • 4
  • 31
  • 54
  • 10
    Uh oh we have a Zen of Python contradiction! `Explicit is better than implicit` but `Flat is better than nested`... BOOOOOM! (I prefer explicit too) – Matt Coubrough Nov 11 '14 at 08:33
  • 2
    They way I see 'if and else' are very easy to understand, return is obfuscating and misleading. So I would stick with the first – f.rodrigues Nov 11 '14 at 08:59
  • 1
    I'm at the verge of doing a LewisBlack-style ranting... hbbbrrrhhhrr . How can a `return` be *obfuscating* or *misleading*? There must be something fundamentelly different about the way we think... – Karoly Horvath Nov 29 '14 at 10:37
  • 1
    `return` is also *explicit*. – Karoly Horvath Nov 29 '14 at 10:50