📖 This document contains guides that I defined and follow when building things. ❤️
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
CONTRIBUTING.md
LICENSE
README.md
package.json

README.md

📖 Ionică Bizău's Code Style ❤️

Patreon PayPal AMA Version Downloads Get help on Codementor

This document contains guides that I defined and follow when building things.

Open issues with any questions, ideas, fixes etc. 😇

Contents

Variable declarations 📝

Variables 💬

Using var in general or let when they should be accesible only in specific blocks (e.g. if).

// One declaration
var foo = 1;

// Multiple declarations
var foo = 1
  , bar = "Hello World"
  , anotherOne = [{ foo: "bar" }]
  ;

if (...) {
   let baz = 42;
  /* do something with baz */
}

Constants 🚩

Using const. The constant names are written with UPPERCASE letters. I also use const when including libraries using require and when they should not be changed. In this case, the names will not be with caps.

// Dependencies
const http = require("http")
   , fs = require("fs")
   , EventEmitter = require("events").EventEmitter
// Constants
const PI = Math.PI
    , MY_CONSTANT = 42
    ;

Globals 🌍

I define globals when there is no commonjs environment (this is actually handled by dist-it. When I manually define globals, I do that using window.MyGlobal (on the client) and global.MyGlobal (on the server).

Semicolons ✏️

I use semicolons. Almost always.

var foo = 1;
function bar (x) {
    var someMethod = function (m) {
        console.log(m);
    };
    return { y: x, foo: someMethod };
}
class Foo {
    ...
}

Method and property definitions 📎

I use the ES6 class for creating classes.

class Person {
    constructor (name, age) {
        this.name = name;
        this.age = age;
    }
    getName () {
        return this.name;
    }
}

Deleting properties

I nullify the properties when that's fine:

var foo = {
    bar: 42
};
foo.bar = null;

However, I use the delete keyword when I really want to delete them.

delete foo.bar;

eval()

eval is evil. 😡 Do not use it. However I use it in some test files and in places where I have to execute the JavaScript code provided by the user.

For converting strings to JSON, use JSON.parse(strObj).

Iterating objects and arrays

For arrays, most of times, I use the forEach function:

arr.forEach(c => {
    // do something
});

However, using for loops is fine too:

for (var i = 0; i < arr.length; ++i) {
    for (var ii = 0; ii < arr[i].length; ++ii) {
        ...
    }
    ...
}

For objects, I use the following style:

Object.keys(obj).forEach(k => {
    var cValue = obj[k];
    // do something
});

To simplify this, I created iterate-object, which abstracts this functionality:

const iterateObject = require("iterate-object");
iterateObject(obj, (value, key) => {
    // do something
});

Multiline strings 🎸

I use backticks to create multiline strings:

var multiLineStr = `Lorem ipsum dolor sit amet, consectetur adipisicing elit
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat
New line again...`;

Modifying prototypes of built-in objects 💩

Just don't, unless that's the scope of the library.

Naming things 💭

Using camel case notation for variables, in general. For constructors I capitalize the variable name (e.g. EventEmitter).

// Node.JS require
const fs = require("fs")
    , events = require("events")
    , EventEmitter = events.EventEmitter
    ;

// Local variables
var x = 1
  , twoWords = "Hello World"
  ;

// Functions
function fooBar () {...}

// Classes
class Person {
    constructor (name, age) {
        this.name = name;
        this.age = age;
    }
    getName () {
        return this.name;
    }
}
// Object fields
var obj = {
    full_name: "Johnny B."
  , age: 20
};
obj.methodA = function () {...};

Curly braces

Open the curly brace at the end of the line. Always put the instructions between curly braces, even there is only one instruction.

if (expr) {
    instr;
} else {
    instr2;
    instr3;
}

Array and Object Initializers 📁

See examples.

// Arrays
var arr = [1, 2, 3, 4];

var lotOfElms = [
    1, 2, 3, 4, 5, 6, 7
  , 8, 9, 10, 11, 12, 13
  , 14, 15, 16, 17, 18
];

var bigElms = [
    "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod."
  , "Tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim."
  , "Veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea."
  , "Commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"
];

// Objects
var obj = { a: 1 };

var obj1 = {
    full_name: "Johnny B."
  , age: 20
};

Commas

Put commas at the beginning of the line, not at the end.

var x = 1
  , y = 2
  ;

const C_1 = 42
    , C_2 = -42
    ;

var obj = {
    x: 1
  , y: 2
};

Blank lines

Group the instructions inserting some blank lines where it's needed.

foo(x);
bar(x);

foo(y);
bar(y);

Binary and Ternary operators

See examples.

var foo = someObj
    .method()
    .method2()
    .method3()
    ;

var a = cond ? v1 : v2;

var b = long_condition_here
        ? v1 : v2
        ;

var c = another_long_condition_here
        ? with_some_long_value
        : or_another_some_long_value
        ;

Quotes 💬

Double quotes, with some exceptions when single quotes are used.

var foo = "\"Hello\", he said.";
var jQuerySelector = "div.myClass[data-foo='bar']";

Comments 🎶

Put relevant comments. The comments start with uppercase letter.

// Dependencies
const lib1 = require("lib1")
    , lib2 = require("lib2")
    ;

// Constants
const FOURTY_TWO = 42;

Use JSDoc comments for functions and methods.

/**
* sum
* Calculates the sum of two numbers.
*
* @name sum
* @function
* @param {Number} a The first number,
* @param {Number} b The second number.
* @return {Number} The sum of the two numbers.
*/
function sum (a, b) {
  return a + b;
};

I use the blah tool to generate documentation.

$ npm install -g blah
$ blah --readme
$ blah --docs some-file.js

Project naming

I use name-it to generate project names.

Project licenses

I 💖 open-source! I prefer the MIT license.

😋 How to contribute

Have an idea? Found a bug? See how to contribute.

💰 Donations

Another way to support the development of my open-source modules is to set up a recurring donation, via Patreon. 🚀

PayPal donations are appreciated too! Each dollar helps.

Thanks! ❤️

📜 License

MIT © Ionică Bizău