Diesel Inclusion Gas of Diesel Generator Set Extinguishes Automatically

Diesel generator set is a diesel engine that extinguishes diesel oil to obtain energy release. The advantages of diesel generator set are high power and good economic performance. The operation of…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




10 Tips to write cleaner JavaScript code

Let’s scrub those variables and functions clean

Image taken from the article: JavaScript is Everywhere, REALLY?

Writing clean and readable code is one of the most important things after constructing business logic. From my experience working with several organizations, startups, and projects, I can vouch that dealing with bad code is a nightmare. It slows down the development process, delays releases, and frustrates developers working with it. There is this saying in the developer community that -

Since your life can be at stake here 🥲, let me quickly guide you through some of the essential tips to keep your JavaScript clean.

I don’t know how I can emphasize this more. Let’s look at the example below:

It is good to explicitly name variables inside the array.forEach() instead of using shorthands that might not make sense later.

If the class or object name already tells you what it stands for, don’t add that information again to your variable names. In the example below, since we already know, we are talking about Car or paintCar you don’t need to mention the context car again in your variables.

Avoid usage of short-circuiting or conditionals in your functions to keep things cleaner. More importantly, remember that only undefined arguments will be provided with values by your function. Default values will not replace any other falsy values.

As a rule of thumb, try and keep the number of your function arguments limited to 2 or at max 3. If it’s taking so many arguments, there is a possibility your function is just doing too much. However, if you still need it, use a JavaScript Object as the argument. To make it obvious what properties the function expects, you can use the ES6 destructuring syntax.

Don’t forget what functions are meant to do — add modularity to your code. Smaller functions with each doing one and one task only will ensure that your code is easy to write, test, and understand. Never set multiple goals for a single function.

Make sure you write functions with names that clearly say what the functions do. Ambiguous function names mean readers will have to look at the function definition and logic to understand their job.

What hurts most is a bunch of the same or similar lines in multiple code sections. We all have come across such a scenario. This usually happens because some logic is slightly differently implemented in 2 or more places. However, think of the nightmares someone will have if they find a bug in the logic, and now they have to modify this everywhere.

Why will you need flags as function parameters? For only one apparent reason, your function is doing multiple things. And from point number 7, you know that’s a bad practice. So, go ahead and split your function into two.

Most of the above tips work with many programming languages, not just JavaScript. If you are looking for even more information for writing clean code in JavaScript, head to this repository. This article is inspired by the content there.

And if you are a beginner and you got overwhelmed, just remember that it’s called good coding habits, and it always takes time to get into a habit. It’s essential, however, that you make sure you know what habits to make, and you practice them so that with time, they just feel natural!

I would like to conclude by mentioning that don’t write clean code only in fear of the guy maintaining your code later on, but write it for yourself so that you don’t end up crazy later like this -

Do you also make apps with Flutter? I wrote an article here 5 Steps to Secure your next Flutter app. Go ahead and give it a read.

Add a comment

Related posts:

Mississippi Decay

On my first day in Mississippi, a resident asked a question I would hear often: "Who's your people?" She wasn't asking my last name. No one in the South is ever asking what you think they are asking.

DxChain Network

DxChain Network is still in working process with the MVP currently set to launch sometime in July. According to the CTO, Wei Wang, a test chain could be launched in the next few months and its…

What Animals Can Teach Us About Effective Teams

Since the earliest humans started forming groups to become more effective hunters, the human species has had a keen aptitude for forming teams to complete tasks too difficult for one individual. Many…