r/javascript • • Mar 27 '15

Airbnb JavaScript Style Guide - A mostly reasonable approach to JavaScript

https://github.com/airbnb/javascript/blob/master/README.md
315 Upvotes

158 comments sorted by

View all comments

3

u/brotherwayne Mar 27 '15

Mostly?

38

u/mrkipling Mar 27 '15

Yes - they suggest a tab indent of 2 spaces instead of the obviously-correct value of 4. Hence, mostly reasonable :)

19

u/Asmor Mar 27 '15

Also, using spaces for indentation instead of tabs.

You can have my \t when you pry it out of my cold, dead hands.

8

u/honestbleeps Reddit Enhancement Suite Mar 27 '15

I feel like you and I are on the losing end of this debate. Everyone's all spaces now. I'm always the outlier with my tabs.

7

u/Asmor Mar 27 '15

My company's all tabs (yay!), even for alignment (d'oh) and tab width is 8 (wtf)

Personally, I like tabs for indentation and spaces for alignment.

5

u/[deleted] Mar 27 '15

Personally, I like tabs for indentation and spaces for alignment.

I personally thought this was the result of the Great Tabs/Spaces Wars. I'm astonished that this is still a thing.

3

u/nschubach Mar 27 '15

I like tabs for indentation and no special alignment. There's no need to align stuff if you keep your methods trim and refactor things that feel like they need to be next-line aligned.

3

u/lolmeansilaughed Mar 28 '15

Agreed on the alignment thing. Why would I spend time hitting the space bar over and over again just because I renamed a variable and now the equals signs all "need" to be five more spaces out? Keep your code orderly, to a fault even, but aesthetics has no place in code formatting.

2

u/milkeater Mar 27 '15

I took a data scientist certification course where they recommended 8 spaces to discourage excessive nesting....dumbest thing I ever heard. Lost all credibility in my opinion.

Four seems like more than enough...

2

u/eusx Mar 28 '15

FYI, the Linux kernel coding style said

Now, some people will claim that having 8-character indentations makes the code move too far to the right, and makes it hard to read on a 80-character terminal screen. The answer to that is that if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

In short, 8-char indents make things easier to read, and have the added benefit of warning you when you're nesting your functions too deep.
Heed that warning.

→ More replies (1)

6

u/munificent Mar 28 '15

You tab freaks brought it upon yourself by having tab stops default to eight spaces on some editors and websites.

→ More replies (1)

3

u/eridal Mar 27 '15

TL;DR: using tabs right requires more effort

You can use tabs, but it please take care of using them in the right place: only for "tabulate", not to "align" code .. that will only work on your screen.

Let me show an example:

$(ctx).click(function() {
  $(this).attr(...)
         .css(...)
         ;
});

and now how to properly indent with tabs

$(ctx).click(function() {
→$(this).attr(...)
→·······.css(...)
→·······;
});

6

u/Asmor Mar 27 '15

Agree that spaces should be used for alignment, but disagree with your interpretation. The .css call shouldn't be aligned, it should be indented. And the semi colon should be at the same level of indentation as the first line.

$(ctx).click(function() {
→$(this)
→→.attr(...)
→→.css(...)
→;
});

3

u/eridal Mar 27 '15

yeah, completely agree with your interpretation! this makes future diffs/patches not mess with surrounding lines

that said, in my example I was trying to come up with a quick example of where to put spaces before tabs .. can you think of a better example than mine?

3

u/Asmor Mar 27 '15

Well, pretty simple.

Spaces should never, ever be before the first non-whitespace character on a line. Tabs should never, ever be after the first non-whitespace character on a line.

Put another way, all lines should match the following regex:

^[\t]*\S[^\t]*$|^$

(i.e. a blank line, or 0 or more tabs followed by 1 a non-white-space character followed by 0 or more non-tab characters)

Example of usage of spaces for alignment:

var clinton = 42;
var bush    = 43;
var obama   = 44;
→ More replies (6)
→ More replies (1)
→ More replies (3)

7

u/theillustratedlife Mar 27 '15 edited Mar 27 '15

Like many people, I used 4 spaces for years, but 2 spaces has definitely been winning in JS culture. It's been the standard at the last few gigs I've consulted for and is also standard on many OSS projects.

After using it for a while I like it. It's especially nice for nested ternaries:

lorem
  ? lorem.ipsum
    ? lorem.ipsum()
    : lorem.dolor()
  : null

or ternaries with nested function calls:

return routerState.has("productID") && routerState.has("variantID")
  ? Store.getOrFetch(
      {
        "productID":  routerState.get("productID"),
        "variantID":  routerState.get("variantID")
      }
    )
  : null

Isn't it nice how those lay on the grid?

24

u/acoard Mar 27 '15

It's especially nice for nested ternaries:

Nothing is nice about nested ternaries.

→ More replies (1)

4

u/tipdbmp Mar 27 '15

It's easier to debug (you can set breakpoints) when if/else-if/else statements are being used.

var result;

if (!lorem) {
    result = null;
}
else if (!lorem.ipsum) {
    result = lorem.dolor();
}
else {
    result = lorem.ipsum();
}

But if you insist on using nested ternary operators, then in my opinion it's better to format them in a table:

var result = !lorem        ? null
           : !lorem.ipsum  ? lorem.dolor()
           :                 lorem.ipsum()
           ;

Note that in both cases the "good" outcome goes last and it's preceded by all the "error" checking (that's generally a good advice).

2

u/nschubach Mar 27 '15

I fall in the camp that nested ternaries are bad and should be refactored to make more sense to the casual reader so setting breakpoints on indented ternaries will never be in my code.

2

u/Asmor Mar 28 '15

I find use cases for ternaries exceedingly rare. I only use them in trivial cases, like:

var foo = (useA) ? a : b;

Even then, I'm a lot more likely to do:

var foo = b;
if (use A) {
    foo = a;
}

Pretty much only really use them when the case is trivial and there's an aesthetic benefit to keeping the whole assignment on one line (e.g. it's just one in a long list of things being assigned).

2

u/alamandrax Mar 28 '15

I've banned ternary usage except for the trivial case you've cited. The code is getting a lot more readable now.

(someCondition) ? executeApi() : variable = assignSomething;
var myBool = (testVarExistance) ? null : testVarExistance;

to illustrate some choice examples. Don't get me started on 3 levels of nested termaries.

1

u/benihana react, node Mar 28 '15

Not that I think this is good practice, but using 4 spaces makes single var'd variables line up nicely:

var sup_bro,
    yo_fam,
    easy_guy;

2

u/DaemonXI Mar 27 '15

Normally I like four spaces, but JavaScript is so heavily nested with callbacks that I think using two spaces actually makes the code more readable.

11

u/anarchy8 Mar 27 '15

Use promises. Problem solved

7

u/redditor0117 Mar 28 '15

Problem resolved

FTFY

3

u/mrkipling Mar 27 '15

All the more reason to use four spaces, so you don't indent too heavily. Also, ES6 promises.

I do see your point though.

→ More replies (15)