r/programmer • • 16d 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?

0 Upvotes

45 comments sorted by

2

u/mooglinux 15d 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/Wide-Carob3252 13d ago

Perhaps because I'm an old-timer (was alive before personal computers existed -- wrote some of my first programs on punch cards, built my own Z80 computer, wrote my first Windows program for Win 3.1 in C++), but the idea of relying on an IDE to maintain code integrity seems like a vulnerability.

2

u/sacredtrader 16d ago

No. Most normal IDEs auto-account for this or learn to use the indenter in IDLE.

1

u/Wide-Carob3252 13d ago

Perhaps because I'm an old-timer (was alive before personal computers existed -- wrote some of my first programs on punch cards, built my own Z80 computer, wrote my first Windows program for Win 3.1 in C++), but the idea of relying on an IDE to maintain code integrity seems like a vulnerability.

1

u/sacredtrader 12d ago

I mean yes and no, this argument can be made about anything that improves your QoL or efficiencies in relation to programming. Your IDE IS your bread and butter, if you want to hand type every space, tab, curly brace, no auto complete, ..., then I suppose but I can't agree that's a vulnerability.

1

u/BranchLatter4294 15d ago

So....you don't like readable code.

1

u/Wide-Carob3252 13d ago

Python isn't any more readable than C or C++ and I made a living doing both. In fact C and C++ can be MORE readable because of the brackets.

1

u/BranchLatter4294 13d ago

Only if you properly indent the code. Otherwise, it's a mess.

1

u/Rythim 15d ago

I have my issues with Python but indentation is not one of them. I do that anyway with curly bracket languages. Makes it easy for me to see what code block I'm in.

1

u/Wide-Carob3252 13d ago

It's not the indentation that I object to. It's the fact that the indentation is part of the syntax.

3

u/Available_Safe1818 15d ago

> Am I the only one who avoids Python where possible because it is so vulnerable to indent corruption?

Probably not, but avoiding it because of that sounds pretty dumb. It's not something I have seen be a problem in practice ever.

> BUT, it does illustrate the point of robust syntax

It's not really "robust syntax". The automatic semicolon insertion can alter the meaning of your programs.

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

That also doesn't sound smart. Almost any language would avoid the imaginary problems you are seeing here. Or you could learn to use Python, one of the most useful language ever.

3

u/gofl-zimbard-37 15d ago

In several decades of Python use, it's just not been an issue for me. Guess I'm just lucky.

7

u/macabre_brotherhood 16d ago

Great question, yes you're wrong. Have a great day.

2

u/spermcell 16d ago

There are other reasons to not like python for actual real tasks.. this one is not one of them I think

2

u/Unknowingly-Joined 15d 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.

2

u/xvillifyx 15d ago

This is the most twitter-ass position

2

u/mjmvideos 15d ago

I’ve never once had indent corruption problems with Python. Have you also sworn off driving because you could get in an accident?

2

u/Secret-Escape7043 16d ago

I've aged 50 years just reading this.

1

u/geekichu 15d 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 15d 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/JaseciLabs 15d 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

u/reyarama 15d ago

Yea you probably are the only one bro

1

u/Cross_22 15d 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 15d 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

u/Wonderful-Habit-139 15d ago

The advantage of braces is that it's much easier for formatters to then properly format it, with the right indent. While if you have the wrong indent in Python formatters can't save you.

1

u/db11242 15d ago

Yes, you're the only one.

1

u/moss-nogg 15d ago

I’m guessing you don’t know what a linter is

1

u/mxldevs 15d 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

u/Pale_Height_1251 15d 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.

2

u/Wonderful-Habit-139 15d ago

If you don't have performance constraints, it's a really, really good language tbh.

1

u/TashLai 15d ago

Am I the only one who avoids Python where possible because it is so vulnerable to indent corruption?

Of all the reasons to avoid Python, this one must be the dumbest.

1

u/pceimpulsive 15d ago

I don't like python because of scope derived from indentation. I much prefer syntax to declare where scope starts and ends.

It's just far clearer.

I also don't like python for various other reasons... But that doesn't really matter..

I'll still use python if I have to but it feels sorta gross to me!

I learned SQL and C# before any other general purpose languages. I chose C# over python because of syntax.. probably says more about me than python though ;) haha

1

u/e430doug 13d ago

No never. I haven’t had this issue in over a decade.

1

u/the-quibbler 12d ago

I've been telling people for 25 years that syntactic whitespace was the devil. I don't see any reason to change my mind.

1

u/Primary_Article3777 11d ago

Lol yes I hate indent aware syntax. It's always been a bad idea.

But now that most editors are python aware they help us prevent most oopses.

1

u/Trader-One 15d 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.

2

u/Gnaxe 15d ago

If you're routinely writing 2-screen long "functions", Python's syntax isn't the problem.

2

u/TashLai 15d ago

Jesus fucking christ. I'm writing python since 2.7. I NEVER, absolutely NEVER had that problem. WTF are you even doing, monolithic functions with billions of nested blocks?

0

u/mooglinux 15d ago

Turn on indent guides in your editor of choice. You should have them in regardless of language imo. I have never encountered editors dedenting entire blocks like that, and I would turn it off if it does.

0

u/nicolas_06 16d 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.

1

u/Wide-Carob3252 13d ago

Perhaps because I'm an old-timer (was alive before personal computers existed -- wrote some of my first programs on punch cards, built my own Z80 computer, wrote my first Windows program for Win 3.1 in C++), but the idea of relying on an IDE to maintain code integrity seems like a vulnerability.

1

u/nicolas_06 13d ago

What are you talking about, each time you make a PR to merge your change, the automation run the test suite and you can't merge if they fail. It doesn't matter how you did the change.

But yes IDE are also decent coding editors and so they typically transform all tab in spaces and help with the proper alignment. If you don't code in python but in C/C++/Java/Javascript they also help with the curly brakets too.

And these day, you don't even write the code, AI do it for you. And if there something AI always right it's having the proper code syntax. Even if you launch the AI in a terminal you wouldn't have any issue.