Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
135 lines (109 sloc) 2.98 KB

JavaScript Style

Please see feross/standard's RULES.md for a general guide of form to follow for javascript. One of the fundamental concepts is the elimination of semicolons but there are some caveats.

Beside that:

  • functions and class methods should be in camelCase
  • pure functions should be in their own file and have their own unit tests
  • files that export classes, static or instantaible, should have filenames UpperCamelCase
  • do not use var anymore (we have const and let)
  • always leave newline between function or method declarations
  • prefer null over an empty string
  • always throw (propagate) if you define a .catch()

import/require of classes should be in UpperCamelCase, even if they are not:

// OK
const Path = require('path')

// Avoid
const path = require('path')

Avoid multiple named imports on one line:

// OK
import {
  thing1,
  thing2
} from 'whatever';

// Avoid
import { thing1, thing2 } from 'whatever'

Avoid defining anonymous static object as function parameter. This improves debugging and avoid Promise-related clutter but I understand the shortcut:

// OK
const myObject = {
  hello: 'Hi!'
}
someFunc(myObject);

// Avoid
someFunc({
  hello: 'Hi!'
})

Avoid checking boolean values with expressions that check for value:

const myBoolean = true;

// OK
if (myBoolean) {}

// Avoid
if (myBoolean === true) {}

Prefer => when using Promises or callbacks.

Use indentation with Promises to improve readability:

// OK
myFuncThatReturnsPromise()
  .then(res => someTransform(res, 'something'))
  .then(console.log)
  .catch(console.error)

// OK
SomeStaticClass
  .myFuncThatReturnsPromise()
  .then(res => {
    console.log(res)
    return someTransform(res, 'something')
  })
  .then(console.log)
  .catch(console.error)

// Avoid - `then` on same line
myFuncThatReturnsPromise().then(res => someTransform(res, 'something'))
  .then(console.log)
  .catch(console.error)

// Avoid - not using arrow and not properly indented
myFuncThatReturnsPromise().then(function (res) {
  return someTransform(res, 'something')
})
.then(console.log)
.catch(console.error)

Nice Things

  // Best
  return {
    user:         state.user,
    channel:      state.channel,
    newuser:      state.newuser,
    creditcard:   state.creditcard,
    coupon:       state.coupon,
    queryparams:  state.queryparams,
  }

  // OK
  return {
    user: state.user,
    channel: state.channel,
    newuser: state.newuser,
    creditcard: state.creditcard,
    coupon: state.coupon,
    queryparams: state.queryparams,
  }

TIPS

  • if you're using let you may want to reconsider your function design

This is ugly, why the JS folks do not want to improve this potentially awesome expression is beyond me.

var select = (x => {
  switch(x) {
    case 'A': return 0
    case 'B': return 1
    default:  return 9
  }
})(y)