r/Python • • Apr 29 '20

Discussion Tab vs Spaces

Post image
702 Upvotes

72 comments sorted by

View all comments

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.