14 Best Practices for you to write 69% Higher Quality JavaScript code. π€ π€ π€
π Follow Thread π§΅
1. Reduce using global variables!
Global variables can be handy and easy to write, but don't overuse them. It can make your code hard to maintain and read.
However, consider using objects where you can group all your variables!
2. Use strict equality operators (===, !==) instead of non-strict (==, !=)
By using == or != operators, you don't check the types of values you compare. So, non-strict equality operators can easily lead to a bug in your code!
3. Use meaningful names for your variables and functions.
It is crucial to write understandable code which is 90% variables/functions.
So, don't worry if you use long names, the most important thing is readability!
4. Use max 2 arguments for Functions.
Using a big number of arguments can lead to a mess in your code.
The main reason for this is that order matters. So, if you mess it up, the function wonβt work properly.
Use objects instead!
5. Function must perform one action.
It is vital to have functions which are as short as possible! Actually, it helps code readability so much!
Take it seriously and you'll be ahead of many developers!
6. Try to avoid Side Effects in your code!
The function creates Side Effects when it does something different than just taking values and returning values.
Of course, you sometimes need side effects in your app. But try to minimize using it!
7. Don't ignore caught errors!
Try always do something with your caught errors, console.log is often not enough. You can notify users about errors or report them to the service.
Errors are a big part of our apps, don't miss them! π
8. Don't overuse comments, they are not required!
Try only comment parts of code that contain business logic and are hard to understand.
And remember, quality code explains itself!
9. Declare Variables outside of the For loops!
In cases where you do querySelector inside for loops, JS must grab your element as many times as for loop produces. It can slow down your code a lot! π±
The performance matters!
10. Add semicolons!
I know it is popular in the JS world not to write them because it's not necessary. I did it by myself as well.
However, be aware it can cause bugs!
So, just set your Prettier with auto semicolons.
11. Add a default case for switch statements.
Don't leave your switch statements without a default case because always something can go wrong and you want to catch the mistake.
12. Keep Ternary Operators short!
Ternary operators can be very helpful in quick comparisons but don't nest more than two of them. Anything longer should be an if statement or switch for readability and easier debugging.
13. Avoid using magic numbers!
Magic numbers are just random values in your code. Try to avoid such numbers by creating variables with suited names for them.
It will make your code more readable and searchable!
14. Use TypeScript instead!
TypeScript can significantly improve your code quality. If you've never used a type system before, it'll take some time, but it'll be worth it in the end.
That's all folks! π₯π§―
If you found this thread useful, please consider following @eugZolotarenko and retweeting the first tweet. π’
πππ VS-Code tricks that will SKY-ROCKET your productivity! π€―π€―π€―
π Thread π§΅
Content: 1. Zen Mode 2. Create and Open a file that does not exist 3. Quickly Navigate to a file 4. Side by Side Markdown Editor and Preview 5. Switch Branches with one click
π
6. Edit the same word within the document. 7. Edit the same word within the entire project. 8. Manage multiple cursors 9. Copy and move lines of code without copying/pasting them.
π€― 2οΈβ£0οΈβ£ π€― Useful websites and tools for Web Developers that you can't miss!
π Follow Mega Thread π§΅
1. Doodleipsum
Doodle Ipsum is a free illustration tool for devs powered by Blush! Inspired by the classic Lorem Ipsum, it automagically generates placeholder illustrations with a simple line of code.