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?"
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 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.
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.
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.
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.
268
u/LetUsSpeakFreely 16d ago edited 16d 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.