These things you should keep in mind while writing JavaScript

These things you should keep in mind while writing JavaScript

ยท

3 min read

Hello Folks ๐Ÿ‘‹

What's up friends, this is SnowBit here. I am a young passionate and self-taught frontend web developer and have an intention to become a successful developer.

Today, I am here with a few important things that you should keep in your mind while writing your JavaScript code.

Happy Reading!


Writing clean code is not a code that works, it's about readability, reused and refactored by other developers. In reality, you are not writing for yourself, you are writing for other developers who can easily read your code who can understand your code easily without any trouble editing or contributing to the project.

In this article, I will be focusing on why and how you should write clean code.

Camel Case

Camel case is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter, and the first word starting with either case.

Source: Wikipedia

// Don't do this โŒ
const isadmin = true

// Do this โœ…
const isAdmin = true

Meaningful variable names

While giving names to the variable, you should not give irrelevant or meaningless names; You should give proper names to the variable according to its job.

// Don't do this โŒ
const foo = ["cars", "boats", "planes"]

// Do this โœ…
const vehicles = ["cars", "boats", "planes"]

Boolean

When assigning a boolean to a variable, the name of the variable must be in interrogative tone.

The reason why you should name a boolean variable in an interrogative tone is that it is easy to find the type of the variable in little code but if you want to check the type of that variable then it becomes pretty time consuming to find that variable type and assign it a new value.

// Don't do this โŒ
let sunday = true

// Do this โœ…
let isSunday = true

Here, you can clearly determine the type of variable - isSunday as you are asking Is it Sunday? and the answer would be yes (true) or no (false) but in the case for variable sunday you can't determine the type of that variable without viewing, here sunday can be anything - I love sunday or I play cricket on sunday.

const isAdmin = true
const hasSubscribed = true
const hasTwitchLinked = false

Avoid extra and unnecessary context

Don't add unwanted information to the variable name when the context is provided by the object or a class

// Don't do this โŒ
const user = {
   userName: "SnowBit",
   userAge: 15,
   isUserAdmin: true
}

// Do this โœ…
const user = {
   name: "SnowBit",
   age: 15,
   isAdmin: true
}

Avoid hardcoded values

It's better to use meaningful variables to store constant values instead of hardcoded values. It is better to keep global constants in Upper Snake Case - UPPER_SNAKE_CASE

// Don't do this โŒ
const areaOfCircle = 3.14 * (4)^2

// Do this โœ…
const RADIUS_OF_CIRCLE = 4
const areaOfCircle = 3.14 * (RADIUS_OF_CIRCLE)^2

So, these were some important tips that I mind sharing with you folks and I hope you enjoyed reading the article. I will be making second part of this article soon so don't forget to follow me.

Thank you for reading, have a nice day! Your appreciation is my motivation ๐Ÿ˜Š

ย