When negative evaluation in if clause will cause a return
call inside a function/method, what is more recommended in Python, to nest the if clauses or to utilise the inverse evaluation and call the function return? e.g.:
if required_condition_1:
if required_condition_2:
if required_condition 3:
pass
return 'error for condition 3'
return 'error for condition 2'
return 'error for condition 1'
Or:
if not required_condition_1:
# preparation...
return 'error for condition 1'
if not required_condition_2:
# preparation...
return 'error for condition 2'
if not required_condition_3:
# preparation...
return 'error for condition 3'
# if runtime reaches this point it means that it has passed all conditions
I have also thought about assertion:
try:
assert(required_condition_1)
try:
assert(required_condition_2)
# do tasks
except AssertionError:
# error for condition 2
except AssertionError:
# error for condition 1
Even though I think this last way is not pretty recommendable as in treating exceptions.
I know this might seem primarily opinion-based, but for me it is not since all languages have style guidelines that produce a more sustainable, scalable and readable environment depending on its characteristics and functionalities. I would like to know what if there is a recommended way of treating this matter inside methods and why.
Aucun commentaire:
Enregistrer un commentaire