1 of 12

Code Formatting And Standards

THE QUEST TO WRITE READABLE CODE.

2 of 12

Contents

  • Formatting
  • Linting
  • Coding standards

3 of 12

Formatting

  • Related to alignment of brackets, indentation, line breaks, etc.
  • Produces visually appealing documents.
  • Options:
    • Prettier – JS
    • flake8 / black / pylint - Python
    • Other VSCode extensions
  • Formatting enables enforcement of certain conventions:
    • Alignment
    • Indentation
    • Tabs or spaces
    • Empty lines

4 of 12

Linting

  • Static analysis of the codebase to identify:
    • Redundant variable declarations.
    • Missing semicolons.
    • Undefined usage.
    • Single quoted strings.
    • And many more stylistic errors.

5 of 12

Formatting vs. Linting

  • Making the code “look” nicer.
  • Enforcement of best practices and standards.

6 of 12

Potential Problems: Linting

  • Opinionated linting.

An example of errors found using airbnb eslint configuration.

7 of 12

Potential Problems: Formatting

8 of 12

Solution

  • It is a good idea to create general guidelines with developer input.
  • This reduces the need for external tools to enforce coding standards.
  • Important to understand good coding practices and use them everywhere.

9 of 12

What rules do we want to recommend?

  • Readability over excessive rules.
  • Using tabs instead of spaces for indentations.
  • Spaced comments
    • // This is a good comment
    • //This is not
  • space-before-blocks
    • function abc() { //there is a space before the curly brace� … �}

10 of 12

Coding Standards Document

  • Contributions by team leaders and developers.
  • Link

11 of 12

Line Break Explanation

  • Spaces between subsequent blocks

12 of 12

Setup

  • .eslintrc.json and .prettierrc.json are stored in the root directory.
  • Install VSCode Prettier, ESLint and Pylint extensions.

  • Errors will be highlighted in the Problems tab.
  • Set tab size in VS Code to 4.
  • Install black formatter for python scripts. (pip install black)
    • Run using: python -m pip file.py