TrabalhosGratuitos.com - Trabalhos, Monografias, Artigos, Exames, Resumos de livros, Dissertações
Pesquisar

Node.js Style Guide

Artigo: Node.js Style Guide. Pesquise 860.000+ trabalhos acadêmicos

Por:   •  2/9/2014  •  1.030 Palavras (5 Páginas)  •  221 Visualizações

Página 1 de 5

Newlines

Use UNIX-style newlines (\n), and a newline character as the last character of a file. Windows-style newlines (\r\n) are forbidden inside any repository.

No trailing whitespace

Just like you brush your teeth after every meal, you clean up any trailing whitespace in your JS files before committing. Otherwise the rotten smell of careless neglect will eventually drive away contributors and/or co-workers.

Use Semicolons

According to scientific research, the usage of semicolons is a core value of our community. Consider the points of the opposition, but be a traditionalist when it comes to abusing error correction mechanisms for cheap syntactic pleasures.

80 characters per line

Limit your lines to 80 characters. Yes, screens have gotten much bigger over the last few years, but your brain has not. Use the additional room for split screen, your editor supports that, right?

Use single quotes

Use single quotes, unless you are writing JSON.

Right:

var foo = 'bar';

Wrong:

var foo = "bar";

Opening braces go on the same line

Your opening braces go on the same line as the statement.

Right:

if (true) {

console.log('winning');

}

Wrong:

if (true)

{

console.log('losing');

}

Also, notice the use of whitespace before and after the condition statement.

Declare one variable per var statement

Declare one variable per var statement, it makes it easier to re-order the lines. However, ignore Crockford when it comes to declaring variables deeper inside a function, just put the declarations wherever they make sense.

Right:

var keys = ['foo', 'bar'];

var values = [23, 42];

var object = {};

while (keys.length) {

var key = keys.pop();

object[key] = values.pop();

}

Wrong:

var keys = ['foo', 'bar'],

values = [23, 42],

object = {},

key;

while (keys.length) {

key = keys.pop();

object[key] = values.pop();

}

Use lowerCamelCase for variables, properties and function names

Variables, properties and function names should use lowerCamelCase. They should also be descriptive. Single character variables and uncommon abbreviations should generally be avoided.

Right:

var adminUser = db.query('SELECT * FROM users ...');

Wrong:

var admin_user = db.query('SELECT * FROM users ...');

Use UpperCamelCase for class names

Class names should be capitalized using UpperCamelCase.

Right:

function BankAccount() {

}

Wrong:

function bank_Account() {

}

Use UPPERCASE for Constants

Constants should be declared as regular variables or static class properties, using all uppercase letters.

Node.js / V8 actually supports mozilla's const extension, but unfortunately that cannot be applied to class members, nor is it part of any ECMA standard.

Right:

var SECOND = 1 * 1000;

function File() {

}

File.FULL_PERMISSIONS = 0777;

Wrong:

const SECOND = 1 * 1000;

function File() {

}

File.fullPermissions = 0777;

Object / Array creation

Use trailing commas and put short declarations on a single line. Only quote keys when your interpreter complains:

Right:

var a = ['hello', 'world'];

var b = {

good: 'code',

'is generally': 'pretty',

};

Wrong:

var a = [

'hello', 'world'

];

var b = {"good": 'code'

, is generally: 'pretty'

};

Use the === operator

Programming is not about remembering stupid rules. Use the triple equality operator as it will work just as expected.

Right:

...

Baixar como (para membros premium)  txt (7.3 Kb)  
Continuar por mais 4 páginas »
Disponível apenas no TrabalhosGratuitos.com