r/Python • • Apr 29 '20

Discussion Tab vs Spaces

Post image
702 Upvotes

72 comments sorted by

View all comments

Show parent comments

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)