Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it matter to use `if ... else` or `if ... return; {implicit else}`? [closed]

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)
like image 577
matt wilkie Avatar asked Dec 08 '22 05:12

matt wilkie


1 Answers

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.
like image 97
Leon Avatar answered Dec 21 '22 04:12

Leon