Standard for checking code style
To identify nitty-gritty spacing issues
Detect serious code anti-patterns like developers throwing errors without classification
On top of ESLint standard rules that cover vanilla JS only, add Node-specific plugins like eslint-plugin-node, eslint-plugin-mocha and eslint-plugin-node-security
Otherwise: Many faulty Node.JS code patterns might escape under the radar.
The opening curly braces of a code block should be in the same line of the opening statement.
// Do
function someFunction() {
// code block
}
//Avoid
function someFunction
{
// code block
}
This will make your code more readable and explicit to other developers who read it.
Otherwise:
JavaScript's interpreter automatically adds a semicolon at the end of a statement if there isn't one which might lead to some undesired results
Name all functions, including closures and callbacks
Avoid anonymous functions
Allow you to easily understand what you're looking at when checking a memory snapshot
Use lowerCamelCase when naming variables and functions
Use UpperCamelCase (capital first letter as well) when naming classes
Use UPPERCASE for constants
// for class name we use UpperCamelCase
class SomeClassExample {
// for const name we use UPPERCASE
const CONFIG = {
key: 'value'
};
// for variables and functions names we use lowerCamelCase
let someVariableExample = 'value';
function doSomething() {
}
}
`const` : signal that the identifier won’t be reassigned
`let` : signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm.
The variable will be used only in the block it’s defined in, which is not always the entire containing function.
`var` : the weakest signal available when you define a variable in JavaScript.
The variable may or may not be reassigned, and the variable may or may not be used for an entire function, or just for the purpose of a block or loop.
Require modules at the beginning of each file, before and outside of any functions.
Help you easily and quickly tell the dependencies of a file right at the top.
Otherwise: If they are called from within a function, it may block other requests from being handled at a more critical time. (Synchronously)
When developing a module/library in a folder, place an index.js file that exposes the module's internals so every consumer will pass through it
Otherwise: Changing to the internal structure of files or the signature may break the interface with clients.
// Do
module.exports.SMSProvider = require('./SMSProvider');
module.exports.SMSNumberResolver = require('./SMSNumberResolver');
// Avoid
module.exports.SMSProvider = require('./SMSProvider/SMSProvider.js');
module.exports.SMSNumberResolver = require('./SMSNumberResolver/SMSNumberResolver.js');