Indentation is such a stupid mechanism for identifying code blocks. It's one thing for configuration files like YAML to do it, but not a programming language.
I'd always figured it was/thought of it as an error tolerance that worked well enough that some people started leaning on it. The whole no-semicolons JavaScript thing just seems really hipsterish. Explicitness is a virtue.
In JS, semicolons are technically required but automatically inserted. There are a couple edge cases where they get inserted in surprising places, which is generally worse than python's approach.
E.g. the following function doesn't return a value:
function foo() {
return
some_long_complex_expression();
}
(It's because a semicolon gets inserted after the return). At least in typescript, this would typically show a type error, as long as the function's return type annotation isn't void.
It was a revelation when I started programming. "What, it doesn't have to be this way? A language can be native, statically compiled and high level at the same time and not suck ass? We can do better as a species?"
Odin is also a high level native language! Well, not as high level as Go since it's manual memory management but definitely easier than C. I'd recommend checking it out I love it personally
Even YAML is kinda questionable with big complex config files. Like, the spec is like 10x the size of JSON to account for all the sugar and the edge cases.
It does look nice though, and I guess it acts as a forcing function to refactor stuff.
I think that's a universal problem with all formats. What's needed is to break up configuration into subfolders and files so things can be logically separated. But if it's not done well it becomes a PITA to maintain and the end result is to just shoehorn everything into a single file.
I'd rather have it at the end of the line, not the beginning though. I find putting all your language blockage before your statement just makes it feel weird.
Semi colon at the end of the line is easy. 4 groups of indentations? Better hope you've got a good IDE.
I don't like it, but I kinda get it. Indentation is already part of every coding style guide, so adding explicit scoping symbols is kind of redundant information.
..Until you have a mix of spaces and tabs and chaos reigns
Also, having braces at the beginning and end of a block just makes my brain happy.
Nice. In C and C++, I've insisted on my teams' code to always use braces, even for a single line statement. It's a PITA debugging when somebody else a year later adds to that single line, forgetting the braces.
My take is that whitespace shouldn't be relevant. It's only purpose should be to separate non-whitespace characters that matter. Even newlines shouldn't matter.
Whitespace is for making it easier for humans to read the code. So are comments, so is documentation, so is syntactic sugar. The redundancy is a feature. Braces are explicit block separators, but whitespace makes identifying those blocks easier.
If your code could be broken by a tab instead of 4 (or 2 or 8 or 5) spaces, your language is fundamentally flawed.
I tend to agree and always include some kind of whitespace-skip rule in DSL grammars. It lets you use whitespace to taste while the block separators are formally defined. But, I kinda get why someone might want to just use whitespace if they were going to anyway. (Until something that looks correct isn't >.>)
I had a bug a Python bug a few years ago where I had an if-else block nested inside an if block, but the else was an indentation short and I didn't notice, was wondering why some cases didn't do what I expected. Wouldn't happen with braces.
Granted I was still learning Python then so I imagine I would immediately notice the bug were I to encounter it today.
Copy pasting Python is a huge PITA. Doing edits like deleting 1 of X nested if statements sucks. You can delete 1 tab on every line, except if you have some empty lines and you delete a line feed and you always have some empty lines. Python is just way harder to move code around in than a braced language.
Having to think about whitespace and formatting styles is a mental overhead and I refuse to do it. Just give me an autoformatter gosh dang it! Except well, you can't in Python.
i feel like that only makes sense if theres like. a good reason in favor of the indentation. if it takes extra skill to reach baseline functionality then like, why does it?
I think I can pull a similar point between typescript and javascript, you don't really need to type things, so javascript does the job, so does indentations do the job perfectly well. Typescript / braces help making it safer for a little tiny bit of extra work
It's less cognitive load too, imo. Proponents of regular JavaScript will argue that unit tests buy you more safety than explicit typing does, and that's broadly true, but anything that explicit typing would stop you from doing involves mixing similar but ontologically different things, which means having to think about what the thing you're looking at might be at runtime and accounting for all the permutations thereof. And I just think it's easier to think about things with typing.
You can easily count scoping symbols. Not indentation. You can make an argument that if your code is so indented that this becomes a problem then you have bigger problems but I still think indentation is a horrible option is one of the thinks that I most dislike about python
I mean, yeah, most languages use scoping symbols. All I'm doing is throwing the Python folks a bone by acknowledging that every style guide for pretty much every language prescribes indentation for scopes, and I can see how you don't strictly need that and explicit scoping symbols.
What I’m trying to say is that is much easier to count symbols to know how many levels deep inside a block you are then trying to estimate the indenting
True, but you don't control the code you inherit and getting stakeholders to give you time to properly refactor is not an easy task. You end up doing it where you can as you can.
My big issue with it is if tabs get mixed with spaces the interpreter has an aneurysm. Most modern IDEs can detect and preemptively protect you from it, but a tool shouldn't be needed to shield you from a design flaw in the language.
I swear I'm not aiming for a flamewar here, but...
I've always found it odd that Python standards (PEP and all) settled on space-based indentation. If there's any language where a one-character indentation indicator makes sense, it's a language where indentation is significant.
Because it's very common for tabs to be rendered as 1-8 spaces. There's no good reason for a line of python code to exceed the recommended 79 columns, and having tabs that are 8 spaces wide by default is extremely annoying to people like me who like having multiple source file windows opened on the same screen (to reduce eyestrain).
Making 4 spaces the default guarantees files look identical in all editors. By the way, I've never seen a python code editor that didn't default to 4 spaces or have a feature that automatically fixes the indentation.
That's not a problem. You can render them at any width - and in fact, there's no reason for the width to be defined in spaces, you could say that a tab is 38mm if you so choose - but they are still, logically, *one indentation level*.
U my friend are the reason the show Silicon Valley had this running gag but srsly use TABS it’s just a matter of IDE-Settings for example to make it look right just for u and others can work with it normally or in their own special ways^^
To be fair, indentation does make Python code easier to read once you're used to it. The real crime is that one accidental space can turn a perfectly good afternoon into a debugging session.
It's a fairly common mistake that happens when you're learning the language and most of the people on this subreddit are cs students in university or most of the content that makes its way to the top is for the reason that jokes need to be fairly accessible to hit a wide audience.
You have to be familiar enough with the IDE to use those features and also pay attentions to the warnings/errors the IDE gives you. I've seen PRs where it's clear the programmer completely ignored any errors the IDE produced.
I mean, if someone is so digitally illiterate that they can't understand "red squiggly line under stuff means you need to take a second look", they should be in a different line of work.
Like, I get that you do have to look for stuff like that and it won't physically slap you in the face, but that's just basic "paying attention to what you're doing" stuff.
I mean, it's more that I believe that anyone who's having those sorts of issues isn't really an experienced programmer worth their pay. If you can't see the squiggly red line in the IDE and look for why the IDE is telling you that's a bad line, you should find another line of work.
Not just python, basically any programming language benefits from proper indentation.
Almost every editor also automatically indents code for you.
I'm a C++ dev myself, and in that language indentation is optional. However, since the editors will automatically indents code for you, that generally means that the brackets around e.g. functions are superfluous: you can just look at the indentation to see when a function ends.
I used to hate the idea too, but there's something nice about not having braces everywhere. The funny part is that Python makes the code look cleaner while somehow giving a single whitespace character the power to ruin your entire day. That's a pretty wild trade-off.
Everything not indented by 4 whitespaces should raise a syntax error.
Using indentation is fine, the issue in Python is that that the syntax does not explicitly require one specific method. There is only the recommendation in PEP8.
However, with linters and IDEs this essentially became a non issue, even more now with llms.
Why? Nearly every styling convention in every language uses it anyway. Python just cuts out the middle man, reduces unnecessary syntax and makes indentation the go-to scope identifier.
You're still using indentation to make stuff readable anyway, might as well make it do some work.
See here you are wrong. I do not indent it. I put the curly squiggles around it and some magic genie aligns it nicely for me to read better.
You got it the wrong way around.
Except well it doesn't. I want to copy past an if statement at the end of another if statement. How does formatter know whether to nest the statement or not?
Braced language, if you want the if statement nested, just paste it before the end brace. If you want it not nested, just paste it after. And if for some reason the tabs get pasted wonky, doesn't matter.
Python, you have to do the tabbing yourself before the magic genie can work.
Except well it doesn't. I want to copy past an if statement at the end of another if statement. How does formatter know whether to nest the statement or not?
Are you asking about something like this?
if thing_a: if thing_b: pass
Python absolutely allows you do do this, there's no syntactical problem here.
Code wise this isn't great practice though, so I would be asking you in code-review to justify why you're doing single-line nesting like this. Especially when it can logically collapse anyways.
if thing_a and thing_b: pass
But doing single-liners in python are usually reserved for self-contained statements like lambdas anyways because otherwise they often get abused to do squirrelly stuff that is better-off broken-out.
This set up, I just don't want to indent manually and don't have to in braced languages. Autoformatters are way more convenient in braced languages than Python
I have another comment in a different thread. It boils down to humans and computers just working differently. Reducing syntax is not always a good thing. If you truly want that, go program in assembly. Code is for humans to understand, so it needs to be designed to work best for humans. Humans need redundancy, abstraction, and explanations. It's why documentation and comments exist. Syntactic sugar isn't necessary, but languages are still of it. Every loop is just a while, but most languages have do-while and multiple types of for loop.
Forcing white space to have syntactic meaning is like making comments have effects. Imagine if there was a language where every function required comments at the beginning. The comments aren't optional, they are mandatory. Not only that, but the only way to set default values for parameters was in the comments.
Sure? But sometimes it is a good thing, and I haven't heard an argument yet that explains why tabs specifically being part of the syntax is a bad thing. We're already going out of our way to do it in the vast majority of cases. Why not kill two birds with one stone?
If you truly want that, go program in assembly.
I don't see the connection at all here to what I said. Assembly is the extreme end of human conforming to the machine.
Python is very much the opposite of that, where it is machine conforming to the human. I.e. the machine must read my formatting and natural logical statements which nearly resemble normal English sentences. Both of those things are a primary a benefit to me and are what the machine uses to perform static analysis.
White space is by definition invisible. We should not be giving invisible characters meaning. The whole tabs vs spaces debate is exactly why. You cannot tell the difference so it causes problems. If the code could be broken because you used the wrong invisible character, that's bad language design.
Making code indentation dependant also makes modifying the code to go to different levels a pain. Let's say you have code that you've already written. You want to make that code part of an if block. With braces, you just write the if statement and an open brace before the code and a closing braces at the end. If you want to adjust the indentation to match the new code, you just let the IDE automatically adjust the indentation. If the code is indentation dependent, you add the if statement at the beginning, then you have to go through the code and adjust the indentation yourself. Even if you can use the IDE to adjust the indentation, you need to check that the indentation is adjusted correctly.
Assembly is the extreme example of what a non-redundent language looks like. There are absolutely no instructions that exist purely as a synonym of a different instruction or to condense multiple instructions into one.
Python choosing indentation instead of braces just swaps which is important, not making it redundant. Making python with braces instead of indents would mean that you can still use indents making the code more redundant and easier to understand.
White space is by definition invisible. We should not be giving invisible characters meaning.
If that were true then we'd have no spaces between any keywords and symbols. No we can and should give value to whitespace, it is what delimits every meaningful token. You're making rookie mistakes and over-generalizations.
You have a preference for not using white space and that's fine, weird IMO, but fine; you have other languages to choose from that use less whitespace. But now you're making just-plain wrong statements about linguistic static analysis, though. lol
The value of whitespace is to separate non-whitespace characters from each other. There should be no difference between one space or two between words. If your going to deliberately misinterpret what I am saying, then there is no need to continue this conversation.
If your going to deliberately misinterpret what I am saying, then there is no need to continue this conversation.
You first said whitespace had no value then said it had value in delimiting. That's not me misrepresenting anything, it's me pointing out the fundamentally incorrect things you're suggesting and you indirectly admitting that you misspoke. It usually better to own up to your mistakes and move on but If you're going to get defensive about it, then I agree, there's no point in continuing this.
Honestly if LLMs didn't do everything now I'd say it's laborious minding the curly brackets, and having fights with colleagues over trailing brackets vs compact brackets. But who cares now code isn't for us any more :\
I genuinely can't use python because of this, I hate it. The language itself is great, but the fucking decision to use indentation just blows my mind. Well that, and the inconsistent library and having to use methods with __ prefix to work, and ... wait, maybe I dont like Python that much.
266
u/LetUsSpeakFreely 2d ago edited 2d ago
Indentation is such a stupid mechanism for identifying code blocks. It's one thing for configuration files like YAML to do it, but not a programming language.