Skip to content

Latest commit

 

History

History
85 lines (63 loc) · 3.34 KB

File metadata and controls

85 lines (63 loc) · 3.34 KB

JavaScript Code Style Guide

This guide outlines the recommended coding style and best practices for writing JavaScript code.

Table of Contents

  1. General Guidelines
  2. Naming Conventions
  3. Spacing and Indentation
  4. Variables
  5. Functions
  6. Conditional Statements and Loops
  7. Error Handling
  8. Comments
  9. Modules
  10. Testing

General Guidelines

  • Use descriptive names for variables, functions, classes, and other identifiers.
  • Keep functions short and focused on a single task.
  • Avoid deep nesting of code blocks.
  • Use proper error handling and avoid swallowing exceptions.
  • Write self-explanatory code and avoid unnecessary comments.
  • Follow the SOLID principles when designing classes and modules.

Naming Conventions

  • Use camelCase for variables, functions, and object properties.
  • Use PascalCase for classes and constructor functions.
  • Use uppercase snake_case for constants and enum values.

Spacing and Indentation

  • Use 2 spaces for indentation.
  • Use spaces around operators and after commas.
  • Place an opening brace on the same line as the statement or declaration.
  • Use a space between the if keyword and the opening parenthesis.

Variables

  • Use const for variables that will not be reassigned, and let for variables that will be reassigned.
  • Declare each variable on a separate line to improve readability.
  • Initialize variables at the point of declaration whenever possible.
  • Avoid global variables and minimize the scope of variables.

Functions

  • Use function declarations or arrow function expressions for named functions.
  • Use concise arrow function syntax (() =>) for callbacks and anonymous functions.
  • Use meaningful and descriptive function names.
  • Limit the number of function arguments to improve readability.
  • Avoid unnecessary side effects in functions.

Conditional Statements and Loops

  • Use strict equality (=== and !==) for comparisons.
  • Avoid unnecessary negations (!) in conditions.
  • Use for...of loops for iterating over arrays and other iterable objects.
  • Prefer for...of over for...in for iterating over object keys.
  • Use Array#forEach or higher-order array methods when appropriate.

Error Handling

  • Use try...catch blocks for catching and handling exceptions.
  • Avoid catching generic Error objects unless necessary.
  • Always provide informative error messages when throwing exceptions.

Comments

  • Use comments to explain complex algorithms, business rules, or any non-obvious code.
  • Write self-explanatory code and avoid excessive comments.
  • Remove commented-out code before committing changes.

Modules

  • Use ES6 modules (import and export) for managing dependencies.
  • Avoid using the import statement without a corresponding export statement.
  • Prefer default exports for single exports and named exports for multiple exports.

Testing

  • Write unit tests for critical and complex functionality.
  • Use a testing framework such as Jest or Mocha.
  • Separate test files from source files and follow a consistent naming convention.
  • Run tests automatically as part of the development process.