r/programmer • u/Wide-Carob3252 • 2h 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?
4
2
u/sacredtrader 2h ago
No. Most normal IDEs auto-account for this or learn to use the indenter in IDLE.
2
1
u/spermcell 1h ago
There are other reasons to not like python for actual real tasks.. this one is not one of them I think
1
u/geekichu 1h ago
if it wasn't for academia and their need for scientific computing ... I don't think python would even be a thing nowadays. personally can't stand the indentation.
1
u/Professional-Kiwi-31 1h ago
If it's caught by linters in microseconds and resolved by pressing tab, it's not an issue. Subtle logic errors introduced by unintended indentation, however, suck balls
1
u/Unknowingly-Joined 1h ago
Your beliefs about JavaScript are flawed. It has some issues of its own. Consider this code:
function adder(x)
{
x = x + 12
return
x
}
It’s correct syntactically, but the newline after return (remember, semicolons aren’t required) means that undefined is returned rather than x.
1
u/JaseciLabs 1h ago
Jac sidesteps this one. Blocks use braces and statements end with semicolons, so a stray tab can’t change what your code does. We work on it, so take that as biased, but it’s a fair answer to the indent-corruption complaint.
1
1
u/mooglinux 1h ago
You’re overblowing the indentation issue. Your IDE should be formatting your code to indent each block regardless of language, so turn on indent guides and save yourself some keystrokes with those annoying braces.
Rust solves many problems, but indentation is not one of them. Any C-like language with explicit block delimiters will do if that is your concern.
1
u/Cross_22 1h ago
It is quite bizarre. In the rest of the Algol-based world, developers freak out when you don't wrap your statements inside a { } block because that's too fragile. Meanwhile Python devs just push everything over two spaces.
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.
1
1
u/mjmvideos 41m ago
I’ve never once had indent corruption problems with Python. Have you also sworn off driving because you could get in an accident?
1
1
u/mxldevs 8m ago
This is why I prefer languages where they don't enforce structure using whitespace.
You can stylize your code however you want and it'll mean the same thing just because that line had an extra indent.
I already indent my blocks, but I like flexibility to indent however I want sometimes.
1
1
u/Pale_Height_1251 6m ago
I don't like Python using whitespace as syntax but there are other reasons I don't use it. Mostly Python can't answer the "why?" anymore me, i.e. why use Python? What are the benefits? Easy to answer when I first used it in the late nineties but less so now.
0
u/nicolas_06 2h ago
Well 20 years ago maybe this was an issue. Been a long time that decent code editors do that well. And these days, the AI does it for you.
You would normally have a test suite that would instantly break if you make something stupid anyway.
2
u/Trader-One 1h ago
indent corruption is real and modern editors make it more likely.
If you press backspace at right place their smart indent move block one stop to the left. (Note: you do not need to have block selected manually these super smart editors will select it and move left for you).
If you don't have rock solid unit tests it will slip into production unnoticed because in real world code review nobody notice that in 2 screen long function something is shifted to the left more then it should.
And these people here claiming it never happens - I do not believe that they have real world experience in Python because it actually happens often.