Skip to content

Coding Conventions

Dinith edited this page Mar 11, 2020 · 4 revisions

Variables/Data

  • Variable names
    • lowerCamelCase
  • Constant names
    • UPPER_SNAKE_CASE
    • This includes environment variables
  • No magic numbers
    • Always use constants where applicable
    • If you find yourself changing the same value in multiple places it should probably be a constant
  • JSON key names
    • lower_snake_case for constant values (e.g. data returned by API)
    • lowerCamelCase if using them in a similar way to const and let

Functions

  • Function names
    • Should be short and descriptive (indicative of what function does)
    • Don't add 'function' on the end of the function name
    • e.g. getData(), not getDataFunction()
  • Function declarations
    • Named arrow functions where possible
      • e.g. const func = (x, y) => { ... }
    • Otherwise use function keyword assigned to a const
      • e.g. const func = function (x, y) { ... }
      • This is most similar to the arrow function so makes it uniform/easy to read
  • Avoid anonymous functions where possible (function names are useful for error tracing and indicating what it does)
    • Exception to this is for callback functions

Brackets

  • Put opening curly brackets on the same line for if, for and while
    • e.g. if (...) {
    • NOT if (...) </br>{
  • Entry point of program at top
  • Called methods below caller method
  • Catching errors (err)
  • Always use curly braces, even if one statement follows the if/for/arrow function
  • DON'T USE var
  • Always use const if the value won't change
  • Otherwise use let
Clone this wiki locally