In an answer (by S.Lott) to a question about Python's try...else
statement:
Actually, even on an if-statement, the else: can be abused in truly terrible ways creating bugs that are very hard to find. [...]
Think twice about else:. It is generally a problem. Avoid it except in an if-statement and even then consider documenting the else- condition to make it explicit.
Is this a widely held opinion? Is else
considered harmful?
Of course you can write confusing code with it but that's true of any other language construct. Even Python's for...else
seems to me a very handy thing to have (less so for try...else
).
S.Lott has obviously seen some bad code out there. Haven't we all? I do not consider else harmful, though I've seen it used to write bad code. In those cases, all the surrounding code has been bad as well, so why blame poor else?
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