Use of a Coding Standard

A coding standard is a set of rules and agreements used when writing source code in a particular programming language. Using a coding standard provides some advantages, such as:

  • •It gives a uniform appearance to the codes written by different programmers.
  • •It improves the readability and maintainability of the code and reduces complexity.
  • •It helps in code reuse and helps to detect errors easily.
  • •It promotes good programming practices and increases the efficiency of the programmers.

Many frameworks come with tools which automate code checking and fixing against a set of predefined rules. For example, Nest comes with ESLint and Prettier out of the box. In this book, we will use ESLint, so let’s learn more about it.

Introducing ESLint

ESLint is a JavaScript linter that enables you to enforce a set of style, formatting, and coding standards for your codebase. It looks at your code and tells you when you're not following the standard that you set in place (https://eslint.org/).

By default, ESLint does not support TypeScript (the language we use to create our controllers and other files). However, Nest includes typescript-eslint/eslint-plugin and typescript-eslint/parser dependencies which enables ESLint to support TypeScript.

Nest defines the ESLint configuration and rules in the .eslintrc.js file. Therefore, Nest defines a script to execute ESLint. That script ( lint ) can be found in the scripts section of the package.json file. Let’s analyze it.

Analyze Code

"lint": "eslint \ "{src,apps,libs,test}/**/*.ts \ " --fix",

When we run npm run lint , the above script is executed. This script executes ESLint and checks and fixes all the TypeScript files inside the src , apps , libs , and test folders.


Running ESLint

In the Terminal, go to the project directory, and execute the following:

Execute in Terminal

npm run lint

This executes ESLint based on the custom configuration defined in .eslintrc.js. You will see changes in some of your TypeScript files (such as the src/app.controller.ts ). If you open that file, you will see that the strange spaces have disappeared. All texts are defined with single quotes, and variables are defined with “cons” instead of “let” when it applies.

Final remark

The current ESLint configuration only checks and fixes TypeScript files. So, if you want to format Handlebars files, you can use a third-party library (such as eslint-plugin-hbs ) or use a Visual Studio Code formatter such as “HTML Language Features”.

TIP: There is a good story about “The Broken Window Theory” described in the (2019 - Thomas, D., & Hunt, A. - The Pragmatic Programmer: your journey to mastery) book. You can search it in Google. From that story, we want to highlight the next tip. Don’t leave “broken windows” (e.g., bad designs, wrong decisions, or poor code) unrepaired. Fix each one as soon as it is discovered. The previous three chapters showed many broken windows which fortunately were fixed.

TIP: Always use a coding standard tool, formatter, static code analysis tool, or even a combination of them in your projects. It will save you a lot of time and improve the code quality. In addition, you will find linters available for most programming languages. Besides, include a rule in your architectural rules document mentioning that all code changes should be previously verified using these tools. You can even automate this process (with a pipeline or CI/CD strategy). However, this is out of the scope of this book.

See Next Article Nest JS ...


Post a Comment for "Use of a Coding Standard"