r/Python • • Apr 29 '20

Discussion Tab vs Spaces

Post image
702 Upvotes

72 comments sorted by

51

u/zacharypamela Apr 29 '20

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

12

u/Shakaka88 Apr 30 '20

Glad someone pointed this out Glad it’s currently too comment Comic is funny, but people should know that spaces are preferred in the PEP style guide

12

u/ggchappell Apr 30 '20 edited Apr 30 '20

people should know that spaces are preferred in the PEP style guide

FTFY ;-)

0

u/[deleted] Apr 30 '20

[deleted]

1

u/Ignisor Apr 30 '20

PyCharm use 120 line chars max length by default. And many developers do so

→ More replies (1)

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

→ More replies (5)

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.

→ More replies (4)

-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

4

u/[deleted] Apr 30 '20

I find it ironic that one comment you are defending personal choice, and the next you are saying you absolutely must not comment in this way

→ More replies (2)
→ More replies (2)

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

20

u/mr1337 Apr 30 '20

Alright everyone, back to the Space Bar!

46

u/roerd Apr 30 '20

The only correct answer is of course using the tab key to insert spaces.

-7

u/[deleted] Apr 30 '20

The correct way is to use Tab for Tabs, and Space for Spaces. Tab for indentation, Space for alignment.

That way code stays formatted at all times, even with alignment art, and indentation remains flexible to each user's preferences, and is less prone to errors when deleting characters.

9

u/TheAcanthopterygian Apr 30 '20

So wrong!

Mixing tabs and spaces is a perfect recipe for broken indentation AND painful editing.

-2

u/[deleted] Apr 30 '20

What are you talking about? Never had a single issue. More often I've had to deal with someone else's bug induced by adding just 3 spaces instead of 4 in an indentation.

2

u/[deleted] Apr 30 '20

As above - consider this:

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

How are you supposed to keep those comments aligned if the lines start with variable-width tabs?

1

u/billsil May 01 '20

Write a docstring. Also the first comment is clearly redundant redundant.

-1

u/[deleted] Apr 30 '20

Ok. You have a piece of code in a style that I'll never recommend anyone write for various reasons like:

  • Comments should explain why code does something, not what it does. Code is for that

  • Explanatory comments are usually long

  • Writing long comments at the same line will quickly make the line too long and just unreadable

In this case, when you write bad code, it will also look inconsistently on other preferences

4

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

First - the example was not about the content of the comment. It was about the format, as in, spaces vs. tabs. By criticizing the example based on your opinions about the content of the comment, you have failed (deliberately or not) to address the issue for which it was presented.

But to humor you and to address your proposition that comments are only legitimate if they are lengthy, here is a revised example:

def some_function(self, request):
    self.requests += 1        # see bug report 1,840
    if self.response is not None:
        self.responses += 1   # see bug report 1,919

Are you really going to take the position that those comments are necessarily superfluous because they are brief?

I'm going to predict your next argument: "They should be on their own line, because otherwise they might not be aligned due to variable-width tabs" - which would, of course, be solipsistic: your personal preferences for tabs does not conflict with any legitimate examples, and this example is de facto illegitimate because it conflicts with your personal preference.

Finally, since PEP8 has been cited as an authority, I'll just point out this portion about proper comments:

Sometimes, this is useful:

x = x + 1         # Compensate for border

1

u/billsil May 01 '20

Code stays formatted thanks to my auto-replace tabs with 4 spaces on push.

1

u/[deleted] May 01 '20

Then when you pull you still have to deal with spaces.

1

u/billsil May 01 '20

That's a "problem" that I can live with.

I also strip all trailing whitespace. My coworker who tends to run my projects got annoyed that there was a diff. Ok...set your IDE to do it too. He eventually gave in.

1

u/[deleted] May 01 '20

Why would you have a problem in the first place?

Ok...set your IDE to do it too. He eventually gave in.

Sounds like you're not an easy person to work with

1

u/billsil May 01 '20

I have my moments. I'd rather have clean code than be easy. It's a very small battle. I got half the 25 person company to adopt my style without even trying. I created it over years based on standard python, how to avoid bugs I commonly make, and how to write nice looking docstrings.

One of us has to give in right? There is no compromise between don't strip whitespace and strip all whitespace. It's annoying to ever have a diff due to whitespace changes. Obviously, you can still add a blank line or remove a blank line, but you should only ever have the whitespace commit once.

Sometimes I'm the voice of reason. Between 3 people, we had 3 opinions on how long and descriptive variable names should be. The previous person advocated variable names of 40+ characters. Another coworker was in the 6 characters max camp. I'm vetoing both of those.

Same thing with line length. We're not doing 160 characters and we're not doing 80. I can handle 100 most of the time with the very rare 140 or whatever at that point. Yeah that was also a debate.

5

u/[deleted] Apr 30 '20

I always preferred spaces... but since I've been using Go lately, I realized that I actually love big fat lazy tabs hahahaha

If you have to use vi/vim to work on servers ever though.. tabs are bogus for that imo.

0

u/[deleted] Apr 30 '20

[deleted]

2

u/[deleted] Apr 30 '20

Every text editor can do this, including vi/vim. It's generally the first thing I do when setting up a new machine with vim. vi/vim can easily edit remote files, so you can keep your settings when working on remote machines

1

u/flying-sheep Apr 30 '20

PyCharm has a per-project setting, and also infers from individual files.

It can also be set up to use black, which is superior to manual formatting anyway.

2

u/Toprelemons Apr 30 '20

Double quotes gang.

3

u/mehx9 Apr 30 '20

pip install black && black $FILE ...and carry on!

1

u/[deleted] Apr 30 '20

HAHA relatable cough coughs INDENTATION ERRORs

1

u/lolitsroo Apr 30 '20

Unsure if other people have ran into this, but Google Cloud Platform won't deploy your function if you have 1 inconsistent spacing in your code (at least in Cloud Functions). Super frustrating. It defaults to 4 spaces and as a tab person it gets annoying. This reminded me of that

4

u/Sukrim Apr 30 '20

Afaik go won't even compile code that's not formatted correctly.

1

u/Wilfred-kun Apr 30 '20
>>> for i in "hello":
...     print(i)    # 4 spaces
...     print(i)    # tab
  File "<stdin>", line 3
    print(i)
    ^

0

u/Sukrim Apr 30 '20

There should be a Python distro called "Black Mamba" that only accepts code formatted with black! :D

0

u/wickeddawg Apr 29 '20

good stuff

-6

u/[deleted] Apr 30 '20

Tabs forever! xxx

-5

u/prettyanonymousXD Apr 30 '20 edited Apr 30 '20

Repost. Why all the downvotes? It literally is.

-5

u/indytechbox Apr 29 '20

So goooood 😂😂😂

0

u/[deleted] Apr 30 '20

[deleted]

21

u/zacharypamela Apr 30 '20

Python, unlike other languages, uses white space to group statements. So the body of a function will be indented, rather than enclosed in curly braces.

So when you mix tabs and spaces, it's sort of like trying to close your function with a parenthesis instead of curly brace.

As you might expect, this can cause consternation. A tab might look identical to a series of spaces, leading to hard-to-identify issues in your code. Hence this comic.

-8

u/o11c Apr 30 '20

International Agreement dictates that "tab" means "round up to the next multiple of 8 spaces".

Python2 allowed tabs to be used as International Agreement provides.

Unfortunately, some people configure their editors to violate this, leading to problems, so Python3 decided to make the error deterministic.

1

u/billsil May 02 '20

International Agreement dictates that "tab" means "round up to the next multiple of 8 spaces".

Not in Python 2. It means whatever is local.