- Global
- Identation
- Commits
- English
- Task number
- Status
- Message convention
- HTML
- Syntax
- Comments
- Character Encoding
- Attribute Order
- Performance
- Base Code
- CSS
- Syntax
- Comments
- Declaration Order
- Name
- Performance
- Media Queries
- Javascript
- Syntax
- Comments
- Variables
- Performance
Always use a semicolon.
// Good
const $items = document.querySelectorAll('.items');
// Bad
const $items = document.querySelectorAll('.items')Use single quotes.
// Good
let string = 'Default';
const target = $element.getAttribute('data-target');
// Bad
let string = "Default";
const target = $element.getAttribute("data-target");Keep else on the same line lock if.
// Good
if ( true ) {
...
} else {
...
}
// Bad
if ( true ) {
...
}
else {
...
}Use space between operators.
// Good
for (i = 0; i < 10; i++) {
...
}
// Bad
for (i=0;i<10;i++) {
...
}Use space outside the (), but not inside.
// Good
if (condition) {
statement
}
// Bad
if( condition ){
statement
}Always use {} for conditional blocks.
// Good
if (condition) {
statement
} else if (condition) {
statement
} else {
statement
}
// Bad
if (condition) statement;
else if (condition) statement;
else statement;To check equality, always use ===.
// Good
if (foo === 'foo') {
statement
}
// Bad
if (foo == 'foo') {
statement
}Use // for single line comment.
// Good
// Description
// Bad
/**
* Description
*/Use the comment from one line above the related code.
// Good
// set the default status to true
const status = this._status || true;
// Bad
const status = this._status || true; // set the default status to trueUse /** ... */ para blocos de comentários
// Good
/**
* Description
* @param {String} Description of the param
*/
// Bad
//
// Description
//You may have comments to share prefix.
- FIXME - a problem that needs to be reviewed
- TODO - suggestion for a solution to the problem that needs to be implemented
// FIXME: shouldn't use a global here
total = 0;
// TODO: total should be configurable by an options param
this.total = 0;A syntax tip to write the reviews is the JSDuck.
/**
* @class Class name
* @param {String} Description of the param
* @extends name of the
* Documentation for the class
*/
/**
* @event click
* Documentation for the event
* @param {String} Description of the param
*/
/**
* @method Method name
* Documentation for the method
* @param {String} Description of the param
* @return {String} Description of the return
*/
/**
* @property {Boolean} [property=false]
* Description
*/
/**
* @class Class name
* Documentation for the class
*
* @constructor
* Documentation for the constructor
* @param {String} Description of the param
*/Always use let or const to declare variables. But will result in global variables.
// Good
const gallery = new Gallery();
// Bad
gallery = new Gallery();Use the statement let or const by variable.
// Good
const items = getItems();
const name = 'name';
const status = 'open';
// Bad
const items = getItems(),
name = 'name',
status = 'open';Group its const and then the lets.
// Good
const status = true;
const items = getItems();
let name;
let i;
let length;
// Bad
let i, len, status,
items = getItems(),
status = true;
// Bad
let i;
const items = getItems();
let status;
const status = true;
let len;Create the variables which need them.
// Good
function checkName(hasName) {
if (hasName === 'test') {
return false;
}
const name = getName();
if (name === 'test') {
this.setName('');
return false;
}
return name;
}
// Bad - unnecessary function call
function checkName(hasName) {
const name = getName();
if (hasName === 'test') {
return false;
}
if (name === 'test') {
this.setName('');
return false;
}
return name;
}Do not chain variable assignments. By chaining variable assignments, it creates implicit global variables.
// Good
(function example() {
let a = 1;
let b = a;
let c = a;
}());
console.log(a); // undefined
console.log(b); // undefined
console.log(c); // undefined
// the same applies for `const`
// Bad
(function example() {
// JavaScript interprets this as
// let a = ( b = ( c = 1 ) );
// The let keyword only applies to variable a; variables b and c become
// global variables.
let a = b = c = 1;
}());
console.log(a); // undefined
console.log(b); // 1
console.log(c); // 1Evitar o uso de incrementos e decrementos unários (++, --).
// Good
let array = [1, 2, 3];
let num = 1;
let increment = num += 1;
let decrement = num -= 1;
array.forEach((value) => {
value += 1;
});
// Bad
let array = [1, 2, 3];
let num = 1;
let increment = num ++;
let decrement = -- num;
for(let i = 0; i < array.length; i++){
let value = array[i];
++value;
}