evernote / javascript-style-guide

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Evernote Javascript style guide

Based off of AirBnB's style guide.

Keeping our JavaScript more consistent

Table of Contents

  1. Strings
  2. Variables
  3. Semicolons
  4. Commas
  5. Declaring Strict Mode
  6. Naming Conventions
  7. Conditional Expressions
  8. Blocks
  9. Tab Spacing
  10. Types
  11. Objects
  12. Arrays
  13. Properties
  14. Accessors
  15. Tools
  16. License

Strings

Use single quotes '' for strings instead of double quotes ""

// bad
var name = "Eva Note";

var fullName = "Eva " + this.lastName;

// good
var name = 'Eva Note';

var fullName = 'Eva ' + this.lastName;

back to top

Variables

Declarations for var as follows:

// bad
var a = 1;
var b = 2;
var c = 'string';

// good
var a = 1,
 b = 2,
 c = 'string';

back to top

Semicolons

Use semicolons ;

// bad
function() {
  return 'Evernote'
}

// good
function() {
  return 'Evernote';
}

back to top

Commas

Don't use leading commas.

// bad
var evernote
  , premium
  , business;

// good
var evernote,
    premium,
    business;

// bad
var product = {
    name: 'Evernote'
  , type: 'App'
  , category: 'Productivity'
};

// good
var product = {
  name: 'Evernote',
  type: 'App',
  category: 'Productivity'
};

// good
function() {
  return 'Evernote';
}

back to top

Naming conventions

  • Use descriptive naming. Avoid single letter names.
// bad
function t() {
  // do something...
}

// good
function test() {
  // do something...
}
  • Use camelCase when naming objects, functions, and instances.
// bad
var OBject = {};
var heres_an_object = {};
function c() {}
var u = new user({
  name: 'Evernote';
});

// good
var thisIsMyObject = {};
function thisIsMyFunction() {}
var user = new User({
  name: 'Evernote';
});

back to top

Declaring Strict Mode

Declare 'use strict' in all your JavaScript files.

back to top

##Conditional Expressions

Use === and !==

  // bad
  var name = 'Eva Note';
  if(name == 'Eva Note'){}

  if(name != 'Eva Note'){}

  // good
  var name = 'Eva Note';
  if(name === 'Eva Note'){}

  if(name !== 'Eva Note'){}

back to top

##Blocks

Use braces when there are multi-line blocks.

  // bad
  if (name)
  return false;

  // good
  if (name) return false;

  // bad
  if (name){return false};

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

back to top

##Tab Spacing

Use two spaces for tabs

  // bad
  function() {
  ∙∙∙∙return true;
  }

  // good
  function() {
  ∙∙return true;
  }

back to top

Types

  • Primitives: When you access a primitive type you work directly on its value

    • string
    • number
    • boolean
    • null
    • undefined
    var foo = 1,
        bar = foo;
    
    bar = 9;
    
    console.log(foo, bar); // => 1, 9
  • Complex: When you access a complex type you work on a reference to its value

    • object
    • array
    • function
    var foo = [1, 2],
        bar = foo;
    
    bar[0] = 9;
    
    console.log(foo[0], bar[0]); // => 9, 9

back to top

Objects

  • Use the literal syntax for object creation.

    // bad
    var item = new Object();
    
    // good
    var item = {};
  • Don't use reserved words as keys. It won't work in IE8.

    // bad
    var superman = {
      default: { clark: 'kent' },
      private: true
    };
    
    // good
    var superman = {
      defaults: { clark: 'kent' },
      hidden: true
    };
  • Use readable synonyms in place of reserved words.

    // bad
    var superman = {
      class: 'alien'
    };
    
    // bad
    var superman = {
      klass: 'alien'
    };
    
    // good
    var superman = {
      type: 'alien'
    };

back to top

Arrays

  • Use the literal syntax for array creation

    // bad
    var items = new Array();
    
    // good
    var items = [];
  • If you don't know array length use Array#push.

    var someStack = [];
    
    
    // bad
    someStack[someStack.length] = 'abracadabra';
    
    // good
    someStack.push('abracadabra');
  • When you need to copy an array use Array#slice. jsPerf

    var len = items.length,
        itemsCopy = [],
        i;
    
    // bad
    for (i = 0; i < len; i++) {
      itemsCopy[i] = items[i];
    }
    
    // good
    itemsCopy = items.slice();
  • To convert an array-like object to an array, use Array#slice.

    function trigger() {
      var args = Array.prototype.slice.call(arguments);
      ...
    }

back to top

Properties

  • Use dot notation when accessing properties.

    var evernote = {
      elephant: true,
      remember: true
    };
    
    // bad
    var isElephant = evernote['elephant'];
    
    // good
    var isElephant = evernote.elephant;
  • Use subscript notation [] when accessing properties with a variable.

    var evernote = {
      elephant: true,
      remember: true
    };
    
    function getProp(prop) {
      return evernote[prop];
    }
    
    var isElephant = getProp('elephant');

back to top

Accessors

  • Accessor functions for properties are not required

  • If you do make accessor functions use getVal() and setVal('hello')

    // bad
    elephant.age();
    
    // good
    elephant.getAge();
    
    // bad
    elephant.age(25);
    
    // good
    elephant.setAge(25);
  • If the property is a boolean, use isVal() or hasVal()

    // bad
    if (!elephant.age()) {
      return false;
    }
    
    // good
    if (!elephant.hasAge()) {
      return false;
    }

back to top

Tools

Sublime

Grunt

Gulp

back to top

License

(The MIT License)

Copyright (c) 2014 Airbnb

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

back to top

};

About