Bullet list for Javascript best practice

09 Jul 2020 1 min

There are a lot of articles about following best practice in javascript. I have listed bullet point from TL;DR article for quick review. We can always write .eslintrc at root of the project to enforce linting. Here are the bullet points:

  • Do not deep nest the map function. Always decompose it to multiple functions.

  • Stop writing too many comments.

  • Don not take more than two argument in a function. If is required to take more than two argument, pass an object rather than multiple arguments.

  • If you see similar code, then you might me repeating yourself. Such code can always be refactored.

  • Avoid large function. Large function can always be spited into multiple small functions.

  • Limit your function to single responsible task. Do not create a side-effect in a function.

  • Use spread operator where as possible and it will help to reduce repeated code.

  • Variable Naming: Always use camelCase

  • Always use meaningful name. So it is are clear to reader. Favour descriptive over concise. Where there is shorter version use shorter version.

  • While naming use consistence verb, such as get create remove update.

  • Use isGreen, hasAirbag and similar verb while defining boolean flags.

  • Use nouns for class name. Write all class in PascalCase.

  • Capitalize constant value, like redux action name. SECOND_IN_A_DAY. They never change and should be update.

  • Avoid one letter variable name.

  • Clean way to console.log and debug it is following: console.log({foo, bar, baz}). It will give use variable name and value in one line and separately. Second argument of console.log function is style. You can use them to stand out the console log.

  • Always use template-literals.

  • Always use ternary operator over if else and if statement.

  • For Loop can be replaced with reduce api in array.

  • Always write async -await code to replace .then and Promise object and callback function