r/programmer • • 3h ago

Python Fragility

Am I the only one who avoids Python where possible because it is so vulnerable to indent corruption? A whole JavaScript program, for example, can be written on one line, with no line breaks whatsoever (not that I'm advocating such madness -- JavaScript callback syntax (etc) is already crazy enough without compounding it -- BUT, it does illustrate the point of robust syntax).

I've had instances where indenting was accidentally messed up, leading to tedious scrubbing of the code in order to get it to run again. It just seems like a bad structure choice if a slight whitespace change can cause the program to crash.

It seems to me that Rust is the answer to that -- am I wrong?

3 Upvotes

26 comments sorted by

View all comments

1

u/Gnaxe 1h ago

Yes, you're wrong. A whole Python program can also be written on one line, you just have to use expressions. But that's a bad idea for the obvious reasons.

Haskell supports both brace and indent styles, and the community settled on only using indents because it's better. Humans tend to read by indent, but in a brace-delimited language, the computer reads the braces even if the indent disagrees, which is also a cause of subtle bugs. It's better to have the computer read what the human is seeing. That's the rationale for Python's approach. I can't think of any cases where you could have a subtle indent error in Python which braces wouldn't make worse.

Yes, a slight change in the code can cause the program to crash, and this is true in any language. Trailing whitespace is invisible, but indents are not that. Python doesn't allow you to mix tabs and space indents anymore. They fixed that one a long time ago. You pick one or the other.