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.

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;

1

u/pyr3 Mar 27 '15

This depends on your alignment style. E.g.:

{key0: value,
 key1: value,
 key2: value}

can't use tabs to align. Also, this follows PEP-8 in Python:

func_call(arg0,
          arg1)

3

u/Asmor Mar 27 '15

Wow. That's ugly as sin. That looks like what I imagine python folks would write to try and cope with a c-style language.

Format your code properly, and you can indent with tabs and it won't look like shit.

{
	key0: value,
	key1: value,
	key2: value
}

func_call(
	arg0,
	arg1
)

2

u/pyr3 Mar 27 '15 edited Mar 27 '15

A better example of the second one:

class User(db.Model):
    id = db.Column(db.Integer)
    group_id = db.Column(db.Integer)
    group = db.relationship('Group', lazy='join', backref=db.backref('users', use_list=True))

Should it look like this:

class User(db.Model):
    id = db.Column(db.Integer)
    group_id = db.Column(db.Integer)
    group = db.relationship(
        'Group',
        lazy='join',
        backref=db.backref('users', use_list=True)
    )

or this:

class User(db.Model):
    id = db.Column(db.Integer)
    group_id = db.Column(db.Integer)
    group = db.relationship('Group', lazy='join',
                            backref=db.backref('users', use_list=True))

Both of those examples fit within PEP-8, so there's not need to start language wars like this:

That looks like what I imagine python folks would write to try and cope with a c-style language.

I would argue that good programmers adjust their style depending on the language. For example, use snake_case in Python, and camelCase in JavaScript for variable names.

[Also, arguably the ugliest Python are the libraries that are written in Java style -- e.g. unittest in the standard library -- since they look nothing like the style of everything else.]

→ More replies (0)

1

u/cresquin Mar 28 '15

let me get to that piece of code:

left-arrow left-arrow left-arrow left-arrow left-arrow left-arrow left-arrow left-arrow