The Python Oracle

"else" considered harmful in Python?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Magic Ocean Looping

--

Chapters
00:00 &Quot;Else&Quot; Considered Harmful In Python?
00:48 Accepted Answer Score 32
01:08 Answer 2 Score 15
01:43 Answer 3 Score 7
02:16 Answer 4 Score 7
02:50 Answer 5 Score 6
03:14 Thank you

--

Full question
https://stackoverflow.com/questions/8657...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #ifstatement

#avk47



ACCEPTED ANSWER

Score 32


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?




ANSWER 2

Score 15


No it is not harmful, it is necessary.

There should always be a catch-all statement. All switches should have a default. All pattern matching in an ML language should have a default.

The argument that it is impossible to reason what is true after a series of if statements is a fact of life. The computer is the biggest finite state machine out there, and it is silly to enumerate every single possibility in every situation.

If you are really afraid that unknown errors go unnoticed in else statements, is it really that hard to raise an exception there?




ANSWER 3

Score 7


Saying that else is considered harmful is a bit like saying that variables or classes are harmful. Heck, it's even like saying that goto is harmful. Sure, things can be misused. But at some point, you just have to trust programmers to be adults and be smart enough not to.

What it comes down to is this: if you're willing to not use something because an answer on SO or a blog post or even a famous paper by Dijkstra told you not to, you need to consider if programming is the right profession for you.




ANSWER 4

Score 6


To me, the whole concept of certain popular language constructs being inherently bad is just plain wrong. Even goto has its place. I've seen very readable, maintainable code by the likes of Walter Bright and Linus Torvalds that uses it. It's much better to just teach programmers that readability counts and to use common sense than to arbitrarily declare certain constructs "harmful".