node-style-guide

A guide for styling your node.js / JavaScript code.

Stars
32

Nodejs Style Guide(by felixge)

.editorconfig . Node.js Style Guide .editorconfig , .

2

2 , Tabs --.

Unix (\n), . Windows (\r\n).

, JavaScript .

, . , (abusing error correction mechanisms), .

80

80 . , , .

JSON, :

:

var foo = 'bar';

:

var foo = "bar";

.

:

if (true) {
  console.log('winning');
}

:

if (true)
{
  console.log('losing');
}

.

var

var , . , Crockford , .

:

var keys   = ['foo', 'bar'];
var values = [23, 42];

var object = {};
while (keys.length) {
  var key = keys.pop();
  object[key] = values.pop();
}

:

var keys = ['foo', 'bar'],
    values = [23, 42],
    object = {},
    key;

while (keys.length) {
  key = keys.pop();
  object[key] = values.pop();
}

, . .

:

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

:

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

.

:

function BankAccount() {
}

:

function bank_Account() {
}

, .

:

var SECOND = 1 * 1000;

function File() {
}
File.FULL_PERMISSIONS = 0777;

:

const SECOND = 1 * 1000;

function File() {
}
File.fullPermissions = 0777;

/

, .

:

var a = ['hello', 'world'];
var b = {
  good: 'code',
  'is generally': 'pretty',
};

:

var a = [
  'hello', 'world'
];
var b = {"good": 'code'
        , is generally: 'pretty'
        };

===

. === , .

:

var a = 0;
if (a !== '') {
  console.log('winning');
}

:

var a = 0;
if (a == '') {
  console.log('losing');
}

, .

:

var foo = (a === b)
  ? 1
  : 2;

:

var foo = (a === b) ? 1 : 2;

.

:

var isValidPassword = password.length >= 4 && /^(?=.*\d).{4,}$/.test(password);

if (isValidPassword) {
  console.log('winning');
}

:

if (password.length >= 4 && /^(?=.*\d).{4,}$/.test(password)) {
  console.log('losing');
}

. (slide), , . , 15 .

if , .

:

function isPercentage(val) {
  if (val < 0) {
    return false;
  }

  if (val > 100) {
    return false;
  }

  return true;
}

:

function isPercentage(val) {
  if (val >= 0) {
    if (val < 100) {
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

, :

function isPercentage(val) {
  var isInRange = (val >= 0 && val <= 100);
  return isInRange;
}

, , cpu .

:

req.on('end', function onEnd() {
  console.log('winning');
});

:

req.on('end', function() {
  console.log('losing');
});

, , .

:

setTimeout(function() {
  client.connect(afterConnect);
}, 1000);

function afterConnect() {
  console.log('winning');
}

:

setTimeout(function() {
  client.connect(function() {
    console.log('losing');
  });
}, 1000);

, , .

:

User
  .findOne({ name: 'foo' })
  .populate('bar')
  .exec(function(err, user) {
    return true;
  });

:

User
.findOne({ name: 'foo' })
.populate('bar')
.exec(function(err, user) {
  return true;
});

User.findOne({ name: 'foo' })
  .populate('bar')
  .exec(function(err, user) {
    return true;
  });

User.findOne({ name: 'foo' }).populate('bar')
.exec(function(err, user) {
  return true;
});

User.findOne({ name: 'foo' }).populate('bar')
  .exec(function(err, user) {
    return true;
  });

. (higher level mechanisms) . .

:

// 'ID_SOMETHING=VALUE' -> ['ID_SOMETHING=VALUE', 'SOMETHING', 'VALUE']
var matches = item.match(/ID_([^\n]+)=([^\n]+)/));

// This function has a nasty side effect where a failure to increment a
// redis counter used for statistics will cause an exception. This needs
// to be fixed in a later iteration.
function loadUser(id, cb) {
  // ...
}

var isSessionValid = (session.expires < Date.now());
if (isSessionValid) {
  // ...
}

:

// Execute a regex
var matches = item.match(/ID_([^\n]+)=([^\n]+)/);

// Usage: loadUser(5, function() { ... })
function loadUser(id, cb) {
  // ...
}

// Check if the session is valid
var isSessionValid = (session.expires < Date.now());
// If the session is valid
if (isSessionValid) {
  // ...
}

Object.freeze, Object.preventExtensions, Object.seal, with, eval

.

Requires

Requires , . , , package.json .

Getters setters

setters, , . getters, , length .

JavaScript , .

:

var a = [];
if (!a.length) {
  console.log('winning');
}

:

Array.prototype.empty = function() {
  return !this.length;
}

var a = [];
if (a.empty()) {
  console.log('losing');
}

License

CC BY-SA 3.0