mercredi 28 janvier 2015

Empty except block vs. huge except block


Which of these is easier to read?


An empty except block:



try:
foo = do_foo()
except FooError:
pass # see below
else:
return do_bar(foo)
# Recover from the FooError (long, complicated)


Or this:



try:
foo = do_foo()
except FooError:
# Recover from the FooError (long, complicated)
else:
return do_bar(foo)


Note that the recovery is significantly more complex than the one-line else block. It may itself involve indented blocks or other complications. The first option has the advantage of eliminating a level of indentation. But the second option is probably closer to what the average developer expects to see. I'm uncertain which of these concerns is more important for readability.





Aucun commentaire:

Enregistrer un commentaire