This style guide is intended to increase the readability of all code in this respository by ensuring that certain standards are followed.
The maximum length of a single line of code should not exceed 80 characters.
Indentation should always be 4 spaces. The tab key should not be used to indent code.
\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.
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
}
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 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
}
);
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; }
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);
}
Use /* ... */ for comments spanning multiple lines.
Use // for comments on a single line.
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');
}
Use // TODO: to specify areas of code that need attention in the future.
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;
Variable names should not exceed 4 words, for readability.
Correct:
const newPerson = 'Evan';
Incorrect:
const newPersonNameDataInitial = 'Evan';
Variables should match the number of the object type they represent.
Correct:
const user = 'Evan';
const users = [];
Only declare one variable per var keyword.
Correct:
var firstValue = 1;
var otherValues = [10, 20];
Incorrect:
var firstValue = 1,
otherValues = [10, 20];
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'
];
Use literal syntax to create objects.
Correct:
const object = {};
Incorrect:
const object = new Object();
Use literal syntax to create arrays
Correct:
const array = [];
Incorrect:
const array = new Array();
Functions should be kept as short as possible to increase readability. Any major service should be broken out into a seperate function.
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'];
This style guide was influenced by several well-written style guides. The most influential were: