The other day I was thinking about how I could learn to code better, and write it in a way that is more reusable and clean. I was watching many videos and reading various articles and in the end I thought, what if I put together everything I have been learning in one article?... And here we are:
- Avoid global variables: Global variables are a bad idea. There's a good chance you'll end up overwriting them by accident; It is more difficult for Javascript to find them compared to the local ones, in addition to other reasons that you can see in this thread.
- Comment: The code should be self-explanatory... But adding some comments doesn't hurt anyone.
- Avoid accessing the DOM: It is very expensive, so avoid it as much as possible.
- Don't trust: Validators are good friends and avoid errors when for some reason something fails.
- Libraries are our friends: Used well, they help us develop faster and with less code.
- Language and framework difference: Libraries and frameworks are very useful, but it is necessary to be able to distinguish one from the others and know what can be done with each one.
- Use tools that unify your code style like eslint.
- Modularize: Create functions that do one thing.
- Avoid nesting too much: Generating complex code makes it very difficult to maintain and understand it.
- Take care of your loops: Try to make the fewer and more optimized loops, the better. Instead of a for or while you can use find, reducer, map or filter when convenient. They are better.
- Stay up to date: The new Javascript specifications have functionalities like spread operator or the rest operator that make programming easier.
- Use let and const: Both are more optimal and avoid overwriting and scope errors.
- Test: Helps on many levels.
- Be curious: Many times we don't wonder how something works until it stops working. It's good to research the code and understand it even when it does work.
- There is a world beyond the console.log: Without a doubt, the console.log is everyone's favorite, however, there are many others that we can use and that will be more useful.
- Name your variables well: Your future self will thank you when they have to understand what you did. And so do your colleagues.
- Be consistent in the naming: If you decide camelCase, kebab-case, or any other... continue with it. Standardizing names helps you find things better and makes your code cleaner.
- Avoid duplication: If you have duplicate code, you are doing something wrong.
- Use strict mode: Javascript is a language that allows you many things without complaining. This is useful for getting something to work, but it can lead to strange behavior.
- Specify default values: This will save you from many careless errors, and will allow you to indicate that a property or parameter is optional.
- Use template literals: They are much more comfortable when handling strings with variables.
Top comments (0)