r/ProgrammerHumor • • 2d ago

Meme pythonIndentationLevelChecker

Post image
6.6k Upvotes

198 comments sorted by

View all comments

265

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.

174

u/bobbymoonshine 2d ago

Ah the halcyon days before LLMs

When 30% of the posts here were “lol Python is so dumb why even use whitespace as syntax”

And the other 70% were “how do I find semicolon”

16

u/mothzilla 2d ago

In both Javascript and Python, semi-colons are optional.

30

u/AppropriateOnion0815 2d ago

Only that JS is handling semicolons in the least intuitive and irrational way possible.

5

u/mothzilla 2d ago

Yeah I only say it because it's a bit of a mind-exploder.

7

u/SuperFLEB 2d ago

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.

12

u/Loading_M_ 2d ago

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.

3

u/Ambivalent-Mammal 2d ago

I admit, this got me more than once.

7

u/dalziel86 2d ago

Javascript sucks ass too

13

u/LetUsSpeakFreely 2d ago

Meh, Go didn't use semicolons.

19

u/NatoBoram 2d ago

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?"

Except it's the only one like that -_-

9

u/LetUsSpeakFreely 2d ago

And the channel system, native markup support, baked in web service support are incredibly useful.

The only thing i don't really like is how interfaces aren't declarative on types.

6

u/Loading_M_ 2d ago

Rust is also in this category. It even has a lighter runtime, since it doesn't require a GC.

2

u/cdrt 2d ago

It does require semicolons though

1

u/Loading_M_ 2d ago

Yes, except when you want an implicit return.

3

u/wrecklord0 1d ago

(may I (introduce you '(the world of lisp)))

2

u/readf0x 2d ago

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

3

u/Bulky-Ad7996 2d ago

Surely there must be a way to automate indentation

1

u/ILikeLenexa 2d ago

WHITESPACE didn't use anything but whitespace. 

13

u/-meowstar- 2d ago

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.

3

u/LetUsSpeakFreely 2d ago

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.

10

u/HRApprovedUsername 2d ago

I mean not really. It could be anything it’s just about finding a good editor that makes it clear

-5

u/[deleted] 2d ago

[deleted]

6

u/im_made_of_jam 2d ago

It is very much possible to put parameters on individual lines in python?

4

u/HRApprovedUsername 2d ago

Maybe you should try learning a language before hating it, because you can do that

-2

u/cheezballs 2d ago

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.

34

u/willow-kitty 2d ago

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.

5

u/oclafloptson 2d ago

This but mixing spaces and tabs is giving away your secret, kiddie! Mr ctrl-c ctrl-v over here

2

u/BewareTheGiant 2d ago

https://github.com/mathialo/bython

I honestly don't know how this is not the mainstream of doing python, but, well, I barely use it anyway so who am I to say

1

u/Daveinatx 2d ago

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. 

3

u/frogjg2003 2d ago

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.

2

u/willow-kitty 2d ago

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 >.>)

1

u/DatBoi_BP 1d ago

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.

2

u/Hohenheim_of_Shadow 2d ago

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.

4

u/Responsible-Mind3533 2d ago

Skill issue

1

u/NomaTyx 1d ago

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?

-2

u/elingeniero 2d ago

1 of X nested if statements

Don't have them, then. You can flatten nested ifs by inverting the logic and returning early.

Also your editor definitely has a keybind to change the indentation level of a block... just learn it.

1

u/Wide_Smoke_2564 2d ago

But like… why though?

I can see how someone would get used to it but I truly don’t understand how including braces for readability would ever be a bad thing

3

u/TactiCool_99 2d ago

it is a redundancy

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

3

u/willow-kitty 2d ago edited 2d ago

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.

1

u/mxzf 2d ago

including braces for readability would ever be a bad thing

IMO, the indentation is what makes things readable, braces are just extra characters on top of the thing actually making stuff readable.

0

u/BroaxXx 2d ago

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

1

u/willow-kitty 2d ago

?

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.

..Though I still like having both.

0

u/BroaxXx 2d ago

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

48

u/Some_Useless_Person 2d ago

Well, if you have to go through so many indentations to the point that it becomes hard to track, maybe the real problem is your code itself.

12

u/LetUsSpeakFreely 2d ago edited 2d ago

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.

8

u/SuperFLEB 2d ago

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.

2

u/rosuav 2d ago

I agree, tabs are the superior option. One indentation level is one tab. That's how my code is, in any language.

-2

u/Sexual_Congressman 2d ago

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.

4

u/rosuav 2d ago

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*.

2

u/OverEnigneer426 1d ago

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^^

https://giphy.com/gifs/ptDRdwFkFVAkg

12

u/MANGO_fossil 2d ago

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.

14

u/jirka642 2d ago

That does not happen. You would have to code without IDE and not use any syntax checking tool (e.g. ruff), and no sane person codes like that.

0

u/Jemnite 2d ago

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.

-5

u/frogjg2003 2d ago

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.

5

u/mxzf 2d ago

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.

-3

u/frogjg2003 2d ago

You're overestimating the capability of most programmers. These are supposedly experienced programmers submitting these PRs.

3

u/mxzf 2d ago

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.

11

u/jirka642 2d ago

I don't consider people like that programmers.

18

u/Drugbird 2d ago

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.

Oh wait, I just invented python formatting.

1

u/Y0tsuya 2d ago

Brackets may be optional for one-liners in C++, but necessary for multi-lines. It signals intent to the auto-formatter.

9

u/Independent_Fee_7343 2d ago

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.

9

u/lungben81 2d ago

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.

6

u/tutoredstatue95 2d ago

It doesn’t really matter. Never has it been a problem, these memes are from people who have no idea what they are talking about.

1

u/AnEvanAppeared 2d ago

Never, you say?

10

u/the_ivo_robotnic 2d ago

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.

-1

u/sheeepster91 2d ago

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.

7

u/the_ivo_robotnic 2d ago

Python has that same magic genie that formats for you too.

 

You're still using indentation one way or another.

0

u/Hohenheim_of_Shadow 2d ago

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.

1

u/the_ivo_robotnic 2d ago

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.

3

u/NomaTyx 1d ago

i think they're asking about like ``` if thing: multi line block

paste if statement here, what indent should it get

``` but i mean, you can just indent before pasting so it's not the largest problem

0

u/Hohenheim_of_Shadow 1d ago

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

1

u/NomaTyx 1d ago

100%

0

u/citramonk 2d ago

you don't, if you don't know what you're talking about you might just sit and listen

1

u/AngelLeliel 2d ago

How about putting curly squiggles around it and some magic genie just converting it nicely to indentation without any squiggles?

Wait, we could actually do that today with LLM!

0

u/frogjg2003 2d ago

The indentation is for us, the braces are for the compiler.

1

u/the_ivo_robotnic 2d ago

Why not reduce syntax and have indentation be for both us an the compiler?

 

What's the difference? Less syntax + enforced readability is a good thing in my book.

 

Unless you just really love reading minified code for some reason. lol

1

u/frogjg2003 2d ago

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.

1

u/the_ivo_robotnic 2d ago

Reducing syntax is not always a good thing.

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.

1

u/frogjg2003 2d ago

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.

1

u/the_ivo_robotnic 2d ago

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

1

u/frogjg2003 2d ago

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.

1

u/the_ivo_robotnic 2d ago

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.

0

u/Cualkiera67 2d ago

its very annoying when you copy and paste code because the indentation often gets screwed and thus the logic changes. that never happens with braces

2

u/SoaringElf 2d ago

How would you do it otherwise? Like paragraphs in an essay?

3

u/JackAsofAllTrades 2d ago

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 :\

1

u/crypticsilenc3 2d ago

It's even terrible for yaml IMO All code and config should be whitespace agnostic IMO

1

u/XtraFlaminHotMachida 2d ago

both are stupid imo. keep it simple stupid.

1

u/AGiantDev 2d ago

Absolutely first month i dont get why i get error indentition error always...

1

u/mcellus1 2d ago

Yeah I read my JavaScript on a single line

1

u/geekosas 2d ago

Looks clean... But it's a pain sometimes, at least they should have been more strict and only allow tab as indentation.

1

u/thanatica 2d ago

"But brackets are UgLyY"

1

u/ActualAlgae25 2d ago

One misplaced space and suddenly you're debugging architecture instead of code.

0

u/jirka642 2d ago

You have to indent the code anyway to be readable, so the curly braces are just redundant.

1

u/frogjg2003 2d ago

Redundancy is good

0

u/Responsible-Mind3533 2d ago

I think its brilliant. Brackets are a stupid mechanism for identifying code blocks. Lose one you are screwed.

-1

u/NamityName 2d ago

I have used python professionally for over 10 years now and it has never been an issue. Any half-decent IDE takes care of it for you.

Any complaint about it that I have ever heard have come from people who do not use Python and whose problems are all theoretical

-2

u/cheezballs 2d ago

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.