Coding Standards
Introduction
Javascript/Typescript is a very flexible language. This flexibility can be a blessing and a curse. It can be hard to enforce a coding standard that is both flexible and consistent. This document is an attempt to define a coding standard that is flexible enough to allow for different coding styles, but also consistent enough to allow for easy code reviews and code sharing.
General code styling rules
Most rules should be enforced by your IDE and the linters.
If you are using VSCode, you can use the Prettier extension to automatically format your code on save. For the rest of the rules, you can use the ESLint extension to automatically fix them on save.
- Indentation: This project uses 2 spaces for indentation
- Semicolons: This project uses semicolons
;
- Quotes: This project uses single quotes
''
- Trailing commas: This project uses trailing commas
,
- New line usage:
- At end-of-file: This project uses new line end-of-file
- Between logical blocks: This project uses new line between logical blocks
- Before return statements: This project uses new line before return
- Naming:
- Use english for all programming
- Variables:
- Use camelCase
- Use descriptive names (e.g.
const numberOfItems = 5;)
- Use plural for arrays
- Don’t use abbreviations or special characters
- Functions:
- Use camelCase
- Use meaningful names/phrases (e.g.
const getSomethingFromSomewhere = () => {...};)
- Use verbs for functions
- Don’t use abbreviations or special characters
- Class names and test IDs:
- Where applicable use the BEM naming convention (
block__element--modifier)
Full list of linters, their plugins:
- eslint: Tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.
- prettier: An opinionated code formatter.
- prettier-eslint: Format code with prettier-eslint-cli. Formats your code using
prettier followed by eslint --fix.
- stylelint: A mighty, modern linter that helps you avoid errors and enforce conventions in your styles.