This document contains guides that I defined and follow when building things.
Open issues with any questions, ideas, fixes etc. 😇
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 */
}
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
;
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).
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 {
...
}
I use the ES6 class
for creating classes.
class Person {
constructor (name, age) {
this.name = name;
this.age = age;
}
getName () {
return this.name;
}
}
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
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)
.
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
});
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...`;
Just don't, unless that's the scope of the library.
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 () {...};
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;
}
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
};
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
};
Group the instructions inserting some blank lines where it's needed.
foo(x);
bar(x);
foo(y);
bar(y);
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
;
Double quotes, with some exceptions when single quotes are used.
var foo = "\"Hello\", he said.";
var jQuerySelector = "div.myClass[data-foo='bar']";
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
I use name-it
to generate project names.
I 💖 open-source! I prefer the MIT license.
There are few ways to get help:
- Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
- For bug reports and feature requests, open issues. 🐛
- For direct and quick help, you can use Codementor. 🚀
Have an idea? Found a bug? See how to contribute.
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
-
Starring and sharing the projects you like 🚀
-
—I love books! I will remember you after years if you buy me one. 😁 📖
-
—You can make one-time donations via PayPal. I'll probably buy a
coffeetea. 🍵 -
—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
-
Bitcoin—You can send me bitcoins at this address (or scanning the code below):
1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6
Thanks! ❤️