

Ask HN: Why PEP8 favors using spaces over tabs for code indentation? - codegeek

This is something that I, a novice python wannabe,has been struggling with. I love to use tabs in my editors to indent code. Much easier to remember than using 4 spaces (as per PEP8 standard). Can some python expert clarify this ?
======
cutie
The reason is that when sharing code use of tabs can lead to confusion,
subtle, and not so subtle errors, especially in Python, where consistent
indentation is most important.

For example, one person has tabs set to the size of 8 chars and writes a
lengthy script. A second person comes along with tabs set to 5 chars and makes
changes. The indentation could well be wrong because they see the same file
differently. Sometimes you get lucky and you get errors immediately, but other
bugs can lurk... like the second statement under an if block for example.
Should it be included in the if or not? Bug.

Therefore, tabs aren't worth the trouble they can potentially cause. Never mix
them with spaces either.

Also, using spaces is not difficult in a modern editor. You don't have to
actually type four spaces each level. You set tabs as 4 spaces in the prefs
and then every time you hit the tab key you get 4 spaces instead, and when you
hit the backspace it removes 4 spaces (geany does this).

Whatever you use, I suggest turning on the display of whitespace in your
editor. It will then show tabs as "--->" and spaces as a dot. Change the color
in the highlighting preferences to be very subtle so it does not overwhelm the
code. It will help you keep every file you work on consistent and such errors
a thing of the past.

~~~
Rust
In your example, if both developers are using TABs instead of spaces, why
would it matter that one sees it as 5 spaces and the other as 8? It's still
just a single TAB character either way.

Plus, each developer has the freedom to choose how many spaces a TAB is - I
like 8 when I'm using my big monitor, but I prefer 4 on my laptop. With TABs,
I can see it both ways depending on the machine.

On the off chance that I'm ever actually editing in a smaller window, I can
even set it to 2 characters instead, but when I open that file again on
another machine, I can see it as 4 or 8 characters.

~~~
cutie
Yes, there's an error there, conflating two ideas. I think tabs-only can still
cause a problem, say when lining up comments on the end of a line because
there might be a different amount needed per line depending on its length.
Still, you can't be sure what other people will have, so for interop spaces it
is.

It might not be the case for everyone, but my laptop screen is 1920x1080 and
has more room for text than I can use. In a three way merge your strategy
could help me perhaps.

------
tyger11
Using tabs over spaces is a catastrophic misunderstanding of modern text
editors. Spaces are the ONLY one-size-fits-all solution for a multi-developer
environment. Text editors can be configured to SHOW a tab character visually
as any number of spaces you want - without changing the code. If someone likes
3 spaces, they can see a tab character as 3 spaces; if someone else likes 8
spaces, they can show each tab as 8 spaces. Treating a tab as a "tab stop" is
the mindset of a typewriter user.

~~~
codegeek
Not sure if I followed what you said. What I mean is that isnt hitting the
spacebar 4 times more difficult and annoying than hitting the tab key once ?
Yes you can configure tab key for any number of spaces but still the point
being about hitting spacebar multiple times.

~~~
kudakeru
Modern editors generally don't require you to hit space four times. There's a
"convert tabs to spaces" option so you can hit the tab key, and most of them
do auto-indenting anyway.

------
csense
I chose spaces over tabs long before I started writing Python. The issues with
tabs are:

(1) Some editors will autoconvert them to spaces. Every time you switch
editors, switch IDE's, edit your code on a different computer, or let someone
else work on it, you have to remember to make sure your tabs are still there.

(2) With tabs, your code will look different for different people. I often
line up parts of adjacent lines that are symmetric, and different tab widths
will interfere with this. Ditto ASCII art in comments.

(3) It's hard to use tabs consistently because tabs and spaces are visually
identical in most editors. (This is less of a problem in Python than other
languages, because inconsistent tabs/spaces in your indentation are often
caught quickly because they cause syntax errors.)

(4) My mathematical mind admires systems that are designed around simple and
elegant ideas. A character display embodies the following two such ideas:

(a) Each location on the screen contains exactly one character.

(b) There is a one-to-one mapping between the pixel contents of an 8x8 box (or
however big your font is, assuming all sane people use monospace fonts for
coding) and an ASCII character. (Don't get me started on Unicode. I don't
understand it, don't use it, and it haven't had it do anything for me but
cause bizarre problems [1].)

Tabs break both of these properties. This causes cognitive dissonance and
offends my sense of aesthetics.

[1] <http://news.ycombinator.com/item?id=4369323>

------
runjake
Most editors automatically convert TABs to _x_ number of spaces automatically,
so you don't have to think about it.

For example, see _set expandtab_ in vim:
<http://vim.wikia.com/wiki/Converting_tabs_to_spaces>

------
Rust
I'm no Python expert, but the only reason I can think of is a combination of
developer inertia and tradition.

In most IDEs, a TAB can be displayed as any number of equivalent spaces (8
being the common indent size), so if I switch back and forth between my big
monitor (8) and my laptop screen (4), I can view the code with an indent
relative to the width of the screen.

If that code uses spaces, I'm usually stuck seeing an indent of 4 on the big
screen monitor - something I find irritating because it makes it harder to
visually scan and parse code quickly.

So, even in Python, I always use TABs whenever I have the choice. It's just
more flexible.

