- Code Functions As Much As Possible
- Compose Large Abstractions From Small Modules
- Write Declarative Code
- NPM Everywhere
- Require & Export on Top
- Avoid
new
,prototype
andthis
- While Loops Are Faster and Simpler
- Declare Variables Outside of Statements
- Create functions that does only one thing.
- Avoid declaring functions with
var
statement
foo()
bar()
function foo(){}
function bar(){}
- Create modules that does one thing.
- Create repositories for modules and publish/open source them on NPM and Github if possible.
- Never couple your code with any dependency doing more than it should
- Avoid frameworks
A good read about this topic: http://substack.net/many_things
- Write less code
- Write code that tells what it does briefly
- Choose simple & short variable names
- Modularize your JS in CommonJS.
- Use browserify
- Avoid other module systems like RequireJS.
var foo = require('foo')
var bar = require('./bar')
module.exports = {
qux: qux,
corge: corge
}
function qux(){}
function corge(){}
Functions returning objects will let you avoid maintaining "this" arguments and also let you benefit from functional programming.
var smith = Child('Joe', 'Smith')
function Child(parent, name){
return {
parent: parent,
name: name
}
}
And keep your code available for functional programming:
var JoeChild = partial(Child, 'Joe')
var smith = JoeChild('Smith')
var kat = JoeChild('Kat')
smith.parent
// => Joe
kat.parent
// => Joe
Declaring variables inside of a statement will make your code work and look less consistent. For example, below code will fail when you run it:
foobar
With the error of "ReferenceError: foobar is not defined". But following code will not fail;
foobar
if (false) {
var foobar
}
For having better consistency, declare the variable on top of your statement;
var foobar
if (false) {
foobar = whatever
}
While loops are faster than for loops, and they're much simpler. Here is an example;
var i = 10
while (i--) {
console.log(i)
}
This will output from 9 to 0. If you'd like the other way:
var i = -1
while (++i < 10) {
console.log(i)
}