In this thread I will share 7+ tips and best practices that will help you write more clean and better javascript code.
Why to write clean code?
✔️ Improved maintainability of code
✔️ Improved code quality
✔️ Increased developer productivity
⚡ Let's dive in!
⭐ Use object destructuring
• It allows to extract values from arrays and objects into distinct variables.
• Why?
✔️ Reduce no. of lines of code.
• When is it useful?
✔️ When using multiple properties from an object.
✔️ Using a deeply nested property in object.
• Example:
⭐ Use guard clauses
• Guard clauses are a great way to prevent nested if..else by using the "return early" approach.
• Why?
✔️ More cleaner code
✔️ Reduced lines of code
• When is it useful?
✔️ When using nested if..else statements
• Example:
⭐ Use meaningful & pronounceable names for variables
1. Use plurals arrays
2. Use is for booleans
3. Use verbs for function names
4. Use meaningful names for callback variables
• Why?
✔️ More readable code
✔️ Improved productivity as a developer
• Example:
⭐ Use template literals for string concatenation
• Template literals are just string enclosed with backticks (`), providing an easy way to create multiline strings and perform string interpolation.
• Why?
✔️ More cleaner code
✔️ Improved productivity as a dev
• Example:
⭐ Keep function simple
• Using the KISS approach is a better way while writing code, for eg writing complex functions can lead to sphagetti code.
• A function shouldn't do more than one thing.
• Why?
✔️ Better code quality
✔️ Increased productivity as a dev
• Example:
⭐ Use default arguments instead of conditionals
• Default args are cleaner than conditionals.
• Beware that if you use them, your function will only provide default values for undefined arguments, NOT for falsy ones.
• Why?
✔️ Cleaner code
✔️ Better code quality
• Example:
⭐ Don't add unneeded context
• If you class or object is self explanatory and tells you something, then you don't need to repeat in its method or variable name.
• Why?
✔️ Better code quality
✔️ More readable and clean code
• Example:
That's it for this thread, I hope you've found it helpful!
Follow me (@vedantnn7) for more helpful tweets & threads like this to become a better web developer.
Also don't forget to like, retweet & share this thread. This really means a lot to me!
Thanks for checking this out!