How To Write Clean Code In Javascript
"Even bad lawmaking can function. But if the code isn't clean, it can bring a development organisation to its knees." — Robert C. Martin (Uncle Bob)
Clean coding means that you write code for your later self and your co-workers and not for the machine. Your code must be easily understandable for humans.
Hither are some of the clean coding practices which I follow while writing Javascript code. These are not framework-specific practices and can exist used with any framework.
1. Write uncomplicated code
Lawmaking should be uncomplicated enough to empathise. For example, if nosotros take to write a method that takes an array of number and return the new assortment with each number in array double its value. This can exist implemented every bit.
Instead of the above code, nosotros should practice it like this.
2. Write linear lawmaking
Nested code is hard to understand. Ever write the linear code as much equally possible. It makes our code uncomplicated, clean, easy to read and like shooting fish in a barrel to maintain thus makes programmer life easier.
For Instance, let's write a function which sends the electronic mail to the issue possessor.
Now let's expect at the same code implemented with async/await
Allow's look at some other example
Now let'due south look at the same code implemented with the fail-fast approach.
3. Better naming of variables and methods
It improves lawmaking readability and code go easier to maintain. Names should be meaningful and have context. Past reading the name of part or variable i should understand its purpose. Example
Always brand affirmative names. And so instead of isNotActive use !isActive
4. Functions should practice one thing
Function should not be larger than 20–25 lines. Smaller the function is better. Function should either modify or query something but not both. Consider the following lawmaking.
The same thing can be done in a cleaner way
5. Use ESLint, Prettier and latest JavaScript
Always utilize ESLint and Prettier to make common coding style across developers, find syntax fault and code formatting. Apply JavaScript latest features to write lawmaking, like de-structuring, spread operator, async-wait, template literals, optional chaining and more. Some Examples are
I promise this post has been helpful and thanks for reading. The feedbacks are always welcome.
Source: https://dev.to/deepaksisodiya/5-best-practices-for-clean-coding-in-javascript-26am
Posted by: minterhattheined.blogspot.com

0 Response to "How To Write Clean Code In Javascript"
Post a Comment