Node.js Tips For Beginners and Experts

1

Get real time updates directly on you device, subscribe now.

Here are 11 great tips for becoming the best in node development, no matter if you are a beginner or an expert:

 

  1. Avoid clutter – try to break your code into as many small pieces as possible. And even more. Why is it still worth avoiding code clutter? One well-known phrase of the American Air Force says: “Everything should be simple to the point of idiocy.” And there are reasons for this. The human mind cannot remember more than 5 to 7 things at a time. It’s just a fact. By breaking down the code into small component parts, you and other developers can easily understand it and understand what it is for. The testing process is also simplified.
  2. Use an asynchronous approach – avoid synchronous programming like the plague. Synchronous code is rarely used in today’s Node.JS. Typically, it finds its use in writing CLI commands or scripts not related to web applications. When it comes to web development, Node.JS programmers prefer to use the asynchronous approach as it avoids blocking threads.
  3. Avoid blocking threads – put ALL required statements at the beginning of the file, as they are synchronous and therefore will block the program. What most developers don’t even know is that require is cacheable. Therefore, as long as there are no noticeable changes in the reserved file name (and, in the case of using npm modules, there are none), the module code will be executed and loaded into a variable only once (for processing). This technique has a positive effect on optimization. However, even with caching, it’s best to try and avoid require first. Try using axios modules.
  4. Require must be cached – consider this a feature in your code. Or a bug. As you wish. We can put the code outside module.exports. Knowing that some parts of the code can be run only once, such an implementation will be more than useful.
  5. Always check your code – mistakes are not embroidery that can be thrown away at any time. Never miss the bugs you find! Node.JS is not Java for you. In Java, you throw exceptions because in most cases your Java application should not continue to run in the event of an error. In Java, you simply use try… catch to do this.
  6. Use try… catch only on a synchronous stream – try… catch is useless in asynchronous code. Also, v8 never optimizes try … catch code.
  7. Return values ​​or use if … else – just in case: return values ​​to stop the execution of a section of code. .JS – parallel. This feature can lead to bugs if not handled with due care. To be safe, stop the execution of a section of code using the return keyword.
  8. Pay attention to error events – almost all Node.JS classes or objects implement the observer pattern and produce error events. Don’t miss them. As a good habit, it would be a good idea to create error event listeners using .on ().
  9. Get to know your npm – install modules with –S or –D switches instead of –save or –save-dev. The NPM team relies on semver, but you shouldn’t. I want to say that they use auto-updates to intermediate versions of open source modules, since they believe that there will be no radical changes in these very intermediate versions. My advice to you is not to believe it too much. Moreover, use npm shrinkwrap. The command will create a new file with the current versions of the dependency dependencies.
  10. Use the current versions in package.json – when working with npm, it stupidly just adds an upper parenthesis by default when used in conjunction with the –S switch. To avoid this, simply manually lock versions. Never trust semver in your applications, but trust it in open source modules.
  11. Bonus – use different dependencies. Place what the project requires only during development in devDependencies. Then use npm i –production. The more unnecessary dependencies are used, the greater the risk of vulnerabilities.

- Advertisement -

1 Comment
  1. sheldon says

    wonderful article. good i found it

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More