
Ask HN: Why do the majority of JS developers prefer spaces over tabs? - iLoch
I know this is a hotly debated topic, but I have to ask. Why is it that specifically for Javascript, 4 in 5 developers prefer to use spaces instead of tabs[1]? I&#x27;ve decided to switch from using tabs to 2 spaces because I&#x27;d rather convenience other developers than burden them, but I still don&#x27;t understand the reason. Tabs take up less space in terms of bytes, and are dynamic width allowing the user to set the width to their personal preference. Sounds like a perfect solution to developers agreeing on a specific number of times to hit the space key. So why are spaces so popular?<p>[1] http:&#x2F;&#x2F;sideeffect.kr&#x2F;popularconvention&#x2F;#javascript
======
bjourne
Because spaces are rendered the same everywhere. Suppose I write this code,
indenting using spaces.

    
    
        im_a_long_method(argument_no_1,
                         set_indentation_true(...),
                         no_string_cutoff,
                         whatever)
    

Now suppose I used tabs to indent and my editors tab width is 4 spaces while
your is 8 spaces, then the above would look like:

    
    
        im_a_long_method(argument_no_1,
                                          set_indentation_true(...),
                                          no_string_cutoff,
                                          whatever)
    

Since not every editor agrees on how wide a tab character is, you get crappy
alignments. Therefore use spaces.

~~~
dragonwriter
> Because spaces are rendered the same everywhere. Suppose I write this code,
> indenting using spaces.

Then it'll look wrong in a proportional font; indenting and aligning with non-
indented text are different things. Either spaces or tabs _indent_
consistently when used consistently in a source file. Alignment cannot be
acheived consistently (with spaces or tabs) unless you also assume a
monospaced font, or (with tabs) have elastic tabstops.

~~~
J_Darnley
I really don't get the point of tabs for visual layout in any kind of document
be it plain text, source code, or a word processor document. They never seemed
to work right for me.

I remember using the tab key to enter tabs back in school to make something
like an informal, 2-or3-column table (or whatever I was actually doing). This
would have been in Word. Things would line up pretty until I wanted to change
some text. If I made it longer I would need to add another tab because the tab
had shrunk to nothing. If I made it shorter the tab would jump into the
previous section and no longer line up with the other lines.

As far as I can see, the same will happen with this source example if it were
using tabs and a proportional font.

~~~
dragonwriter
Elastic tabstops are so named because the width adjusts based on the content,
avoiding that problem.
[http://nickgravgaard.com/elastictabstops/](http://nickgravgaard.com/elastictabstops/)

------
MalcolmDiggs
In days gone by (pre git) a lot of devs used to share files via FTP; and the
FTP clients back then were garbage; if you were sending files from a unix-like
computer to a windows one and back and forth, your tabs might get converted to
spaces by the client. Developers would end up with slightly different copies
of the same file.

Many people decided to standardize various things (like the use of spaces for
indentation) to compensate for this.

These days not a huge deal.

------
dragonwriter
> Tabs take up less space in terms of bytes

Optimizing for size in the primary format for editing isn't that important.
For deployment, sure, but you're going to minify for that anyway, right? So
spaces vs. tabs makes no difference for that.

------
michaelmior
I think the fact that tabs take up fewer bytes is basically irrelevant. For
anything that matters, the JS will be minified anyway, removing the
whitespace.

------
vishalchandra
I decided to use spaces instead of tabs and convert a tab to two spaces
because while typing I sometimes alternate between using the space bar and the
tab while indenting something or editing code. So it is much easier for me to
be consistent as an individual dev with spaces.

------
aldnoahzero
I prefer spaces (4 spaces to be precise), because I can write like this:

var abc = something,

    
    
        xyz = somethingElse,
    
        stringOfAbc = abc.toString();
    
    

the variable names are neatly aligned on the left side!

------
motyar
I remember once I got an error in an python script, and had to convert all
tabs to space. Btw I am JS developer.

~~~
lutusp
This experience is more common than most people realize. A Python program
mysteriously stops working, especially after being transferred to a new
platform, and the most experienced hands will suspect a mixture of tabs and
spaces, and/or tabs being treated differently than expected.

------
lutusp
> Why do the majority of JS developers prefer spaces over tabs?

Because, with a fixed-pitch font such as is used in programming, the meaning
of a space is unambiguous (unlike tabs). Also, having two indistinguishable
whitespace characters is bad, and mixing them in a single file is worse. So
one chooses the least problematic of the two.

------
jdimov
I'm a Python dev, and it's the same in Python world. I prefer spaces, and so
do most other Python devs. This is in line with one of the Python community's
core beliefs - "Explicit is better than implicit"

~~~
Zergy
> "Explicit is better than implicit"

Seems like a strong argument for sticking with strongly typed languages.

~~~
kyllo
Python _is_ strongly typed.

Dynamic typing and weak typing are not the same thing.

Python:

    
    
        >>>1 + "1"
        Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        TypeError: unsupported operand type(s) for +: 'int' and 'str'
    

PHP:

    
    
        >1 + "1"
        2

