r/Python • • Apr 29 '20

Discussion Tab vs Spaces

Post image
702 Upvotes

72 comments sorted by

View all comments

Show parent comments

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

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

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

0

u/[deleted] Apr 30 '20

Personal choice is personal. Nobody else has to deal with it. The fact that I set my tabs to 20 for example will be unknown to anybody so it's no concern.

So why is it funny? Because the comments are NOT a personal choice, since a lot of people will be editing the same code and will be scratching their head because it will say

something += 1  # Increment something

and the reader will be "well thank you captain obvious!", and curse at the screen because he has no clue WHY that thing was necessary there.

3

u/[deleted] Apr 30 '20

So if someone wrote:

# Increment something
something += 1

You would be totally fine with that right? Since your disagreement was over the format of the comments, not the idea of commenting obvious code

1

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/[deleted] Apr 30 '20

First - the example was not about the content of the comment. It was about the format, as in, spaces vs. tabs

Which in normal code isn't an issue. Usually you need to align words, numbers, whatever, that are all in the same nesting level. Doing it across levels doesn't make sense. And if this was an example of why it would make sense, it's a bad one.

self.requests += 1 # see bug report 1,840

Again a bad example. There's git blame for this. This type of comment is bad because it still doesn't explain why, but rather leads you to some weird place. Bug report 1840 where? Flyspray? Jira? We switched 3 years ago and lost the Flyspray DB, now what?

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

Kinda yeah... Most of the times comments that are necessary are much longer. And in the rare cases where they're short, there are zero benefits to writing them at the end of the line (inconsistent with the rest of the comments), and definitely zero benefit in having them aligned.

I'm going to predict your next argument

Well you kinda failed.

Finally, since PEP8 has been cited as an authority

Argument from authority. Just because an authority says so, doesn't mean that it is better. An authority dictated that this is the standard. One has the choice of following or not, however because of the nature of standards, that choice is practically nil. This is a prime example of how some of the worst standards get to live, just because they become standardized.

Luckily there are also other authorities, not just pep8. For makefiles you don't even have the option of space indentation, because the creators realized that it's kinda stupid and didn't even support it.

Sometimes, this is useful: x = x + 1 # Compensate for border

Why not keep it consistent?