<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Umang Mittal</title>
    <description>The latest articles on DEV Community by Umang Mittal (@umangmittal).</description>
    <link>https://dev.to/umangmittal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F849325%2F0eab8696-fd2d-45df-a35d-0fe9bf7d25bc.jpg</url>
      <title>DEV Community: Umang Mittal</title>
      <link>https://dev.to/umangmittal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/umangmittal"/>
    <language>en</language>
    <item>
      <title>The feature flags</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Sun, 21 Sep 2025 06:06:32 +0000</pubDate>
      <link>https://dev.to/umangmittal/the-feature-flags-39c6</link>
      <guid>https://dev.to/umangmittal/the-feature-flags-39c6</guid>
      <description>&lt;p&gt;Imagine launching a new feature to your entire user base, only to discover a critical bug that brings your application to a halt. The frantic rollback, the lost trust, the late-night fixes – it's a developer's nightmare. But what if there was a way to deploy new code confidently, test features in production with a small segment of users, or even turn features on and off at will, without redeploying your application?&lt;/p&gt;

&lt;p&gt;Enter Feature Flags (also known as Feature Toggles or Feature Switches). In this post, we'll dive deep into what feature flags are, how they work, and how you can leverage them to build more resilient, user-centric web applications. Get ready to transform your deployment strategy!&lt;/p&gt;

&lt;p&gt;The way the feature flag works is just be returning true or false and based on this condition your code will show new UI or the older UI.&lt;/p&gt;

&lt;p&gt;That means you need to have the older + new logic in the code. Something like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;featureFlagClient&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;initFeatureFlagsClient();&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;newUIEnabled&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;await&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;featureFlagClient.get(&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="s2"&gt;"new-feature"&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;fallback&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="err"&gt;context:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; 
          &lt;/span&gt;&lt;span class="err"&gt;//Any&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;user&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;details&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;for&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;which&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;you&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;want&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;pass&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;determine&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;the&lt;/span&gt;&lt;span class="w"&gt; 
     &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;);&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="err"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;(newUIEnabled)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;renderNewUI();&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;else&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;renderOldUI();&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now, you have a ON/OFF switch to control the visibility of new UI. Even after it was deployed, it won't be available to anyone. Once you set the flag to true, then only the users will be able to see the shiny new UI that you just created.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros of using feature flags:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ON/OFF switch:&lt;/strong&gt; You can just control the flow without the deployments and it also gives you a configuration you can change anytime&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Safety Net:&lt;/strong&gt; You can roll a new feature incrementally and rollback instantly that gives you a safety net if something goes wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Beta Access:&lt;/strong&gt; This enables you to roll feature for a certain individuals who signed up for using new UI in BETA mode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No Deployments:&lt;/strong&gt; If you want to enable or disable this new UI, you don't need to deploy the new build or revert any commit. All clean and good.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But not everything that shine is gold, if you use feature flags without giving it a much thought, you will find yourself in a technical debt very fast.&lt;/p&gt;

&lt;h4&gt;
  
  
  Things you should consider before
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Complexity * Complexity:&lt;/strong&gt; Yes, if you use feature flags blindly and think of it as one solution for most of the problems, you are adding a very thick layer of complexity in your codebase. Now, you have to maintain both the old and new features, as both can be used and anytime and nobody knows which ones are using in which cases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nested complexity:&lt;/strong&gt; What if you used a feature flag inside of old and new UI features that are already under one feature flag. You are basically adding a complex nested flag based structure that won't go far enough.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Technical Debt:&lt;/strong&gt; If you use feature flags for a deployment and never removed the feature flag after the users are successfully migrated to the new UI, then it will become a debt that either needs to paid and the more flags you have, the more interest that debt collects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, feature flags are a great way to roll out/test features on production without making them available for public, but only if you know what you are doing and you do it the right way.&lt;/p&gt;

&lt;h4&gt;
  
  
  Things to consider
&lt;/h4&gt;

&lt;p&gt;While making the decision whether to implement feature flags or not, keep these things in your mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ask if you really need it:&lt;/strong&gt; Ask yourself whether you really really need the feature flag? If yes, keep reading: else exit;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clean the clutter:&lt;/strong&gt; If you are implementing a feature flag, make sure to assign a task to remove this feature flag if not needed afterward.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Document the requirement:&lt;/strong&gt; And keep it documented, what is the need for this flag, the scope of this flag, when it introduced.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As we wrap up our exploration of feature flags, it's clear they are far more than just a temporary switch. They represent a fundamental shift in how we approach software development and deployment. By embracing feature flags, you're not just gaining the ability to turn features on and off; you are also reducing the stress and risk associated with every new release.&lt;/p&gt;

&lt;p&gt;Implementing feature flags effectively requires careful planning and clear communication within your team. But the benefits—faster innovation, happier users, and more confident deployments—are well worth the effort. &lt;/p&gt;

&lt;p&gt;So, next time you're planning a new feature, consider the power of the flag. It might just be the most impactful decision you make for your web application's future.&lt;/p&gt;

&lt;p&gt;I hope this blog post made you learn something new or just pressed F5 on your knowledge system. Thanks for reading.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Umang Mittal&lt;/em&gt; signing off!&lt;/p&gt;

</description>
      <category>learning</category>
      <category>beginners</category>
      <category>softwaredevelopment</category>
      <category>development</category>
    </item>
    <item>
      <title>From Junior to Pro: Mastering Code Design with S.O.L.I.D.</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Sun, 21 Sep 2025 05:58:23 +0000</pubDate>
      <link>https://dev.to/umangmittal/from-junior-to-pro-mastering-code-design-with-solid-4nhh</link>
      <guid>https://dev.to/umangmittal/from-junior-to-pro-mastering-code-design-with-solid-4nhh</guid>
      <description>&lt;p&gt;In the world of software development, we often talk about building "good" code. But what does "good" really mean? Is it code that runs fast? Code that has no bugs? Or is it something more? "Good" code, at its core, is code that is easy to understand, maintain, and extend. It's code that doesn't crumble under the pressure of new features or unexpected changes.&lt;/p&gt;

&lt;p&gt;This is where the SOLID principles come in. More than just a set of rules, they are a philosophy for building software that is flexible, resilient, and ready for the future. Over the next few minutes, we'll dive into each of the five SOLID principles—Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion—and see how they can transform the way you write code. Get ready to move beyond the theory and discover how these principles can save you from late-night debugging sessions and make you a more confident, capable developer.&lt;/p&gt;

&lt;p&gt;Let's take a look at them one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  S - Single Responsibility
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Your class/function only has one RESPONSIBILITY and only ONE REASON TO CHANGE.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose we are having a NotificationService class, the job of this class is to send Notification to user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ BAD: This class has two responsibilities&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Responsibility: Formatting the message&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formattedMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Message: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Responsibility: Sending the notification&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;formattedMessage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; to user: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//Actual message sending code goes here...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here this function/class has two main responsibilities: Formatting the message + Sending the formatted message to user.&lt;/p&gt;

&lt;p&gt;If we have to modify the formatting of the message to say "Hi" instead of "Hello", we have to modify the NotificationService class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ✅ GOOD: Each class has one clear job&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MessageFormatter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;formatMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Message: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formatter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messageFormatter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;formatter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;formattedMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messageFormatter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;formatMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;formattedMessage&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; to user: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//Actual message sending code goes here...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you have to change formatting of the message, our NotificationService class needs not to be modified. Only MessageFormatter class will be modified. And if something needs to be updated in sending logic, only that class gets the update is the NotificationService minimizing bugs and achieving the "S" in SOLID.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The DIFFERENCE?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
If you update the formatting logic, there could be a possibility that a new bug get introduced in sending logic (accidentally), which will break your entire message sending logic.&lt;br&gt;
In Short, the blast radius is huge.&lt;br&gt;
On the other hand, making changes in MessageFormatter means you are not dealing with any sending logic, even if a bug got introduced, the only risk is that the formatting of the message is done incorrectly, but the other functionality(message sending logic) still works great.  You've contained the risk of change to only the formatting itself.&lt;br&gt;
[What if other services are using the same NotificationService?]&lt;br&gt;
In short, the blast radius is tiny.&lt;/p&gt;
&lt;h2&gt;
  
  
  O - Open/closed principle
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Your class/function should be open for extension, but closed for modifications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Simply put, you should be able to add new feature without changing the existing code. This prevents bugs in the code that already works.&lt;/p&gt;

&lt;p&gt;Suppose we are having a NotificationService class, the job of this class is to send Notification to user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ BAD: We have to modify this class for every new notification type&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Email: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sms&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending SMS: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="c1"&gt;// 😱 We'd have to add a new 'case' for Push notifications here!&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose, we want to add a push notification to our sending logic, we need to modify the NotificationService class, which was already working fine + battle tested for bugs.&lt;br&gt;
By modifying the logic inside the class/function, you are introducing a risk of accidentally creating bugs.&lt;br&gt;
What if you forgot to add break statement in your new case block?&lt;br&gt;
What if you changed some scoped variable in your logic?&lt;br&gt;
For sending email, you need emailAddress, for sms you need phone number, for push notification you need device id, you are making the class bloated which is also &lt;br&gt;
breaking the SRP(Single Responsibility) principle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ✅ GOOD: We can add new notifiers without touching existing code&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Email: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmsNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending SMS: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// ✨ NEW: Add a PushNotifier without changing NotificationService!&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PushNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Push Notification: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// The main service is now "closed" for modification but "open" to new notifiers&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;notifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, adding PushNotifier or any other type requires zero changes to the NotificationService.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The DIFFERENCE?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Updating the logic for adding the push notification functionality could potentially introduced new bugs + risks and could break the already working functionality for sms and email.&lt;br&gt;
After the changes, adding new notification type service in our codebase is now easy and less error prone. No risk of disturbing the older services even if something  breaks in the push notification as only those will stop working.&lt;/p&gt;
&lt;h2&gt;
  
  
  L - Liskov Substitution
&lt;/h2&gt;

&lt;p&gt;The concept of this rule is very simple but yet it is hard to maintain.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every child class should be substitutable with their parent class. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All child classes should extend the functionalities of the parent class not narrow down them.&lt;/p&gt;

&lt;p&gt;Suppose you have a Notification class which takes in email and sends the message to the recipient. Enter a new GuestNotification class which also sends notification to the guest clients but only via push notification, because we do not have email addresses of the guests. So, we throw error in send function.&lt;/p&gt;

&lt;p&gt;When we try to call the send function on this new class, our code breaks as we don't have the functionality to send message to guests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ BAD: The subtype breaks the parent's contract&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Notification&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recipient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;recipient&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;" to &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recipient&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailNotification&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Notification&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GuestNotification&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Notification&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Guests don't have a recipient email, so we break the rule&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This is already awkward&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// And now we can't fulfill the parent's primary function&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Guests cannot receive notifications.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sendWelcomeMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notification&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;notification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// This will crash for GuestNotification!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;sendWelcomeMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EmailNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dev@example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Works fine&lt;/span&gt;
&lt;span class="nf"&gt;sendWelcomeMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;GuestNotification&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 💥 CRASH!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To solve this kind of situation, we need to rethink the our design schema.&lt;br&gt;
Perhaps GuestNotification shouldn't be a Notification at all, or we need a different abstraction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ✅ GOOD: All subtypes are safely interchangeable&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Notification&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Method 'calculateArea()' must be implemented by subclasses.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailNotification&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Notification&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;recipient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recipient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;recipient&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Email "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;" to &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;recipient&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PushNotification&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Notification&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;deviceId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deviceId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;deviceId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Push "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;" to device &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deviceId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sendWelcomeMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notification&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;notification&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Works for ANY notification type&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;sendWelcomeMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EmailNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dev@example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nf"&gt;sendWelcomeMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PushNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;device-123&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, any function expecting a Notification will work perfectly with all its subtypes.&lt;br&gt;
In future, we can also integrate any new Notification Type and inherit the send message function the way that class expects and our Notification class can be used anywhere in the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The DIFFERENCE?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Instead os having child subclasses narrowing some methods or functionalities of the parent class, it should extend them. By this, we can make sure, wherever in our code we are using the parent class, we can expect the same functionality no matter the type of child class we receive.&lt;/p&gt;
&lt;h2&gt;
  
  
  I - Interface Segregation
&lt;/h2&gt;

&lt;p&gt;Keep your interfaces lean and they will keep your code maintainable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Client or New classes should not be forced to implement the functions or properties they don't use.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose you have an interface of NotficationService for different clients. One clients only uses Push Notification service, and you inherit this with the NotficationService class.&lt;/p&gt;

&lt;p&gt;The NotficationService interface is too fat and bulky that it forces the class to implement the methods that they don't use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ BAD: This interface is too "fat"&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;INotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;sendSms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;sendPushNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// This class only cares about push notifications but is forced to implement all methods&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PushNotifier&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;INotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// I don't do this! 🤷‍♂️&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;sendSms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Or this!&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;sendPushNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Push Notification: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To solve this situation, we break the bulky interface into smaller, role-based interfaces. A class can then implement only the interfaces it needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ✅ GOOD: Small, focused interfaces are better&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IEmailNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ISmsNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendSms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IPushNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendPushNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Now our class implements ONLY what it needs. So clean!&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PushNotifier&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IPushNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendPushNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Push Notification: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Another class could handle multiple types if needed&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AllInOneNotifier&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IEmailNotifier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ISmsNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Email: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;sendSms&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending SMS: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach keeps your code clean, understandable, and prevents you from implementing useless methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The DIFFERENCE?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Now you can implement a new Notification Service for a new client that only uses email or both email and sms notification service.&lt;/p&gt;
&lt;h2&gt;
  
  
  D - Dependency Inversion
&lt;/h2&gt;

&lt;p&gt;The goal of this principle is to decouple the modules.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The high-level modules or classes should not depend on low-level modules or classes, instead both should depend on abstractions (or interfaces).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose you have NotificationService that creates an instance of EmailService. This means that NotificationService is tightly coupled to EmailService. In future, if we want to implement a new SMS service we need to modify the NotificationService class which could potentially lead to bugs related to sending email code which was already working fine and battle-tested.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ BAD: High-level module depends directly on a low-level module&lt;/span&gt;
&lt;span class="c1"&gt;// Low-level detail&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Email: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// High-level module&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 😱 Dependency is created and hard-coded inside the class!&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emailService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EmailService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emailService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of directly creating the instance of EmailService, we will create an interface or abstract layer that defines how the notification sending service should look like. And use this interface for telling our NotificationService to send message. And we do not create instance inside the NotficationService and pass it from the outside.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ✅ GOOD: Both modules depend on an abstraction&lt;/span&gt;
&lt;span class="c1"&gt;// The Abstraction (the "what")&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;IMessageSender&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Low-level details (the "how")&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailService&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IMessageSender&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending Email: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SmsService&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;IMessageSender&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Sending SMS: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// High-level module depends on the abstraction, not the detail&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IMessageSender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// The dependency is "injected" from the outside! ✨&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IMessageSender&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sender&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Now we can easily switch dependencies without changing NotificationService!&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;emailNotifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;EmailService&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;smsNotifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NotificationService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SmsService&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="nx"&gt;emailNotifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello via Email!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;smsNotifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello via SMS!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that our code is ready. Even if a new notification type needs to be introduce, suppose push notification, we can use the interface (abstract layer) to inherit the properties and methods.&lt;br&gt;
And no modifications are needed inside the NotificationService class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The DIFFERENCE?&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Even if you introduce a new notification service, you just need to use the abstraction to create it and even if you have accidentally introduced some bugs, they will only be for push notifications.&lt;br&gt;
The Sms and Email notification service will work as it was working before.&lt;/p&gt;

&lt;p&gt;You've now completed your journey through the five SOLID principles. From the Single Responsibility Principle, which teaches us to keep our classes focused, to the Dependency Inversion Principle, which frees us from rigid dependencies, these principles are more than just a theoretical framework. They are a practical toolkit for building software that is more robust, easier to maintain, and ready to adapt to the ever-changing demands of a project.&lt;/p&gt;

&lt;p&gt;Adopting SOLID principles may seem like a challenge at first, but the long-term benefits are undeniable. Less technical debt, easier debugging, and the ability to confidently add new features without breaking existing ones are just some of the rewards. So, the next time you write a line of code, ask yourself: Is it SOLID? Your future self—and your team—will thank you for it.&lt;/p&gt;

&lt;p&gt;That's it for this one. Thanks for reading and let me know your thoughts in the comments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Umang Mittal...&lt;/em&gt; Signing off!&lt;/p&gt;

</description>
      <category>solidprinciples</category>
      <category>learning</category>
      <category>productivity</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Crypto Trading project</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Mon, 23 Jun 2025 03:48:02 +0000</pubDate>
      <link>https://dev.to/umangmittal/crypto-trading-project-1k6</link>
      <guid>https://dev.to/umangmittal/crypto-trading-project-1k6</guid>
      <description>&lt;p&gt;Hey Devs and possibly traders,&lt;/p&gt;

&lt;p&gt;In my college, I created a side-project that trades in cryptocurrencies with real money. So, I can make some profits and more importantly save myself from losses while sleeping.&lt;br&gt;
It started like most side projects — I was bored, curious, and had just enough caffeine to believe I could beat the crypto market (spoiler: I couldn't, but it was worth it).&lt;/p&gt;

&lt;p&gt;Let's start with the "BACKGROUND" first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;At that time crypto was becoming mainstream and I was also fantisized by the wave of crypto currency. A decentralized network, sharing tokens without any third party, a mined block, and obviously a 10x or sometimes 1000x profits. ($ in eyes emoji).&lt;br&gt;
So, I thought why not start trading in crypto. I created my account on CoinDCX and started trading with my first 500 ruppee.&lt;/p&gt;

&lt;p&gt;After some time, I realized I was looking at the charts too often and everytime I get 10 min, between my classes, during lunch break, while traveling to and from college. And since I have a developer mindset, my dev instincts kick in. Why not automate this process? Can I make a script to do all that for me?&lt;/p&gt;

&lt;p&gt;This small question lead me to creating my Crypto.trader() project. But I can't give you the exact code as it got deleted when my hard disk once crashed a few years back.&lt;/p&gt;

&lt;p&gt;Nah, I am not building it in this blog, and even now that I think of building it, there's a whole lot of taxes on crypto and stuff. But, my dev instincts still want to create something like that again. Well, another story for another day.&lt;/p&gt;

&lt;p&gt;By this blog, I just want to write down my experience of how a simple idea lead me to building a full-fledged project, and might be possible that someone's dev instincts kick in by reading this article  leading to a new solution.&lt;/p&gt;

&lt;p&gt;So, I digged in, did some research and found that coindcx has developer API, from which you can place order, get market details of a token like current price, cancel an order, get status of the order and much more.&lt;/p&gt;

&lt;p&gt;Now my idea has a ground, let's create a script/bot that will run 24/7 and monitor the different currencies and get the market data, run some calculation, either place the order and after making profits sell it or hold it.&lt;/p&gt;

&lt;p&gt;Back then, I did not understand the design diagrams and design patters, so I just created what I like. &lt;/p&gt;

&lt;h2&gt;
  
  
  Okay, Enough chit-chat. Let's get started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ⚙️ My Very Own Crypto Bot: Overview
&lt;/h3&gt;

&lt;p&gt;The idea was to build a 24/7 script/bot that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Monitors market data of multiple currencies.&lt;/li&gt;
&lt;li&gt;Runs some calculations using a tiny brain (i.e., an algorithm).&lt;/li&gt;
&lt;li&gt;Makes a decision: buy, sell, or hold.&lt;/li&gt;
&lt;li&gt;Places the order if needed, and logs everything and notify me like my well-wisher.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🧱 Architecture: Four Simple Python Classes
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Portfolio
&lt;/h4&gt;

&lt;p&gt;Handles everything market-related:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fetches the current price of a token.&lt;/li&gt;
&lt;li&gt;Places buy/sell orders.&lt;/li&gt;
&lt;li&gt;Tracks wallet balance.&lt;/li&gt;
&lt;li&gt;Maintains what tokens we hold.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basically, it’s the bot’s bank account + market window.&lt;/p&gt;

&lt;h4&gt;
  
  
  Trader
&lt;/h4&gt;

&lt;p&gt;This is where the magic (or nonsense?) happens:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contains the algorithm that decides whether we should buy, sell, or hold.&lt;/li&gt;
&lt;li&gt;Core logic lives in a method called decide_buy_sell_hold()&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Logger
&lt;/h4&gt;

&lt;p&gt;If it logs, it's real:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writes all decisions and actions into a log.txt file. (Buy/Sell signal, at what time, at what price, for which token, etc.)&lt;/li&gt;
&lt;li&gt;Sends alerts when an order is placed or an asset is bought/sold.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Crypto
&lt;/h4&gt;

&lt;p&gt;This is the main orchestrator class.&lt;br&gt;
It combines Portfolio, Trader, and Logger together and kicks off the loop.&lt;/p&gt;

&lt;p&gt;Main class that packs all of these classes is called Crypto, hence I named it Crypto.trader(), because why not — let’s pretend Python has namespaces like Java.&lt;/p&gt;

&lt;p&gt;Yes, you heard it right, I used python language to fuel up my idea. &lt;/p&gt;

&lt;h2&gt;
  
  
  🐍 Technology Stack
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Language:&lt;/strong&gt; Python (because of course)&lt;br&gt;
&lt;strong&gt;API:&lt;/strong&gt; CoinDCX Developer API, Twilio for messaging&lt;br&gt;
&lt;strong&gt;Data Storage:&lt;/strong&gt; Flat file logging (log.txt)&lt;br&gt;
&lt;strong&gt;Runtime:&lt;/strong&gt; A loop that sleeps between API calls and keeps the bot running 24/7&lt;br&gt;
&lt;strong&gt;Deployment:&lt;/strong&gt; Amazon EC2 instance&lt;/p&gt;

&lt;h3&gt;
  
  
  💸 The five senses of the Bot: Portfolio API's
&lt;/h3&gt;

&lt;p&gt;This class deals with all kind of marketplace related things&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Getting information about the token/coin&lt;/li&gt;
&lt;li&gt;Placing/Revoking a market place order (BUY/SELL)&lt;/li&gt;
&lt;li&gt;Polling on the pending order to mark if fulfilled or rejected.&lt;/li&gt;
&lt;li&gt;If order is fulfilled, add it to the available to sell funds list and if the order is not fulfilled for 10 min, cancel the order and add the funds back into available funds.&lt;/li&gt;
&lt;li&gt;Fetches the available balance from the wallet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This class manages all the configs and available funds, so the Trader class can make an informed decision.&lt;/p&gt;

&lt;p&gt;Whenever algo generates a buy/sell signal, the Portfolio class creates a corresponding BUY/SELL order, and put a watcher on it, that polls the order every 10 seconds to know if the order is fulfilled or rejected.&lt;/p&gt;

&lt;p&gt;If the order if fulfilled, add the token/coin into the bought list so it can later sell it, when feasible.&lt;/p&gt;

&lt;p&gt;If the order is not fulfilled for 10 min, cancels the order and add the funds back to the available funds so it can be used for buying a token later.&lt;/p&gt;

&lt;h3&gt;
  
  
  🧠 The Brain of the Bot: Trader Logic
&lt;/h3&gt;

&lt;p&gt;The main logic of this bot relies in the Trader class, the trader class has a method decide_buy_sell_hold, which when called upon with params, return whether we want to hold the asset, buy it (if yes, at what price) or sell it if we already bought (if yes, at what price).&lt;/p&gt;

&lt;p&gt;📜 Step-by-Step Process:&lt;br&gt;
&lt;strong&gt;Step 1&lt;/strong&gt; Receive Token: The method is called with the token name (e.g., BTC-INR).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt; Get Price History:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pulls the last n prices (default 5) of the token.&lt;/li&gt;
&lt;li&gt;Used to spot trends.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt; Check If Can Sell:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Looks at Portfolio holdings.&lt;/li&gt;
&lt;li&gt;Already holding this token? Okay, maybe we can sell or hold.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt; Sell Conditions:&lt;br&gt;
If prices are strictly decreasing → look deeper:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If current loss &amp;gt; 5%, sell (stop-loss).&lt;/li&gt;
&lt;li&gt;If current profit &amp;gt; 20%, sell (profit booking).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt; Check If Can Buy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Looks into the Portfolio wallet.&lt;/li&gt;
&lt;li&gt;Enough balance? Okay, maybe we can buy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 6&lt;/strong&gt; Buy Conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the last 5 prices are strictly increasing (bullish trend).&lt;/li&gt;
&lt;li&gt;AND we have money → return BUY signal with price.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 7&lt;/strong&gt; Else:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No condition matched → return null (HOLD).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🦾 Treasurer and Notifier: Logger
&lt;/h3&gt;

&lt;p&gt;The main objective of this class is to log everything in txt files so there is a written log of everything happened, why it happened and if the bot needs to restart itself, it knows what it already has in account and when it bought it at what price so it can take right decisions.&lt;/p&gt;

&lt;h4&gt;
  
  
  Logging Mechanism
&lt;/h4&gt;

&lt;p&gt;Whenever algo generate buy or sell signal, timestamp, when the market order is placed, when it got fulfilled or rejected, what was the market depth when the decision was generated, and more. It writes everything down in txt files.&lt;/p&gt;

&lt;h6&gt;
  
  
  algo_logs.txt
&lt;/h6&gt;

&lt;p&gt;One txt file is to log all of the decisions that algo takes and parameters that made that decision possible.&lt;/p&gt;

&lt;h6&gt;
  
  
  portfolio_summary.txt
&lt;/h6&gt;

&lt;p&gt;And One txt file is to maintain the config, what currencies to trade in, current order statuses, available funds, what we have in portfolio, when we bought at and at which price point. All the data that the bot can read again if it restarted so it knows what it was doing, funds, and other things. This helps, when I want to change some code or something so the process needs to be restarted.&lt;/p&gt;

&lt;h4&gt;
  
  
  Notifications
&lt;/h4&gt;

&lt;p&gt;As for the notification part, I used Twilio's API for sending whatsapp messages to myself whenever the bot buys, or sells and at what price. Just to loop myself in and to know if I'm broke or not.&lt;/p&gt;

&lt;p&gt;Twilio provides a way so you can send whatsapp messages to any number, so I integrated it to send me a notification when the buy/sell order is placed and when an order gets fulfilled or rejected.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⛭ Deployment
&lt;/h2&gt;

&lt;p&gt;Since I need to run this script 24*7, I chose Amazon's EC2 instance, the basic and Free for 1 yr plan. I uploaded my code directly on the instance, setup the configs, and ran the script hoping I would not go bankrupt.&lt;/p&gt;

&lt;h2&gt;
  
  
  💰 Finally, let's count the money
&lt;/h2&gt;

&lt;p&gt;Time for the results, we all are waiting for.&lt;br&gt;
I ran the script first time with real money and let it run for one night.&lt;br&gt;
In one night, it made 18Rs. profit. I know it wasn't enough but hey it was just testing.&lt;br&gt;
So, I tuned in some more parameters and let it run for one full day. It made 84.6 rs profit on that day.&lt;br&gt;
After this, I found that if created an order but since the service restarted, it never pulled the status and it never counted it as bought. Here goes, my 342 rs.&lt;br&gt;
I fixed the bug and ran the script again with a max limit on one order of 100 rs. just to keep myself safe from huge losses.&lt;/p&gt;

&lt;p&gt;After all the initial tests and fixes, I let it run for about one week. Since I capped the order value to 100 rs. and adjusted the params defensively, it didn't made huge profits.&lt;br&gt;
In first half of the week, it made -23 rs. (yes negative)&lt;br&gt;
And after a whole week, 47 rs. profit (total)&lt;/p&gt;

&lt;p&gt;If I've increased the cap, it could've made more money (HOPE), but can't say it might screw up and I could've lost all money. &lt;/p&gt;

&lt;p&gt;Because when I checked logs, I found some bugs again which is causing the buying/selling at unfavourable prices. After my semester exams, when I started debugging it again, that's when my ssd fails and since I killed my EC2 instance, the data was also gone. (All hard work gone)&lt;/p&gt;

&lt;h2&gt;
  
  
  😅 Things I Would Change (But I can't)
&lt;/h2&gt;

&lt;p&gt;Now, that I think of what I could've improved if that project kept running?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Putting in AI into my algo, so it could make more advanced decisions.&lt;/li&gt;
&lt;li&gt;Adding in some more information like market sentiments, trending crypto's, news sentiment regarding crypto and other factors so my algo can make more informed decisions.&lt;/li&gt;
&lt;li&gt;Use proper exception handling (not just try: except: print(e) 🙈).&lt;/li&gt;
&lt;li&gt;Store logs in a database instead of a text file.&lt;/li&gt;
&lt;li&gt;Add a web dashboard for live alerts.&lt;/li&gt;
&lt;li&gt;Use design patterns like Strategy or Observer, because I now know they exist.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ✍️ Conclusion
&lt;/h2&gt;

&lt;p&gt;This bot wasn't perfect, maybe my algo was the culprit or my lack of skills that created the bugs, or why haven't I pushed my code on github or... or... or..., but it taught me more about APIs, automation, and trading logic than most tutorials ever did. Sometimes, the best way to learn is to build something just a little beyond your understanding.&lt;/p&gt;

&lt;p&gt;If you’re a dev who loves creating automation scripts and finds finance interesting — give it a shot. Build a bot. Lose some fake money. Learn a lot. 🧠&lt;/p&gt;

&lt;p&gt;Thank you for reading about one of the coolest project I built and meet you again.&lt;/p&gt;

&lt;p&gt;Umang Mittal&lt;br&gt;
Singing off !!&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>programming</category>
    </item>
    <item>
      <title>Behind the Query: The Secret Life of Database Indexes</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Sun, 04 May 2025 06:12:16 +0000</pubDate>
      <link>https://dev.to/umangmittal/behind-the-query-the-secret-life-of-database-indexes-37a6</link>
      <guid>https://dev.to/umangmittal/behind-the-query-the-secret-life-of-database-indexes-37a6</guid>
      <description>&lt;p&gt;Hi developers, I hope your database queries are not taking much time to find the records. But if they are... well, that's why I am writing this article.&lt;/p&gt;

&lt;p&gt;Before diving into the secret life of our database indexes, let's first understand why do we even need them in the first place. &lt;br&gt;
&lt;em&gt;Can't we use our database without them?&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Are my queries slow if I don't use them?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Answer is, "Yes" and "No" both. Let's look at it.&lt;/p&gt;
&lt;h1&gt;
  
  
  Why should you even care about indexes?
&lt;/h1&gt;

&lt;p&gt;Database can work without using indexes as well but think of the index as a separate lookup table where only the indexed fields are stored along with a pointer to the memory location of the actual record.&lt;br&gt;
Simply put, An index is a data structure that helps find rows fast without scanning the whole table.&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;br&gt;
Suppose you have a 1000 page dictionary with you. Now if you want to search a specific word, you don't search every page, instead you search it alphabetically which reduces the number of lookups in dictionary to find the right word.&lt;/p&gt;

&lt;p&gt;Indexes work the same way, when you create index on a field, the field is now copied in a separate collection which only holds the indexed field's values and a pointer. Now, when a query is performed on the database, the database determines which indexes to use. If it finds an index on the filtered or sorted field, then it uses index table to find the record instead of going through a million records.&lt;br&gt;
And obviously, it is fast.&lt;/p&gt;

&lt;p&gt;In a nutshell we can say, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Indexes are the difference between instant results and your app timing out."&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1&gt;
  
  
  But, How does it makes my query faster?
&lt;/h1&gt;

&lt;p&gt;To simplify this, let's take an example. As we know that all the data is written into the disk memory and in our disk, the data is stored in blocks. For our example, let's say one block stores 4 kilo bytes of data (just assume).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhk2f51xaauh4vqjzkma4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhk2f51xaauh4vqjzkma4.png" alt=" " width="800" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, Let's look at our database table which we will use for the demonstration.&lt;/p&gt;

&lt;p&gt;The table looks something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy7vjwmjydkxxa1v7mrmh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy7vjwmjydkxxa1v7mrmh.png" alt=" " width="800" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's assume one row of this table takes 400 bytes of disk storage. Now if we calculate, that means ~10 rows/records are stored in one block of disk. That means 1-10 rows are stored in block-1, 11-20 rows are stored in block-2, and so on.&lt;/p&gt;

&lt;p&gt;Our table has 1000 rows, which means 100 blocks are required to store our table.&lt;/p&gt;

&lt;p&gt;Everytime we need to read even a record, we need to read the whole block, load it into the memory and then do the operations.&lt;/p&gt;

&lt;p&gt;Okay, now our user wants all the students whose age is 16.&lt;br&gt;
If user performs this query, we need to go through each block, load it into the memory and then read the 10 rows and check every row if it contains the student with age 16. &lt;/p&gt;

&lt;p&gt;Hypothetically, let's assume that one block read takes 1 second to read. According to this, we can say that, to read 100 blocks we need 100s that means your simple query is now taking 100 seconds to fetch the results.&lt;/p&gt;

&lt;p&gt;What if we now have indexes on the field age:&lt;br&gt;
The indexed field is now copied to new collection like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fss7qhgy7qyv6a8s6irzo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fss7qhgy7qyv6a8s6irzo.png" alt=" " width="800" height="857"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's say this row takes 40 bytes of storage. And we have 1000 rows, that means 40,000 bytes we need.&lt;br&gt;
To store this in the disk, we need ~10 blocks to store the records.&lt;/p&gt;

&lt;p&gt;And now if we consider the same query, and this time the indexes are used.&lt;/p&gt;

&lt;p&gt;Read the whole index table, 10 seconds (10 blocks)&lt;br&gt;
And let's say we found 10 records and those all records are distributed in 10 blocks(worst case). To fetch these records, we need to read the whole block, means 10 seconds again.&lt;/p&gt;

&lt;p&gt;If we calculate, &lt;br&gt;
10s(to read index table) + 10s(to fetch the actual records) = 20 seconds.&lt;/p&gt;

&lt;p&gt;This is a significant gain in our case, from 100 seconds to 20 seconds. This is ~5x gain in results fetching, and that's huge. And if you scale this to 1 million rows, the effects are significant.&lt;/p&gt;

&lt;p&gt;Again, these calculations are just hypothetical, just to prove the index fields are faster and why.&lt;/p&gt;

&lt;p&gt;But hey I heard that indexes uses B-tree, and this is just a lookup table.&lt;/p&gt;

&lt;p&gt;Yes, let's get into some real optimization.&lt;br&gt;
Now, if you know that indexes are stored in B-Tree, we can divide this lookup table into a B-tree where one node(with multiple values) acts like one block of storage.&lt;/p&gt;

&lt;p&gt;Now, if I store the index table blocks (10 blocks) to a B-tree like structure(except you've always seen B-tree vertically, but here think of it as horizontally mounted B-tree).&lt;/p&gt;

&lt;p&gt;If the value is between 8 and 9 look into block 1, if it's between 9 and 10, look into block 2 and so on.&lt;br&gt;
And to store this new index-lookup table (B-tree), we only need 1 block as there are only 10 entries in the table.&lt;/p&gt;

&lt;p&gt;Then in this case, user is searching for age is 16. Then, we only read the root node of B-tree (1 block) and found that which block we have to read to get the data, then read that exact indexed table entry and then the actual records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Load B-tree in memory (1 block)
Find required pointer(pointing to the index table block) using log(n) time
Load the founded block in the memory
Finally, load all those blocks required from the main table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, this is how our table looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ab3pbzy25onqhhk3wdo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ab3pbzy25onqhhk3wdo.png" alt=" " width="800" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, if we take a look, 1 block to read the main root table (B-tree), find the  blocks where the the required block is stored that lies in user-filtered range, then read those block(s), 1 block again and then find the 10 items that are (worst case) splitted into different blocks.&lt;/p&gt;

&lt;p&gt;Calculations:&lt;br&gt;
1 block(B-tree) + 1 block(indexed records) + 10 blocks(full records) = results&lt;br&gt;
1s + 1s + 10s = 12 seconds, all the way from 100 seconds.&lt;/p&gt;

&lt;p&gt;Huh, that's a lot of things going on there. Hope, I was able to make you understand how indexes improve searching time.&lt;/p&gt;
&lt;h1&gt;
  
  
  I get the gist, but can you explain B-Tree more?
&lt;/h1&gt;

&lt;p&gt;Gotcha friend, The indexes are not stored in the table itself where it is created. It is in a separate database structure in itself. A whole collection where only indexed fields are stored. And The most common approach to store the indexes are in "Balanced Trees" (also known as B-Trees).&lt;/p&gt;

&lt;p&gt;Data is stored like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1bdtsqy2obki3j650ytp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1bdtsqy2obki3j650ytp.png" alt=" " width="800" height="398"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Image source: &lt;a href="//qwertee.io"&gt;qwertee.io&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
&lt;em&gt;If you want to learn more about B-tree index in detail, refer this &lt;a href="https://www.qwertee.io/blog/postgresql-b-tree-index-explained-part-1/" rel="noopener noreferrer"&gt;blog&lt;/a&gt; by qwertee.io.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Why B-tree? because searching time in B-tree is very less compared to other data structures.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96nkr17iaa4eipuqxifm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96nkr17iaa4eipuqxifm.png" alt=" " width="800" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might argue that Balanced BST and B-Tree are having similar times, what's the difference?&lt;/p&gt;

&lt;p&gt;Think of a B-Tree node as a block (like a disk page) — reading one node gets you many keys at once, minimizing costly I/O. The height of the tress is low as compared to Balanced BST as B-Tree has broader nodes.&lt;/p&gt;
&lt;h1&gt;
  
  
  How DB determines when to use index table or full table scan?
&lt;/h1&gt;
&lt;h3&gt;
  
  
  SQL: A Multi-Stage Process
&lt;/h3&gt;

&lt;p&gt;Relational databases like PostgreSQL, MySQL, and SQL Server follow a classic architecture for query execution:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parsing&lt;/strong&gt; – SQL is parsed into a syntax tree.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Planning/Optimization&lt;/strong&gt; – The query planner analyzes different strategies, using available indexes, stats, and constraints.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution&lt;/strong&gt; – The most efficient plan is executed, often using indexes to avoid full table scans.&lt;/p&gt;

&lt;p&gt;Example: If there's an index on email, the planner chooses an index scan instead of a sequential scan of the whole table.&lt;/p&gt;

&lt;p&gt;SQL: Parser → Optimizer → Executor → Storage Engine&lt;/p&gt;
&lt;h3&gt;
  
  
  NoSQL: Indexing is Not Optional
&lt;/h3&gt;

&lt;p&gt;In NoSQL databases like MongoDB or Cassandra, there's no table scan fallback (or it's extremely slow). Efficient querying requires indexing.&lt;/p&gt;

&lt;p&gt;MongoDB uses a query planner similar to SQL engines but works with JSON-like documents.&lt;/p&gt;

&lt;p&gt;Cassandra relies heavily on partition keys and secondary indexes, designed around high-throughput, distributed access.&lt;/p&gt;

&lt;p&gt;If there's no index in MongoDB, a full collection scan is performed—which can be expensive.&lt;/p&gt;

&lt;p&gt;NoSQL (e.g., MongoDB): Query Engine → Query Planner → Cursor Traversal.&lt;/p&gt;
&lt;h1&gt;
  
  
  How can I determine if my query is taking benefit of indexes?
&lt;/h1&gt;
&lt;h3&gt;
  
  
  MySQL – Use EXPLAIN
&lt;/h3&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPLAIN SELECT * FROM users WHERE email = 'test@example.com';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What it shows:&lt;/strong&gt;&lt;br&gt;
Whether it's using an index (type = ref or index)&lt;br&gt;
What index is being used (key)&lt;br&gt;
How many rows are being scanned&lt;/p&gt;

&lt;p&gt;To see all indexes on a table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SHOW INDEXES FROM users;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  MongoDB – Use .explain()
&lt;/h3&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.users.find({ email: "test@example.com" }).explain("executionStats")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see "stage": "IXSCAN" → you're using an index&lt;/p&gt;

&lt;p&gt;If you see "stage": "COLLSCAN" → full collection scan ❌ (slow!)&lt;/p&gt;

&lt;p&gt;To see all indexes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.users.getIndexes()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Some common pitfalls
&lt;/h1&gt;

&lt;p&gt;If you think that you apply index at every field and your database now becomes blazingly fast, where it can query in nano-seconds, fellow dev, that's not the case.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Too many indexes = slower writes
Yes, if you apply indexes on a certain field that means a index tree will be created and every time the field value gets updated, the index-ed tree needs to be updated as well to place the new value at the right place(leaf).&lt;/li&gt;
&lt;li&gt;Wrong order in composite indexes
While you can create composite indexes, which includes multiple fields, the order of the index matters. If you search in the exact order then only the indexes will be used.&lt;/li&gt;
&lt;li&gt;Indexing low-cardinality columns (e.g., gender, boolean)
If the field only has a limited variation of values like if it's a gender field or an isActive boolean flag, the index tree created is only divided into less number of branches, resulting in a normal scan. It's faster than not having an index, but not much faster.&lt;/li&gt;
&lt;li&gt;Forgetting to reindex after big migrations or bulk operations
After adding new fields or data migrations, look at the fields again to ensure that the new field is indexed (if required).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  When NOT to Use an Index
&lt;/h1&gt;

&lt;p&gt;Let's see when you should not use indexes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Small tables where full scan is faster&lt;/strong&gt;&lt;br&gt;
If the table is small, has less data, couple of thousand records then creating and maintaining indexes are heavy tasks compared to light weight searches.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Frequently updated/deleted fields (causes index maintenance overhead)&lt;/strong&gt;&lt;br&gt;
Pretty self explanatory, if an indexed field is a updates or gets deleted frequently, then indexing it might not be a great idea as it increases an index updation overhead on the database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Write-heavy workloads where read speed isn’t crucial&lt;/strong&gt;&lt;br&gt;
Indexes slow down the write operations. If the field is a write heavy field, better if leave it as is.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, let's wrap up the indexes section this time. Hope you learned something new or brushed up some already known indexes life.&lt;/p&gt;

&lt;p&gt;Let's end this post, with an un-indexed line :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An unindexed query walks into a bar. It takes forever to find a seat.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's Umang, signing off!!&lt;br&gt;
Thanks for reading this post.&lt;/p&gt;

</description>
      <category>database</category>
      <category>mongodb</category>
      <category>sql</category>
      <category>performance</category>
    </item>
    <item>
      <title>useEffect: The Hook That Keeps You Guessing (And Refreshing)</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Mon, 17 Feb 2025 06:39:23 +0000</pubDate>
      <link>https://dev.to/umangmittal/useeffect-the-hook-that-keeps-you-guessing-and-refreshing-3i0p</link>
      <guid>https://dev.to/umangmittal/useeffect-the-hook-that-keeps-you-guessing-and-refreshing-3i0p</guid>
      <description>&lt;p&gt;Hi my developer community!&lt;/p&gt;

&lt;p&gt;Let's be honest, useEffect is one the most used hook in the React Eco-system. We all have at some point in our development life used this hook for running some side-effects.&lt;/p&gt;

&lt;p&gt;Let's learn some advanced things about the useEffect.&lt;/p&gt;

&lt;p&gt;We all know that useEffect is used to trigger any side-effect in your React component.&lt;/p&gt;

&lt;p&gt;The syntax of useEffect is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect } from 'react';

useEffect(callbackFn, dependencyArr)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dependency Array
&lt;/h2&gt;

&lt;p&gt;Well, in my experience in taking interviews I asked this question a lot. What would happen if I don't pass any value in the second argument of the useEffect?&lt;/p&gt;

&lt;p&gt;And mostly candidates say, "The callback function will run infinitely". &lt;strong&gt;That is not the correct answer&lt;/strong&gt;. And it also shows how much experience they have with the useEffect hook.&lt;/p&gt;

&lt;p&gt;Let's try doing that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdvccxa76kq72mqww72z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdvccxa76kq72mqww72z.png" alt=" " width="671" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have one console log in the component's useEffect and one outside of the effect. Notice that there is not dependency array in the useEffect hook.&lt;/p&gt;

&lt;p&gt;And here is the output:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6qp6lv7gmyjkkfvzyrlc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6qp6lv7gmyjkkfvzyrlc.png" alt=" " width="565" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see there is no infinite logs. That means useEffect hook only runs on every render when there is no dependency array.&lt;/p&gt;
&lt;h2&gt;
  
  
  Cleanup Function
&lt;/h2&gt;

&lt;p&gt;Sometimes, we attach an event listener on the mount of a component. But have you wondered what happens when the component unmounts. Does that event listener gets destroyed?&lt;/p&gt;

&lt;p&gt;To demonstrate, let's attach a keyup listener on the mount of child component and unmounts the child component after 3 seconds.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fymo45zilwq8t0w910tto.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fymo45zilwq8t0w910tto.png" alt=" " width="596" height="776"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CAUTION: For demonstration purpose, I have removed the Strict Mode provided in React to demonstrate the deployed version of outputs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9z1beih3k7bf3sr0x5e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx9z1beih3k7bf3sr0x5e.png" alt=" " width="563" height="754"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, even after the child component unmounts, the event listener is still active and I can still see the logs when I press any key. That proves the fact that event listeners will still mount even when the component itself unmount.&lt;/p&gt;

&lt;p&gt;To resolve this we have a cleanup function in useEffect. This will run before the callbackFn and on unmount of the component (I know sounds confusing but I will explain).&lt;/p&gt;

&lt;p&gt;Some Dev's say it will run only when the component unmounts which is not entirely true. According to React dev docs:&lt;br&gt;
React calls your setup and cleanup functions whenever it’s necessary, which may happen multiple times:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Your setup code runs when your component is added to the page (mounts).
2. After every re-render of your component where the dependencies have changed:
    - First, your cleanup code runs with the old props and state.
    - Then, your setup code runs with the new props and state.
    - Your cleanup code runs one final time after your component is removed from the page (unmounts).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, if you have given some dependencies in the dependencyArr, then the cleanup function is called before it runs the side-effect again and in case of empty dependency array it will run on unmount.&lt;/p&gt;

&lt;h2&gt;
  
  
  Order - Order
&lt;/h2&gt;

&lt;p&gt;Now the basic things out of the way, let's understand the execution order of useEffect in a Parent-Child relationship.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flm6cd8iyc695uvrm12hr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flm6cd8iyc695uvrm12hr.png" alt=" " width="800" height="814"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this code, as you can see we have console logs out of the useEffect and inside useEffects as well. So, try to solve it without looking at the solution.&lt;br&gt;
&lt;strong&gt;Note: I have disabled React.StrictMode to make the console look cleaner, but if won't make any difference in the order of execution.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqq94531pjfbo9hvw90ul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqq94531pjfbo9hvw90ul.png" alt=" " width="800" height="785"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hmm.., If you think the Parent's useEffect should've run first, here you got the answer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why??&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;React prioritizes effects in the child components before the parent to ensure that effects affecting the child’s layout or behavior are handled first. This helps to avoid potential issues like the parent reacting to changes in the child that have not yet been fully applied.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And what would be the order of the cleanup functions, is it the same or different?&lt;br&gt;
Let's tweak our little example to see the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsks53hfewu0cqspapsit.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsks53hfewu0cqspapsit.png" alt=" " width="800" height="987"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, Now we have two useEffect's in both the components. I introduced a count variable just to re-render the parent and child and passed that as a prop to child component.&lt;/p&gt;

&lt;p&gt;And yes, the order of the useEffects being registered also matters. If both the side-effects needs to run, the one that registers first will run first.&lt;/p&gt;

&lt;p&gt;Let's see what would be the execution of this. As an exercise, you can think of a solution first before looking at the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7rv09bz82t9v8bqb4r5c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7rv09bz82t9v8bqb4r5c.png" alt=" " width="800" height="904"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;what we've understood so far seems to be working here as well. First we have both parent's and child's outer console's and then useEffect console's. Child's first and then parent's.&lt;/p&gt;

&lt;p&gt;Now, let's see what happens when you update the state. In our case, clicking on the button.&lt;/p&gt;

&lt;p&gt;I am going to put a console log "Button clicking event" to segregate the output of before the button clicked and after the button is clicked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rdhjzx6daadc8cv9ak3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rdhjzx6daadc8cv9ak3.png" alt=" " width="800" height="1110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, Parent and child render comes first.&lt;br&gt;
Next up is the cleanup functions. And yes, in the reverse order again.&lt;br&gt;
After the cleanup functions, we again have the side-effects running similarly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: For the simplicity, I've removed the React.StrictMode&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I guess that's it for the post, I hope you have gained some extra knowledge of how React works by attaching events and the order of execution for useEffect by reading this post.&lt;/p&gt;

&lt;p&gt;Thanks for reading, I'll code a bug next time.&lt;br&gt;
That's Umang, signing off.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Do you think you know Context API in React.js?</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Sun, 15 Sep 2024 16:07:05 +0000</pubDate>
      <link>https://dev.to/umangmittal/do-you-think-you-know-context-api-in-reactjs-2n1a</link>
      <guid>https://dev.to/umangmittal/do-you-think-you-know-context-api-in-reactjs-2n1a</guid>
      <description>&lt;p&gt;Hello folks! Hope you're all doing awesome.&lt;/p&gt;

&lt;p&gt;Today I want to ask you about the Context API available in React.js.&lt;br&gt;
We all know that it solves prop drilling problem and we should not use it in the complex projects and all. But why we say that. Let's explore that.&lt;/p&gt;

&lt;p&gt;So, you might be saying I already know about context API, let's skip this post. WAIT fellow developer... atleast read and find out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: I am not going in the basics of what and why we need context API.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;I am going to creating three components named Parent, Child1 and Child2. And a Counter Context for storing the counter value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm8cm79xgiflq8e1m2p4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxm8cm79xgiflq8e1m2p4.png" alt=" " width="619" height="428"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Parent Component
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7w4l3wk6d3o3bdq4ohc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7w4l3wk6d3o3bdq4ohc.png" alt=" " width="671" height="223"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Child1 Component
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0tx7y4p2pe1o45zsaa5g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0tx7y4p2pe1o45zsaa5g.png" alt=" " width="482" height="162"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Child2 Component
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzku6gsyvtvh8lryexsj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqzku6gsyvtvh8lryexsj.png" alt=" " width="502" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see we have a button in  component to increase the counter value and using the CounterContext in Child1 only.&lt;/p&gt;

&lt;p&gt;The output looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1zq0i9vt94fr61db22g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1zq0i9vt94fr61db22g.png" alt=" " width="573" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Console logs to determine the rendering flow
&lt;/h2&gt;

&lt;p&gt;Now that we've created the components, it's time to add the console log statements to determine how the rendering is happening.&lt;/p&gt;

&lt;p&gt;So, I added a simple console logs like these in all of the components:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yxhsrp0u0lu9f7utqtj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9yxhsrp0u0lu9f7utqtj.png" alt=" " width="596" height="158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokq8cwkapb82gygpphlf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fokq8cwkapb82gygpphlf.png" alt=" " width="604" height="111"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmlrsrbxzp941q80i6ndd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmlrsrbxzp941q80i6ndd.png" alt=" " width="484" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If I now open the console of the output window, below are the logs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7swiaqfsctt301f35ls9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7swiaqfsctt301f35ls9.png" alt=" " width="578" height="717"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Some might ask why we are having two same logs for one console logs statement, well that's because of the React's Strict Mode(Another topic for another day!)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Back to the topic, What do you think will be the output if I click on the Increment button?&lt;/p&gt;

&lt;p&gt;I cleared the console and clicked on Increment.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fir17ybc7j18c73iw84bq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fir17ybc7j18c73iw84bq.png" alt=" " width="566" height="616"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hmm, Same thing again.&lt;br&gt;
Well yes, whenever we change the state stored in the Context API, only the children components using the context will gets re-rendered.&lt;/p&gt;

&lt;p&gt;This dips the performance of React as it has to re-render even those who do not have any linking to the context.&lt;/p&gt;

&lt;p&gt;Next time, think about whenever you change any state in the Context API in your project because if you change the state all the components wrapped inside will get re-rendered no matter if they uses the context or not.&lt;/p&gt;

&lt;p&gt;That is one more reason people uses different state management techniques like Redux. As in Redux, if the component is using the variable that gets changed, only then it will re-render.&lt;/p&gt;

&lt;p&gt;Okay, let me ask you one more question. What if I use React.memo() to wrap my Parent, Child1 and Child2 components? What would happen?&lt;/p&gt;

&lt;p&gt;When the button is clicked, will&lt;br&gt;
No component re-renders&lt;br&gt;
Only Parent and Child2 re-renders&lt;br&gt;
Or, all of them&lt;/p&gt;

&lt;p&gt;Let's see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcj4nal78af6g7kcyn0uk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcj4nal78af6g7kcyn0uk.png" alt=" " width="679" height="661"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if clicked on Increment button:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffuwdxyx432rfxb3q42dn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffuwdxyx432rfxb3q42dn.png" alt=" " width="570" height="633"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you chose Option 2, Congratulation you got it right.&lt;/p&gt;

&lt;p&gt;That means, even if you memoize the components, and triggers an state update in the Context. Only the components that uses the context will get re-rendered.&lt;/p&gt;

&lt;p&gt;But what if I just declare the useContext hook inside the Child1 component. Not going to destructure or access any state from context. What do you think would happen?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ek5cyapcyx5v8imnh03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ek5cyapcyx5v8imnh03.png" alt=" " width="336" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now when clicked on Increment counter:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzg182konu85yfwuvmzla.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzg182konu85yfwuvmzla.png" alt=" " width="567" height="702"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, even when you are just using the context and not the state that gets triggered, the components accessing the context will get re-rendered.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The Context API solves the problem of prop drilling and is great if used in smaller contexts/applications. But when the application state gets complex or you have a large number of components accessing and updating the state, Context API is a No-No. Try using Redux or any other state management libraries and you will save yourself from a ton of re-rendering.&lt;/p&gt;

&lt;p&gt;Thanks for reading and I will see you in the next one.&lt;/p&gt;

&lt;p&gt;Keep developing your bugs.&lt;/p&gt;

&lt;p&gt;Okay, so that's Umang Mittal signing off.&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>contextapi</category>
      <category>learning</category>
    </item>
    <item>
      <title>My TOP 10 GoTo Vs code Extensions</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Sun, 01 May 2022 13:33:16 +0000</pubDate>
      <link>https://dev.to/umangmittal/my-top-10-goto-vs-code-extensions-bpe</link>
      <guid>https://dev.to/umangmittal/my-top-10-goto-vs-code-extensions-bpe</guid>
      <description>&lt;p&gt;Hello folks! Hope you're all doing awesome.&lt;br&gt;
Today I want to share with you guys my most favourite and most useful VSCode Extensions that increases my productivity as well as the look and feel of my code editor.&lt;/p&gt;

&lt;p&gt;I am going to divide the extensions into parts, which affects my design of the editor, which increases my productivity, which are a must to have, etc.&lt;/p&gt;

&lt;p&gt;These extensions are my personal choices which might differ from yours, which I totally agree. These extensions help me to get better out of my coding time. &lt;br&gt;
So, without a futherado, let's roll in to see what I was talking about.&lt;/p&gt;
&lt;h2&gt;
  
  
  Section1: Setting your VS code Theme
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Atom Material Theme &lt;em&gt;by Tobias Althoff&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
This extension provides Atom inspired color theme which in my opinion looks dope. I rock this in all of my vs code fragments whether I am on my laptop or desktop. &lt;br&gt;
I like the way it looks and feels. Here's a look at it.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnyiurferc3ieyv060ek3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnyiurferc3ieyv060ek3.png" alt="Atom Material theme Demo" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Material Icon Theme &lt;em&gt;by Phillipp Kief&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The Material Icon theme is one of the great extension out in the market which has over 52 Million downloads and still growing. It offers various icon themes for every file extension and also the standard folder icons which automatically change depending upon the name of folder like "assets", "images","android", "gradle","components" and what not.&lt;br&gt;
See Pictures below.&lt;br&gt;
File Icons&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FPKief%2Fvscode-material-icon-theme%2Fmain%2Fimages%2FfileIcons.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2FPKief%2Fvscode-material-icon-theme%2Fmain%2Fimages%2FfileIcons.png" alt="Material Icon Theme file Icons" width="800" height="4033"&gt;&lt;/a&gt;&lt;br&gt;
Folder Icons&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fme2bfpaa5an4pb2t5u7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fme2bfpaa5an4pb2t5u7w.png" alt="Material Icon Theme folder Icons" width="800" height="1571"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Section 2 : Productivity Increasers
&lt;/h2&gt;

&lt;p&gt;This section is devoted to the extensions that increases my productivity either by adding some common shortcuts or by doing some underling task.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Better Comments &lt;em&gt;by Aaron Bond&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
This extension, as the name suggests, help you create more human-friendly comments in your code.With this extension, you will be able to categorise your annotations into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Alerts&lt;/li&gt;
&lt;li&gt;Queries&lt;/li&gt;
&lt;li&gt;TODOs&lt;/li&gt;
&lt;li&gt;Highlights&lt;/li&gt;
&lt;li&gt;Commented out code can also be styled to make it clear the code shouldn't be there&lt;/li&gt;
&lt;li&gt;Any other comment styles you'd like can be specified in the settings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this VS extension, you can use the following characters after a double forward slash // to add easier commenting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;* for highlighted text&lt;/li&gt;
&lt;li&gt;! for errors and warnings&lt;/li&gt;
&lt;li&gt;? for queries and questions&lt;/li&gt;
&lt;li&gt;// for strikethrough&lt;/li&gt;
&lt;li&gt;TODO for to-dos
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9z62mot49tw6p249zos.png" alt="Better Comments" width="800" height="380"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Auto Rename Tag &lt;em&gt;by Jun Han&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
This Vs code extension is very useful if you are a web developer as it automatically rename the paired HTML/XML tag.&lt;br&gt;
I like this extension because I don't have to manually change the other HTML tag if I've changed the paired one. Well, obviously if you changed one tag you have to change the other, right?&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzbfzdf55aby4m8imx55g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzbfzdf55aby4m8imx55g.gif" alt="Auto Rename Tag Gif" width="1440" height="938"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Live Server &lt;em&gt;by Ritwick Dey&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The next extension we have on the list is probably the most used and well-known extension in the web community. &lt;br&gt;
If you've ever write HTML/CSS, you know every time you make changes you have to save those changes and reload the web page to see those new changes.&lt;br&gt;
But, With the help of this extension we do not have to reload the webpage anymore, it adds a script (behind the scene) in the end of your HTML document which helps it auto reload whenever you save the changes.&lt;br&gt;
Here's the demo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fau5nwv7xzxg70t53mvzf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fau5nwv7xzxg70t53mvzf.gif" alt="Live Server vs code Extension" width="760" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Prettier &lt;em&gt;by Prettier&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The Prettier extension here does not need any introduction. This is one of the most used extensions out in the market. Prettier, as the name suggests, formats the document whenever you hit the ctrl+s. It automatically beautifies the document.&lt;br&gt;
Here's what is written on the official documentation page of Prettier.&lt;br&gt;
"Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary."&lt;br&gt;
The great thing about Prettier is that it supports a ton of languages and editors which makes it stand where it is standing now.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpbrz2dt9won3ws052q28.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpbrz2dt9won3ws052q28.gif" alt="Prettier Gif" width="1000" height="481"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Section 3: A Must to have and productivity multipliers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;7. Advanced New file &lt;em&gt;by Patbenatar&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
This extension gives you the power to create a new file without even touching your mouse/touchpad which in turn decreases the time it takes to switch between keyboard and mouse/touchpad.&lt;br&gt;
Features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fuzzy-matching autocomplete to create new file relative to existing path&lt;/li&gt;
&lt;li&gt;Create new directories while creating a new file&lt;/li&gt;
&lt;li&gt;Create a directory instead of a file by suffixing the file path with / as in somedirectory/ to create the directory &lt;/li&gt;
&lt;li&gt;Ignores gitignored and workspace files.exclude settings.&lt;/li&gt;
&lt;li&gt;Additional option of adding advancedNewFile.exclude settings to workspace settings just like native files.exlude except it explicitly effects AdvancedNewFile plugin only.&lt;/li&gt;
&lt;li&gt;Control the order of top convenient options ("last selection", "current file", etc) via config setting advancedNewFile.convenienceOptions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After installation, hit "ctrl+n" whenever you want to create a new file, provide the folder in which you want to create the file, press enter, now provide the file name with the extension to create the file, you can also create a folder just add "/" at the end of the new file and extension will create a new folder like: folder/filename.fileextenstion.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5eirexln9pkkct30khg2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5eirexln9pkkct30khg2.gif" alt="Adevanced New File" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Git Lens &lt;em&gt;by GitKraken&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
If you are working with the git and with a team of programmers, you definitely wants to see this extension. This does not add git into vscode, rather is supercharges the git by adding some cool new features. &lt;/p&gt;

&lt;p&gt;It helps you to visualize code authorship at a glance via Git blame annotations and CodeLens, seamlessly navigate and explore Git repositories, gain valuable insights via rich visualizations and powerful comparison commands, and so much more.&lt;/p&gt;

&lt;p&gt;GitLens simply helps you better understand code. Quickly glimpse into whom, why, and when a line or code block was changed. Jump back through history to gain further insights as to how and why the code evolved. Effortlessly explore the history and evolution of a codebase.&lt;/p&gt;

&lt;p&gt;Current Line Blame&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs5jnyri0wf5gmm6cjcls.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs5jnyri0wf5gmm6cjcls.png" alt="Gitlens" width="610" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Git Code Lens&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gwtb807bbi71al5f1fr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8gwtb807bbi71al5f1fr.png" alt="Gitlens" width="610" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And Much more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Quokka.js &lt;em&gt;by Wallaby.js&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
The Quokka.js extension is provided by Wallby.js. It is a javascript/typescript playground which can make your life a lot easier if you use pure JS/TS. &lt;br&gt;
Quokka.js runs JavaScript and TypeScript with instant feedback. Runtime values are updated and displayed in your editor next to your code, as you type.&lt;/p&gt;

&lt;p&gt;Code runs immediately as you type, on unsaved changes; no need to do anything manually or switch context. Error messages are displayed right next to the code that caused them. Console logs and identifier expression values are displayed inline as well.&lt;/p&gt;

&lt;p&gt;It offers some cool features like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Live Execution and results&lt;/li&gt;
&lt;li&gt;Live coverage&lt;/li&gt;
&lt;li&gt;Time Machine&lt;/li&gt;
&lt;li&gt;Code Stories&lt;/li&gt;
&lt;li&gt;Value Explorer&lt;/li&gt;
&lt;li&gt;Live Comments and values&lt;/li&gt;
&lt;li&gt;Project Files Import&lt;/li&gt;
&lt;li&gt;CPU Profiler&lt;/li&gt;
&lt;li&gt;Quick Package Install
&lt;em&gt;Note: some of these features are only available in pro version.&lt;/em&gt;
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fccrcn5ffp3qzcw21gd2v.gif" alt="QuokkaJs" width="600" height="460"&gt;
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb4uhpc9goky9ilb1gd2e.gif" alt="QuokkaJs" width="600" height="460"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;10. Turbo Console log &lt;em&gt;by ChakrounAnas&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Last but not least is the Turbo Console Log. This is one of the most used extension by me and my team members.&lt;br&gt;
Have you ever had to log the variable to the console to know what is the response of the network call, what is the value of this variable at this point of time. And after writing many console logs, when you open up your browser's console, you see a ton of consoles with no meaning.&lt;br&gt;
I know I've been there. Turbo console to the rescue.&lt;br&gt;
This extension make debugging much easier by automating the operation of writing meaningful log message.&lt;br&gt;
Suppose, you want to log the value of param in this function foo. If you have this extension installed and enables, just select the variable and hit the shortcut- "ctrl+alt+l" and you will get the message like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🚀 ~ file: demo.js ~ line 2 ~ foo ~ params&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This message is more meaningful as it exactly tells from where this log is generating,right? It has filename, line number,function name, and the variable's name in it.&lt;/p&gt;

&lt;h2&gt;
  
  
  PS
&lt;/h2&gt;

&lt;p&gt;So, that was my top 10 vs code extensions that I rock all the time whenever I code, they not only increases my productivity but also adds a bit more elegance to my code editor.&lt;/p&gt;

&lt;p&gt;Hope you liked them. If you have some recommendation, just let me know in the comments. I will definitely try them and probably include it the next time I create a extension list.&lt;/p&gt;

&lt;p&gt;Okay, So that's Umang Mittal signing off! &lt;/p&gt;

</description>
      <category>vscode</category>
      <category>tooling</category>
    </item>
    <item>
      <title>The Rest and the Spread operator of JS</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Mon, 25 Apr 2022 01:56:41 +0000</pubDate>
      <link>https://dev.to/umangmittal/the-rest-and-the-spread-operator-of-js-8an</link>
      <guid>https://dev.to/umangmittal/the-rest-and-the-spread-operator-of-js-8an</guid>
      <description>&lt;p&gt;Hello folks! I hope you are all doing great.&lt;br&gt;
Ever heard developers talking about "spread" operators or just saw "..." [Triple Dots] in JS code and wondered what is this thing.&lt;br&gt;
Well, we're going to cover that today.&lt;/p&gt;

&lt;p&gt;The syntax of rest and spread operator looks exactly the same but they are polar opposite of each other in terms of functionality.&lt;/p&gt;

&lt;p&gt;Let's look at both of them one by one.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Rest operator
&lt;/h2&gt;

&lt;p&gt;The rest operator is used to take an indefinite number of arguments in a function and condenses it to an array.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// [ 1, 2, 3, 4 ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// [ 1, 2 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whatever the number of parameters you pass into a function the rest operator collects all the arguments and "condenses" them into an Array which can be useful in a lot of cases.&lt;/p&gt;

&lt;p&gt;It can also be used like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 1&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [ 3, 4 ]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we've done is told the function that we are going to pass "a", "b" and whatever we pass after that, condense it into an array.&lt;br&gt;
Now, you might me thinking of some edge cases like "what will happen if I ... ". Don't worry I am getting on that too.&lt;br&gt;
&lt;em&gt;What happens if I pass no value for the rest oprator?&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 1&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 2&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// []&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the rest parameter does not gets any value which results in an empty array.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But, what happens if I shift the rest parameter to the first or the second position?&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;zen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,...&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;  &lt;span class="c1"&gt;// SyntaxError: Rest parameter must be last formal parameter&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;zen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will not run. The reason is clearly mentioned in the error. &lt;br&gt;
The rest parameter must be the last function argument.&lt;/p&gt;

&lt;p&gt;Now that we know, what is rest operator. Let's look at a use-case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Suppose, you were said to create a function that takes two values and returns the sum of them. Easy? isn't it. Let's escalate it, Now you need to create the same function but there could be any possible number of parameters and your function must return the sum of all of the arguments.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One approach is that you create different functions for different number of parameters. Now that you know the rest operator, you must'nt worry about that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addValues&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;addValues&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;// 15&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: I know I could've used foreach or even reduce method to do the same task but to keep this post general for all the people, I used the for loop.Well, another topic for another day.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Spread operator
&lt;/h2&gt;

&lt;p&gt;Earlier, I told you that both rest and spread has same syntax and still they are opposites of each other.&lt;br&gt;
Where rest operator takes multiple values and condenses them , the spread opeartor expands the object values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// [ 1, 2, 3, 4 ]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// 1 2 3 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see the spread operator spreads the value of the array and it also works on the object.&lt;/p&gt;

&lt;p&gt;Spread Operator can be used in many different tasks like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Copying an array&lt;/li&gt;
&lt;li&gt;Adding to state in React&lt;/li&gt;
&lt;li&gt;Concatenating or combining arrays&lt;/li&gt;
&lt;li&gt;Using Math functions&lt;/li&gt;
&lt;li&gt;Converting NodeList to an array&lt;/li&gt;
&lt;li&gt;Using an array as arguments&lt;/li&gt;
&lt;li&gt;Adding an item to a list&lt;/li&gt;
&lt;li&gt;Combining objects &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The spread works on the objects too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// { hello: 'world', foo: 'bar' }&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// TypeError: Found non-callable @@iterator&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;  &lt;span class="c1"&gt;// { hello: 'world', foo: 'bar' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can create a clone of an object and updates it's value with the help of it's key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Creating array&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;  &lt;span class="c1"&gt;// [ 1, 2, 3, 4, 5 ]&lt;/span&gt;

&lt;span class="c1"&gt;// Creating Object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({...&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;zen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;  &lt;span class="c1"&gt;// { hello: 'world', foo: 'zen' }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;The rest parameters and the spread operator has become an integral part of any web project since it's launch. I hope I made both of them clear to you. &lt;br&gt;
Thanks for reading.&lt;/p&gt;

&lt;p&gt;Okay, so that's Umang Mittal signing off.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript is a little weird!!</title>
      <dc:creator>Umang Mittal</dc:creator>
      <pubDate>Wed, 20 Apr 2022 03:17:43 +0000</pubDate>
      <link>https://dev.to/umangmittal/javascript-is-a-little-weird-47pf</link>
      <guid>https://dev.to/umangmittal/javascript-is-a-little-weird-47pf</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hey Folks, Hope you're all doing great.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Today, I wanted to talk about some weird things that are really really weird about Javascript language and not only in JS, some of them are also applicable in other programming languages.&lt;/p&gt;

&lt;p&gt;Let's start with the basics.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Javascript?
&lt;/h2&gt;

&lt;p&gt;Well, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript" rel="noopener noreferrer"&gt;Javascript&lt;/a&gt; is a scripting language that conforms to the ECMAScript standard. And you know, its initial release was built in only ten days back in December, 1995.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I was talking about.
&lt;/h2&gt;

&lt;p&gt;But Js is a little bit wierd and it doesn't always behave the way you might think and does not produce the expected output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: Even senior JS developers don't use some of these syntax'es. This post is just for showing how quirky js can get.&lt;/strong&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Talk is cheap, show me the code
&lt;/h2&gt;

&lt;p&gt;Here you go,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output: bar&lt;br&gt;
Try to expect the output, you might say it will log out "Foo". But that's the catch, if you do something like this you can see why it logs "bar".&lt;/p&gt;

&lt;p&gt;That's just the problems with the programming languages not particularly with js. Programming languages lacks the base 10 accuracy. Let's see what I mean.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;//0.30000000000000004&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So obviously, 0.30000000000000004 is not equals to 0.3&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Okay, the next one.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([,].&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the array has 2 elements, right? One before the comma and one after that (Even though they are empty). But JS treats this as only 1 element. In Js, until and unless you pass the value to the last position (after the last comma) of the array, it will not treat this as a value and will not insert it into the array but that's not the case with the other positions.&lt;br&gt;
The last comma is called the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas" rel="noopener noreferrer"&gt;trailing comma&lt;/a&gt;&lt;br&gt;
And you will find it everywhere in the JS code, like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;example&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 👈🏻 here you can see a trailing comma&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's the example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,].&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([,,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;//3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, when you pass nothing in the last position, the array's length is 2 but when you pass only the last position it still counts all the other values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One More&lt;/strong&gt;&lt;br&gt;
We all at one or the other point in time of our coding journey used increment operator.&lt;br&gt;
Let's look at that in JS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//Error&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, to be honest, it's giving an error not because of JS, but due to the fact that we are trying to mutate an already defined value.&lt;/p&gt;

&lt;p&gt;You might say that increment operator might not exists in Js. But let me tell you that the syntax is absolutely correct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//2 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a valid syntax but it only works with variables. &lt;br&gt;
And this is not the case with Js only, it works the same way with Java, C and many more programming languages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The last one&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;//1&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;//2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator" rel="noopener noreferrer"&gt;comma operator&lt;/a&gt; evaluates its operands, from left to right, but returns only the last operand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The definitely last one&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// a,b,c1,2,3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This happens because when you try to concat two array's, they will first get converted into strings and are then concatenated.&lt;/p&gt;

&lt;p&gt;If you really need to concat two arrays, you should use the "spread" operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="p"&gt;]);&lt;/span&gt;  &lt;span class="c1"&gt;//[ 'a', 'b', 'c', 1, 2, 3 ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;So, I think you all might agree with me on the fact that js is a little funny language and does not works the same we think it does.&lt;/p&gt;

&lt;p&gt;If you think Js is not wierd enough, please let me know in the comments below and I will probably make another post about the more quirks that Js has.&lt;/p&gt;

&lt;p&gt;Okay, so that's Umang Mittal signing off.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
