Coding conventions are a style guide for programming. These include programming best practices and principles. Here we will discuss some coding conventions.

Benefits of following conventions

  • Clean code
  • The code quality
  • Code readability
  • Makes code maintenance easier

“Clean code is simple and straightforward. Clean code reads like well-written prose. Clean code never obsuses the designer’s intent, but is filled with clear abstractions and direct lines of control.” — Robert C. Martin

Magic number

A magic number means we are assigning numbers that have no clear meaning. Sometimes we use values for specific purposes, and we don’t assign values in meaningful variables. The problem is that when someone uses your code, they don’t know what the direct value means.

For (let I = 0; i < 50; Let NUMBER_OF_STUDENTS= 50 for(let I = 0; i < NUMBER_OF_STUDENTS; I++){// business code}

Deeply nested

Sometimes we use nested loops that are hard to understand. The way to deal with this is to extract all the loops into separate functions. Suppose we have an array that contains another array, and we want the value of the last array. We can write a nested loop to meet our requirements. But that’s not the way to go. Here I’ve written a function that can do the same thing, but it’s cleaner, simpler, less repetitive, and easier to read and reuse.

Const array = [['Shoaib Mehedi']] array.foreach ((firstArr) =>{firstarr.foreach ((secondArr) =>{const array = [['Shoaib Mehedi']]] array.foreach ((firstArr) =>{firstArr. secondArr.forEach((element) => { console.log(element); // Const array = [[['Shoaib Mehedi']]] const getValuesOfNestedArray = (element) => { if(Array.isArray(element)){ return getValuesOfNestedArray(element[0]) } return element } getValuesOfNestedArray(array)

[[Author: Wu Xiongwei]](https://wuxiongwei.com)

comments

This is a personal attack on someone. Comments help people understand them later and help other programmers working on the same project. Comments in your code mean that your code may not be self-explanatory. This is Jeff Atwood’s famous quote about writing reviews.

“While reviews are neither good nor bad in themselves, they are often used as a crutch. You should always write code as if the comments don’t exist. It forces you to write code in the simplest, simplest, most self-documenting way a human can think of.” “– Jeff Atwood

The comments should be fine, but your code needs to speak for itself.

Avoid large functions

When a function or class is large, it is recommended that it be divided into multiple classes. This will make our code simpler, cleaner, easier to understand, and reusable. Suppose we need to add or subtract two numbers. We can do that with a function. But a good idea is to divide them into two parts. When there are individual functions, this will be reusable throughout the application.

Const addSub = (${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition}${addition} ' // const add = (a,b) => const sub = (a,b) => {return a-b}

Code duplication

Repeated code is a block of code that is repeated multiple times in your code. This means that parts of your code need to be extracted into a function. This is the example we used earlier in 2.point deep nesting. Look at part one: We repeat the same thing three times. The solution of having a separate function do the same thing is a better solution; Plus, it’s reusable.

Const array = [['Shoaib Mehedi']] array.foreach ((firstArr) =>{firstarr.foreach ((secondArr) =>{const array = [['Shoaib Mehedi']]] array.foreach ((firstArr) =>{firstArr. secondArr.forEach((element) => { console.log(element); // Const array = [[['Shoaib Mehedi']]] const getValuesOfNestedArray = (element) => { if(Array.isArray(element)){ return getValuesOfNestedArray(element[0]) } return element } getValuesOfNestedArray(array)

Variable naming

Camel case is the naming standard for variables and functions, as well as other identifiers. This means that the name should start with a lowercase letter, and each first letter of the next word will be capitalized. Both functions and variables must follow the rules.

Const thisIsCamelCase = () => {// business code}

Meaningful names

Meaningful names are one of the most important conventions. Always use meaningful names for variables, functions, and other names. Choose a name that expresses the meaning of your purpose. If we needed a function to get the user’s bank information, we couldn’t use the similar name getUserInfo. We should use getUserBankInfo to be more specific.

Descriptive is better than concise

Try to use details for any naming. Suppose we need a function to find out who is using a mobile phone. Here, we can use meaningful names, but if there are other similar functions, there is a high probability of error. We must use a detailed, meaningful name to express the meaning in general terms.

Const searchUser = (phone) => const searchUserByPhoneNo = (phone) => {// Service code}

[[Author: Wu Xiongwei]](https://wuxiongwei.com)

Use a consistent verb for each concept

This is one of the important naming conventions. If we need a CRUD function, we use create, GET, or UPDATE with name. If we need to get user information from the database, the name of the function could be userInfo, user, or fetchUser, but that’s not the convention. We should use getUser.

Function getUser(){// do something}

Use the noun as the class name and use Pascal case

Classes don’t take things; They are things. Classes are basically blueprints for something. Do not use verbs in class names. In addition, a class should contain Pascal case. Camel Case is for objects, so if you use Camel Case in a class, it will be a little unclear.

Class MakeCar = {//... } class Car = {//... }

(SNAKE UPPER CASE)

This is another convention that we need to follow. Always use full uppercase names for constants. Snake capital means that all letters are capitalized, and the underscore separates all words.

const DAYS_IN_A_YEAR = 365;

Avoid single-letter variable names

One-letter variables are very, very bad to use. Do not use it for variable names. But in a loop, we use some variables with letters, and it works.

// Error demonstration const q = () => {//.... } // Correctly demonstrate const query = () => {//.... } for(let I = 0; i < 10; i++){ //... }

Conclusion Following these conventions will benefit you in the long run. When you write code with them, no matter who’s using it, that person understands everything for themselves. Both individuals and teams need clean code. Thank you for reading. Have a nice day.

[[Author: Wu Xiongwei]](https://wuxiongwei.com)