Code Style Practices

Node.js

1.  Use ESLint

  • Standard for checking code style​​
  • To identify nitty-gritty spacing issues
  • Detect serious code anti-patterns like developers throwing errors without classification

2.  Node JS Specific Plugins

  • Otherwise: Many faulty Node.JS code patterns might escape under the radar.

3.  Start a Codeblock's Curly Braces in the Same Line

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
  }

4.  Don't Forget the Semicolon

  • 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

5.  Name Your Functions

  • 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

6.  Naming conventions for variables, constants, functions and classes

  • 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() {
      
    }

  }

Example

7.  Prefer const over let. Ditch the var

  • `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.

8.  Requires come first, and not inside functions

  • 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)

9.  Do Require on the folders, not directly on the files

  • 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');

Example

THE END

 

 

Thank You

Code Style Practices

By nur amirah

Code Style Practices

Node.js

  • 196