Am i must to use here else:
or i have opportunity to remove it and just type return True
def some_function(x):
if another_function(x) == -1:
return False
else:
return True
EDIT: I know how to make this code compact with just one return. The main question is about else:
.
Should i always use 'else:' even it is not necessary?
I myself believe that they are not necessary. Returning at the beginning of the function in case of edge cases is something that allows you to skip sometimes lots of indentations caused by else
s:
def some_function(x):
if edge_case_1:
return []
if edge_case_2:
return None
#a
#lot
#of
#code
#here
return result
looks better than
def some_function(x):
if edge_case_1:
return []
elif edge_case_2:
return None
else:
#a
#lot
#of
#code
#here
return result
right?
But it's not only about the looks:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With