r/Python • • Apr 29 '20

Discussion Tab vs Spaces

Post image
702 Upvotes

72 comments sorted by

View all comments

51

u/zacharypamela Apr 29 '20

Those spaces must've gotten turned around on their way to the Pep 8 club. 😃

-2

u/jeremiah1119 Apr 30 '20 edited Apr 30 '20

Woah woah woah, spaces are preferred? What madness is this? (for real, I don't code so I don't know about this stuff)

17

u/Zomunieo Apr 30 '20

Spaces look the same to every user so formatting is preserved perfectly. (Except for you weirdos who use variable width fonts for code. I'm watching you.) Tabs don't because people have different space per tabs settings.

4

u/-Tilde Apr 30 '20

Which to me, seems like a good thing rather than a bad. So what if I want to have 2 characters or 4 characters or 8 characters per tab?

6

u/Zomunieo Apr 30 '20

Many reasons:

*If you like 2, 4 or 8 characters per tab, you can use 2, 4 or 8 spaces. Your editor should be able to change the indentation to your preference if you like.

*Sometimes people do ASCII diagrams, tables, or equations in their comments. These will get messed up with tabs unless everyone who edits them is very disciplined about using spaces and tabs appropriately. (They're not.) When done with spaces they always look consistent.

*You can't type a literal tab in web browser's text box, including Reddit's. Websites can't display true tabs and they don't work properly in copy and paste - not without some Javascript anyway.

-3

u/[deleted] Apr 30 '20

*If you like 2, 4 or 8 characters per tab, you can use 2, 4 or 8 spaces. Your editor should be able to change the indentation to your preference if you like.

No. Absolutely not. That would mean changing the whole code because of a formatting preference. If I see any of my colleagues do this, it will warrant a serious proverbial slap.

*Sometimes people do ASCII diagrams, tables, or equations in their comments. These will get messed up with tabs unless everyone who edits them is very disciplined about using spaces and tabs appropriately. (They're not.) When done with spaces they always look consistent.

This is another problem that, yes, solved with spaces. This is not indentation though. This is alignment.

Indentation is what you use for nested code. Alignment is what you do to make the = signs be under each other to be pretty.

*You can't type a literal tab in web browser's text box, including Reddit's. Websites can't display true tabs and they don't work properly in copy and paste - not without some Javascript anyway.

	what is this then

Well, without Javascript you can't really make the "Tab" character work nicely. And if you have to sit there and tap Space 12 times for code that's nested 3 levels deep, then you're definitely doing it very wrong.

4

u/[deleted] Apr 30 '20

Any modern IDE should (a) understand the user’s intent for encoding tabs or spaces, (b) translate the tab key into that intent, and (c) intelligently auto-indent in accordance with that intent based on the syntactic context.

Nobody should be hammering the space bar a bunch of times to indent multiple levels.

-1

u/[deleted] Apr 30 '20

Well obviously, that was a ridicule of the idea of typing code in a web editor that uses the tab key for switching between page elements. Have you read what I was replying to?

1

u/[deleted] Apr 30 '20

Have you read what I was replying to?

Yes, and you made similar remarks (regarding alignment vs. indenting) in other comments in this post, to which I've separately replied. If you like, I can reproduce them here.

I responded here only to address the spacebar thing. And you're not the only person to raise that argument in this post: see here.

-1

u/[deleted] Apr 30 '20

Yes

liar

I responded here only to address the spacebar thing

which you would know is not a serious suggestion, and just highlighting the ridiculousness of some of the stuff in the post that I replied to. I was writing EXACTLY why it was bad, and now you tell me it is bad.

And you're not the only person to raise that argument in this post

That wasn't my argument, not my post. You're generalizing completely different people who made completely different things, because: Have you read what I was replying to?, the answer should be "No", you didn't read.

→ More replies (0)

1

u/imanexpertama Apr 30 '20

Always wondered about the why, thanks

1

u/[deleted] Apr 30 '20

Spaces look the same to every user

How can you turn a disadvantage into an advantage?

Users are different. Users use different machines in different ways. What if I can't stand the 4-space indentation? With tabs, I can customise that. With spaces, I can't. This is the absence of a feature, that you're spinning as a benefit.

And if you're referring to alignment instead of indentation, which has to be a fixed character width to match the above or below fixed number of characters, then it's done with spaces. That way, indentation (which dictates the nesting depth you are in) can easily be tabs, while formatting and next-row-alignment can be supplemented with spaces. This was a problem that was solved even in 2000, how can it be a problem 20 years later?

2

u/[deleted] Apr 30 '20 edited Apr 30 '20

You can’t mix-and-match like that. The variable width of tabs will alter any following fixed-width alignment via spaces.

For example:

def some_function(self, request):
    self.requests += 1        # record request
    if self.response is not None:
        self.responses += 1   # keep in sync with self.requests

Here, it’s important to align the comments. That can’t be reconciled with preceding variable-width tabs.

1

u/JimDabell Apr 30 '20

You shouldn't align code like that. If you change the length of one of the lines, you need to edit the other line to keep the alignment. This can cause unnecessary merge conflicts.

The example PEP8 provides is similar:

Avoid extraneous whitespace in the following situations:

# Wrong:
x             = 1
y             = 2
long_variable = 3

3

u/[deleted] Apr 30 '20

That's interesting, but there is some nuance here.

The problem with using whitespace in the PEP8 example is that it is in the middle of a statement, which disrupts readability. Alignment of comments following statements is different because the things that are separate are individual units.

For instance, there are clear problems with readability here:

The quick brown fox      jumped over the lazy dogs.
She sells seashells      by the seashore.

...but not with this:

Goodnight, bears.        Goodnight, clocks.
Goodnight, chairs.       And goodnight, socks.

In fact, in the latter example, readability is improved by separating the different units. So, too, is readability improved by using whitespace to set comments apart from programming statements.

0

u/JimDabell May 06 '20

The problem with using whitespace in the PEP8 example is that it is in the middle of a statement, which disrupts readability.

No, that's not the problem. I already said what the problem was. The problem is that it can cause unnecessary merge conflicts. This is true regardless of whether you're commenting or assigning. Most style choices don't have an impact beyond the immediate readability. This style choice causes further problems with version control.

1

u/[deleted] May 06 '20

Not all projects are version-controlled.

And among those that are, not all projects have multiple branches that raise the prospect of merge conflicts.

Why should a technical consideration that exists in a sub-subset of projects justify a general-purpose style rule for all projects? Your explanation of ā€œthe problemā€ does not apply to a vast proportion of Python projects.

0

u/JimDabell May 06 '20

a sub-subset of projects

You're describing the norm as if it were an extreme minority. Why?

→ More replies (0)

-2

u/[deleted] Apr 30 '20

Never put comments at the end of lines. It suggests that either the code is bad and isn't evident what it's doing, either that you don't know that comments should highlight why something is fine and not what is done, which would make them longer and unreadable when coupled with code. Also code lines are often long enough instead, without comments afterwards.

If it doesn't work for every case, use something that does - put comments before the statement, not on the same line. This allows more room to write proper comments

→ More replies (6)

-5

u/Yojihito Apr 30 '20

1 Tab = 1 intendation = 1 key press

4 spaces = 1 intendation = 4 key presses

4 times the work everytime.

7

u/SalemClass Apr 30 '20

If you're tapping space 4 times you're doing it wrong. Basically every code editor supports using the tab key for spaces indentation.

4

u/[deleted] Apr 30 '20

They've got these great things called "text editors", with features like tab expand. You should check them out!

1

u/OldSchoolBBSer Apr 30 '20

Not for me. I've done both for a while, and really they both suck at some point. It depends so much on: 1) the type of programming and language, 2) the tools to be used, and 3) the team (if there ever will be one working on that code). Neither is better than the other and "most cases" has little meaning when you start to dig into it. I settled back on tabs for everything I do. I'm ok with spaces if it fits and number has been agreed on, but only when I'm in an IDE/ST3, otherwise annoys the piss outa me if I have to use a stock/simple editor.

1

u/flying-sheep Apr 30 '20

Tabs are better than spaces for a number of reasons, but black is the best. It happens to use spaces, but the fact that it’s automatic supersedes that.