Tabs versus Spaces in Source Code
Xah Lee, 2006-05-13
In coding a computer program, there’s often the choices of tabs or
spaces for code indentation. There is a large amount of confusion
about which is better. It has become what's known as “religious
war” — a heated fight over trivia. In this essay, i like to
explain what is the situation behind it, and which is proper.
Simply put, tabs is proper, and spaces are improper. Why? This may
seem ridiculously simple given the de facto ball of confusion: the
semantics of tabs is what indenting is about, while, using spaces to
align code is a hack.
Now, tech geekers may object to this simple qualification because
they itch to drivel about different editors and so on. Simply put,
the alleged problem created by tabs as seen in the industry coders
are caused by 2 things: (1) stupid tech geekers who did not
understand things and think about things. (i.e. the semantics of tabs
vs spaces) (2) Due to (1), it has created a massive none-
understanding and mis-use, to the degree that many editors are fucked
upfront (e.g. vi), so that in the end spaces seem to be actually
better. In short, this is a phenomenon of misunderstanding begetting
a snowball of misunderstanding, such that it created a cultural
milieu to embrace this misunderstanding and kick what is true. (this
happens a lot in unix. For one non-unix example, is the file name's 3-
character “extension” (e.g. “.txt”, “.html”) as part of
the file name. Another popular example is the HTML practices in the
industry, where stupid coding and misunderstanding are so wide-spread
such that they force the correct way to the side by any eventual
standardization)
Now, tech geekers may still object, that tabs requires the editors to
set their positions, and plain files don't carry that information.
This is a good question, and the solution is to advance the sciences
such that your source code in some way embed such information. This
would be progress. However, this is never thought of because the
fucking unix already fucked up people's mind by hacking things. In
this case, many will simply use the character intended to word
separation for the purpose of indentation or alignment, and spread it
with hostile drivels.
Now, given the already fucked up situation of the tabs vs spaces by
the unixers (vi) and unix brain-washing of the coders in the
industry... What can we do NOW? I do not have a good proposition,
other than just use whichever that works for you but put more
critical thinking into things to prevent mishaps like this.
Tabs vs Spaces can be thought of as parameters vs hard-coded values,
or HTML vs ascii format, or XML/CSS vs HTML 4, or structural vs
visual, or semantic vs format. In these, in is always easy to convert
from the former to the latter, but near impossible from the latter to
the former. And, that is because the former encodes information that
is lost in the latter. If we look ta tabs vs spaces, indeed, it is
easy to convert tabs to space in a source code, but more difficult to
convert from spaces to tabs. Because, tabs as indentation actually
contains the semantic information about indentation. With spaces,
this critical information is botched.
This issue is intimately related to another issue in source code:
soft-wrapped lines versus physical, hard-wrapped lines by EOL (end of
line character). This issue here is far more consequences than tabs
vs space, and the unixer's unthinking has made far-reaching damages
in the computing industry. Due to unix's EOL ways of thinking, it has
created languages based on EOL (just about ALL languages except the
Lisp family including Mathematica) and tools based on EOL (cvs, diff,
grep, and basically every tool in unix), thoughts based on EOL (code
size estimating by counting EOL, hard-coded email quoting system by
“>” prefix, and silent line-truncations in many unix tools), such
that any progress towards a “programing code unit” or
“algorithmic code unit” concept are suppressed. I have not written
a full account on this issue, but i've touched it in this essay:
“The Harm of hard-wrapping Lines”, at http://xahlee.org/
UnixResource_dir/writ/hard-wrap.html
----
This post is archived at
http://xahlee.org/UnixResource_dir/writ/tabs_vs_spaces.html
☄