I actually use Python and Flask for my devblog. I know that depending of the language, it is advisable to use a explicit else
when it is not obligatory, but I don't know how it's work in Python.
By example, I have a a function with a if
that return something if the statement is true. So, The else
is not necessary because with or without it, the execution continue normally.
def foo(bar):
if not isinstance(foo, list):
return "an error"
else: # not necessary
return "something"
So, I should use it like this, or like :
def foo(bar):
if not isinstance(foo, list):
return "an error"
return "something"
return retvalue
at the end. One slight advantage of not using anelse
orelif
besides it being an extra line to type, is when you have more than two, it's easier to delete or add one anywhere you want in the chain. Generally I don't believe there's much difference performance-wise.if... return...return
instead ofif...return...else...return
because I am lazy. :D