Skip to content

Latest commit

 

History

History
298 lines (227 loc) · 4.63 KB

File metadata and controls

298 lines (227 loc) · 4.63 KB

Node Style Guide

This style guide is intended to increase the readability of all code in this respository by ensuring that certain standards are followed.

Formatting

Line Length

The maximum length of a single line of code should not exceed 80 characters.

Indentation

Indentation should always be 4 spaces. The tab key should not be used to indent code.

Whitespace and newlines

\n should always be used as the newline character. \r\n should not be used.

There should be no trailing whitespace or newlines at the end of any line of code or at the end of a file.

Spacing

Place one space after an if or else keyword and after before the opening parenthesis for the block.

Correct:

if (isTrue) {
  // execute code
}

Incorrect:

if(isTrue){
  // execute code
}
Braces and Parentheses

Opening braces should be placed on the same line as the expression that precedes them.

Correct:

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

Incorrect:

if (true)
{
  console.log('Incorrect');
}
Closing Parentheses

Closing parentheses and braces should be grouped on the same line.

Correct:

res.json({
  user: req.user,
  token: token
});

Incorrect:

res.json(
{
  user: req.user,
  token: token
}
);
Code Blocks

Use braces for multi-line blocks of code.

Correct:

if (true) return true;

if (true) {
  return true;
}

function () {
  return true;
}

Incorrect:

if (badFormatting)
  return false;

function () { return false; }
If-else Blocks

For if-else blocks, put else on the same line as the end of the corresponding if statement.

Correct:

if (isTrue) {
  console.log('true');
} else {
  console.log('not true);
}

Incorrect:

if (isTrue) {
  console.log('true');
} 
else {
  console.log('not true);
}

Comments

Multi-Line Comments

Use /* ... */ for comments spanning multiple lines.

Single-Line Comments

Use // for comments on a single line.

Indentation

Comment indentation should match that of the block of code it is inside.

Correct:

if (true) {
  // prints that it is true
  console.log('true');
}

Incorrect:

if (true) {
// prints that it is true
  console.log('true');
}

if (true) { 
    // prints that it is true
  console.log('true');
}
TODO

Use // TODO: to specify areas of code that need attention in the future.

Variables

Camel Case

Camel case should be used for all variable declarations. Upper or lower camel case can be used depending on the situation.

Correct:

const newVariable = 1;

Incorrect:

const new_variable = 1;
const new-variable = 1;
4 Word Variable Names

Variable names should not exceed 4 words, for readability.

Correct:

const newPerson = 'Evan';

Incorrect:

const newPersonNameDataInitial = 'Evan';
Match Number of Data Type

Variables should match the number of the object type they represent.

Correct:

const user = 'Evan';
const users = [];
One Variable Per Var

Only declare one variable per var keyword.

Correct:

var firstValue = 1;
var otherValues = [10, 20];

Incorrect:

var firstValue = 1,
    otherValues = [10, 20];
Object and Array Formatting

For objects and arrays, place opening and closing syntax on the same line, if it is a short declaration.

Correct:

var object = ['good', 'object'];

Incorrect:

var oneObject = [
  'bad', 'object'
];

var anotherObject = [
  'still',
  'bad',
  'object'
];

Objects

Literal Syntax

Use literal syntax to create objects.

Correct:

const object = {};

Incorrect:

const object = new Object();

Arrays

Literal Syntax

Use literal syntax to create arrays

Correct:

const array = [];

Incorrect:

const array = new Array();

Functions

Keep Functions Short

Functions should be kept as short as possible to increase readability. Any major service should be broken out into a seperate function.

Properties

Dot Notation

Use dot notation to access the properties of variables.

Correct:

var event = {
  name: 'Fun Event',
  time: '6:00pm',
};

const eventTime = event.time;

Incorrect:

var event = {
  name: 'Fun Event',
  time: '6:00pm',
};

const eventTime = event['time'];

References

This style guide was influenced by several well-written style guides. The most influential were:

https://github.com/airbnb/javascript

https://github.com/felixge/node-style-guide