/javascript

JavaScript Style Guide

MIT LicenseMIT

INDEC JavaScript Style Guide() {

A mostly reasonable approach to JavaScript

Note: this guide is base on airbnb guide Airbnb JavaScript Style Guide, it reinforces the most important rules and overwrites some of them.

Table of Contents

  1. Functions
  2. Blocks
  3. WhiteSpace
  4. ES7

Functions

  • 1.1 Method Naming: Methods should be named with a verb in infinitive.
// bad
function validating() {
    //...
};

// good
function validate() {
    // ...
};

Blocks

  • 2.1 Use braces with all single-line blocks.
// bad
if (test) return false;

// good
if (test) {
    return false;
}

⬆ back to top

Whitespace

  • 3.1 Use soft tabs (space character) set to 4 spaces.
// bad
function foo() {
∙let name;
}

// bad
function bar() {
∙∙let name;
}

// good
function baz() {
∙∙∙∙let name;
}

⬆ back to top

ES7

  • 4.1 Async/Await: use async/await instead of then (promises)
// bad
const fetchUser = url => getAuthHeader().then(
    authorization => fetch(url, { headers: {authorization} })
).then(
    response => response.json
).then(
    data => new User(data.user)
)catch(
    err => console.log(err);
);

// good
const fetchUser = async url => {
    try {
        const response = await fetch(url, {
            credentials: 'same-origin',
            headers: {
                authorization: await getAuthHeader()
            }
        });
        const data = await response.json();
        return new User(data.user);
    } catch (err) {
        console.log(err);
        throw err;
    }
}

⬆ back to top

}