<?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: Ashay Tiwari</title>
    <description>The latest articles on DEV Community by Ashay Tiwari (@ashay_tiwari_3658168ad5db).</description>
    <link>https://dev.to/ashay_tiwari_3658168ad5db</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3114225%2Fecf3ef8c-a7e9-4872-a8bf-039005be28df.jpg</url>
      <title>DEV Community: Ashay Tiwari</title>
      <link>https://dev.to/ashay_tiwari_3658168ad5db</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashay_tiwari_3658168ad5db"/>
    <language>en</language>
    <item>
      <title>Inheritance in OOP: Solving Code Duplication or Creating New Problems?</title>
      <dc:creator>Ashay Tiwari</dc:creator>
      <pubDate>Thu, 25 Jun 2026 07:29:46 +0000</pubDate>
      <link>https://dev.to/ashay_tiwari_3658168ad5db/inheritance-in-oop-solving-code-duplication-or-creating-new-problems-2d4d</link>
      <guid>https://dev.to/ashay_tiwari_3658168ad5db/inheritance-in-oop-solving-code-duplication-or-creating-new-problems-2d4d</guid>
      <description>&lt;p&gt;Many developers learn:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Inheritance allows code reuse.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Technically true.&lt;/p&gt;

&lt;p&gt;But if that's your primary reason for using inheritance, you'll often create bad designs.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;❓ Why Was Inheritance Invented?&lt;/strong&gt;
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ├── Customer
 ├── Admin
 └── Seller
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All of them have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;id&lt;/li&gt;
&lt;li&gt;name&lt;/li&gt;
&lt;li&gt;email&lt;/li&gt;
&lt;li&gt;login()&lt;/li&gt;
&lt;li&gt;logout()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of duplicating these everywhere:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Customer&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;Admin&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;Seller&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;we extract common behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;id&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="nl"&gt;name&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="nl"&gt;email&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="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="nf"&gt;logout&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;Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;User&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;Admin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;User&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;Seller&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates an &lt;strong&gt;IS-A relationship&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Admin IS A User

Customer IS A User

Seller IS A User
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the most important rule of inheritance.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Does &lt;code&gt;extends&lt;/code&gt; Actually Do?
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;login&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;login&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Admin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;admin&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;Admin&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;works.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because JavaScript walks up the prototype chain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;admin
   ↓
Admin.prototype
   ↓
User.prototype
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and finds &lt;code&gt;login()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Inheritance is fundamentally prototype delegation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
   ↓
Admin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Admin has everything a User has, plus additional capabilities.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;login&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Admin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;banUser&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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;banUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Overriding Methods&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Children can change behavior.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;getRole&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;User&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Admin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;getRole&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Admin&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;getRole&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// User&lt;/span&gt;

&lt;span class="nf"&gt;newAdmin&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;getRole&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// Admin&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same method name.&lt;/p&gt;

&lt;p&gt;Different behavior.&lt;/p&gt;

&lt;p&gt;This becomes important later when we discuss polymorphism.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🤯 The Biggest Mistake with Inheritance&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Hidden Problem&lt;/p&gt;

&lt;p&gt;Over time, developers started encountering a different challenge.&lt;/p&gt;

&lt;p&gt;Imagine we have a Vehicle class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="nf"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="nf"&gt;lockDoors&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;Seems harmless.&lt;/p&gt;

&lt;p&gt;But what happens when we introduce a Bicycle?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bicycle&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Vehicle&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bicycles don't have doors.&lt;/p&gt;

&lt;p&gt;Yet they've inherited door-related behavior.&lt;/p&gt;

&lt;p&gt;Now our model starts feeling strange.&lt;/p&gt;

&lt;p&gt;The problem becomes even worse as systems grow.&lt;/p&gt;

&lt;p&gt;Developers often found themselves inheriting behavior they didn't actually need.&lt;/p&gt;

&lt;p&gt;Changes in parent classes unexpectedly affected child classes.&lt;/p&gt;

&lt;p&gt;The hierarchy became increasingly difficult to maintain.&lt;/p&gt;

&lt;p&gt;What started as a solution to code duplication sometimes created tightly coupled systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;⚔️ The Double-Edged Sword&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Inheritance solves one problem very well:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reusing common behavior.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But it can introduce another:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Tight coupling between parent and child classes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The child depends heavily on decisions made by the parent.&lt;/p&gt;

&lt;p&gt;As hierarchies become deeper, understanding the system becomes harder.&lt;/p&gt;

&lt;p&gt;A small change in a base class can ripple through multiple child classes.&lt;/p&gt;

&lt;p&gt;This is one reason why modern software design is often more cautious about using inheritance.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;✅ When Inheritance Is Actually Good&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Use inheritance when:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Real IS-A relationship exists
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Admin IS A User

Manager IS An Employee

Dog IS An Animal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Parent behavior is truly shared
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="nf"&gt;logout&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;All child classes genuinely need this.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Hierarchy is shallow
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ├─ Admin
 └─ Customer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User
 ↓
Employee
 ↓
Manager
 ↓
RegionalManager
 ↓
DistrictManager
 ↓
SuperDistrictManager
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;⏭️ What's Next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Inheritance helped developers reuse behavior.&lt;/p&gt;

&lt;p&gt;But over time, many teams discovered that inheritance wasn't always the most flexible solution.&lt;/p&gt;

&lt;p&gt;This led to an important shift in software design thinking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Instead of inheriting behavior, what if we assembled behavior from smaller pieces?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the next article, we'll explore Composition vs Inheritance and understand why many modern applications prefer composition when building complex systems.&lt;/p&gt;

</description>
      <category>inheritance</category>
      <category>classes</category>
      <category>oop</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Abstraction in OOP: Hiding Complexity Without Hiding Capability</title>
      <dc:creator>Ashay Tiwari</dc:creator>
      <pubDate>Wed, 17 Jun 2026 12:24:42 +0000</pubDate>
      <link>https://dev.to/ashay_tiwari_3658168ad5db/abstraction-in-oop-hiding-complexity-without-hiding-capability-1111</link>
      <guid>https://dev.to/ashay_tiwari_3658168ad5db/abstraction-in-oop-hiding-complexity-without-hiding-capability-1111</guid>
      <description>&lt;p&gt;In the previous article, we discussed Encapsulation and how objects should control changes to their own state.&lt;/p&gt;

&lt;p&gt;Now let's talk about another fundamental concept of Object-Oriented Programming: Abstraction.&lt;/p&gt;

&lt;p&gt;Like always, let's start with a problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine you own a car.&lt;/p&gt;

&lt;p&gt;To drive it, you do a few simple things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start the engine.&lt;/li&gt;
&lt;li&gt;Press the accelerator.&lt;/li&gt;
&lt;li&gt;Apply the brakes.&lt;/li&gt;
&lt;li&gt;Turn the steering wheel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You don't need to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How fuel is injected into the engine.&lt;/li&gt;
&lt;li&gt;How the pistons move.&lt;/li&gt;
&lt;li&gt;How the gearbox changes gears.&lt;/li&gt;
&lt;li&gt;How combustion generates power.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All that complexity is hidden from you.&lt;/p&gt;

&lt;p&gt;You simply interact with a straightforward interface.&lt;/p&gt;

&lt;p&gt;Software systems work in a very similar way.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;A Real-World Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Suppose we need to send an email.&lt;/p&gt;

&lt;p&gt;The application code might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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="na"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;john@example.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;subject&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="na"&gt;body&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 John&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;Looks simple.&lt;/p&gt;

&lt;p&gt;But what actually happens behind the scenes?&lt;/p&gt;

&lt;p&gt;Potentially a lot:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Validate the email address.&lt;/li&gt;
&lt;li&gt;Create an SMTP connection.&lt;/li&gt;
&lt;li&gt;Authenticate with the provider.&lt;/li&gt;
&lt;li&gt;Build the message.&lt;/li&gt;
&lt;li&gt;Retry if sending fails.&lt;/li&gt;
&lt;li&gt;Log failures.&lt;/li&gt;
&lt;li&gt;Close the connection.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As consumers of email service, we don't care about those details.&lt;/p&gt;

&lt;p&gt;We only care that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If I call send(), the email should be sent.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is exactly the problem Abstraction tries to solve.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Problem with Exposing Everything&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine if sending an email required every caller to do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;buildMessage&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="nf"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every single time.&lt;/p&gt;

&lt;p&gt;Suddenly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every consumer knows too much.&lt;/li&gt;
&lt;li&gt;Every consumer is responsible for the implementation.&lt;/li&gt;
&lt;li&gt;Changing the implementation becomes painful.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What if we switch from SMTP to a third-party email provider?&lt;/p&gt;

&lt;p&gt;Now every place that sends emails needs to change.&lt;/p&gt;

&lt;p&gt;The consumers have become tightly coupled to implementation details.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Idea Behind Abstraction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Abstraction is about exposing what an object can do while hiding how it does it.&lt;/p&gt;

&lt;p&gt;Instead of exposing every implementation detail, we expose only what consumers need.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The caller doesn't need to know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which provider we're using.&lt;/li&gt;
&lt;li&gt;How authentication works.&lt;/li&gt;
&lt;li&gt;How retries are handled.&lt;/li&gt;
&lt;li&gt;How messages are constructed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those details remain hidden.&lt;/p&gt;

&lt;p&gt;Consumers interact with a simple and stable interface.&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstraction Is Not the Same as Encapsulation
&lt;/h3&gt;

&lt;p&gt;These two concepts are often confused.&lt;/p&gt;

&lt;p&gt;Encapsulation asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Who should be allowed to change the object's state?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Abstraction asks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What details should consumers be aware of?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Encapsulation protects data.&lt;/p&gt;

&lt;p&gt;Abstraction hides unnecessary complexity.&lt;/p&gt;

&lt;p&gt;They often work together, but they solve different problems.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What's Next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So far, we've discussed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How objects protect their state using Encapsulation.&lt;/li&gt;
&lt;li&gt;How objects hide complexity using Abstraction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next, we'll look at another question developers faced:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If multiple objects share similar behavior, do we really need to write that code repeatedly?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This leads us to one of the most well-known—and sometimes controversial—concepts in OOP:&lt;/p&gt;

&lt;p&gt;Inheritance.&lt;/p&gt;

</description>
      <category>abstraction</category>
      <category>classes</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Encapsulation in OOP: Protecting an Object's State from Unintended Changes</title>
      <dc:creator>Ashay Tiwari</dc:creator>
      <pubDate>Mon, 15 Jun 2026 08:16:37 +0000</pubDate>
      <link>https://dev.to/ashay_tiwari_3658168ad5db/encapsulation-in-oop-protecting-an-objects-state-from-unintended-changes-18a2</link>
      <guid>https://dev.to/ashay_tiwari_3658168ad5db/encapsulation-in-oop-protecting-an-objects-state-from-unintended-changes-18a2</guid>
      <description>&lt;p&gt;In the previous articles, we discussed why Object-Oriented Programming was introduced and explored different ways of creating objects using factory functions and classes.&lt;/p&gt;

&lt;p&gt;Now it's time to look at one of the most important ideas behind OOP: Encapsulation.&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%2F9a9ggqht66ngh0l8vp45.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%2F9a9ggqht66ngh0l8vp45.png" alt="Encapsulation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like every concept in this series, let's start with the problem rather than the definition.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Real Problem Encapsulation Solves&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine you're building a bank account.&lt;/p&gt;

&lt;p&gt;Without encapsulation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&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;account&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;BankAccount&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your account contains:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;50000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which may be impossible according to business rules.&lt;/p&gt;

&lt;p&gt;The problem isn't that someone changed a variable.&lt;/p&gt;

&lt;p&gt;The problem is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Anybody can put the object into an invalid state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Encapsulation is NOT About Hiding
&lt;/h3&gt;

&lt;p&gt;Most beginners think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Encapsulation = private fields&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Private fields are just one tool.&lt;/p&gt;

&lt;p&gt;The actual goal is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Protecting the integrity of an object's state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An object should control how its data changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why This Becomes Dangerous&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In small applications, unrestricted access may not seem like a big deal.&lt;/p&gt;

&lt;p&gt;As applications grow, however, data starts being accessed from many places.&lt;/p&gt;

&lt;p&gt;Imagine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A payment module updating balances.&lt;/li&gt;
&lt;li&gt;A refund module updating balances.&lt;/li&gt;
&lt;li&gt;An admin panel updating balances.&lt;/li&gt;
&lt;li&gt;A reporting service reading balances.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now suppose an issue appears.&lt;/p&gt;

&lt;p&gt;A customer's balance becomes negative.&lt;/p&gt;

&lt;p&gt;Where did it happen?&lt;/p&gt;

&lt;p&gt;Which part of the application modified it?&lt;/p&gt;

&lt;p&gt;Was validation skipped?&lt;/p&gt;

&lt;p&gt;Did someone accidentally overwrite a value?&lt;/p&gt;

&lt;p&gt;The more places that can directly manipulate data, the harder it becomes to reason about the system.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;A Better Design&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;force all changes through methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&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;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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;balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;&amp;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;balance&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="s1"&gt;Insufficient funds&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getBalance&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is allowed.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt;&lt;span class="o"&gt;=-&lt;/span&gt;&lt;span class="mi"&gt;50000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is impossible.&lt;/p&gt;

&lt;p&gt;The object protects itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Think Like a Security Guard
&lt;/h3&gt;

&lt;p&gt;Imagine a company database.&lt;/p&gt;

&lt;p&gt;Bad design:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Employee Database
      ↑
Everybody can modify it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good design:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Employee Database
      ↑
HR Department
      ↑
Employees submit requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Employees don't directly change records.&lt;/p&gt;

&lt;p&gt;They request changes.&lt;/p&gt;

&lt;p&gt;The HR department validates them.&lt;/p&gt;

&lt;p&gt;Encapsulation works exactly like this.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Idea Behind Encapsulation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Encapsulation is about controlling access to an object's internal state.&lt;/p&gt;

&lt;p&gt;Instead of allowing anyone to modify data directly, the object becomes responsible for managing its own data.&lt;/p&gt;

&lt;p&gt;Rather than exposing the balance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we expose behaviors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now every change passes through rules defined by the object itself.&lt;/p&gt;

&lt;p&gt;The object protects its own integrity.&lt;/p&gt;

&lt;p&gt;This is the real purpose of Encapsulation.&lt;/p&gt;

&lt;p&gt;Not hiding data.&lt;/p&gt;

&lt;p&gt;Protecting data.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Encapsulation in TypeScript&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;TypeScript gives us access modifiers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;name&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;private&lt;/span&gt; &lt;span class="nx"&gt;password&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;protected&lt;/span&gt; &lt;span class="nx"&gt;role&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Accessible everywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Accessible only inside the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.password
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❌ Error&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;protected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Accessible inside:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;current class&lt;/li&gt;
&lt;li&gt;derived classes
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;user&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Admin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;checkRole&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&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;Works.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Important Reality About TypeScript&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Many developers are surprised by this.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is primarily a compile-time restriction.&lt;/p&gt;

&lt;p&gt;After compilation to JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;still exists.&lt;/p&gt;

&lt;p&gt;TypeScript prevents you from accessing it in code, but it's not true runtime privacy.&lt;/p&gt;

&lt;p&gt;Modern JavaScript introduced:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which is actual runtime privacy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;balance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is impossible.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Key Takeaway&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Encapsulation is often described as &lt;strong&gt;hiding data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A more useful way to think about it is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Encapsulation ensures that an object controls how its own state changes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By doing so, it protects business rules, reduces accidental misuse, and makes software easier to maintain as systems grow.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What's Next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Encapsulation helps us control access to data.&lt;/p&gt;

&lt;p&gt;But another challenge still remains.&lt;/p&gt;

&lt;p&gt;Even when objects manage their own state correctly, consumers often need to understand too many implementation details to use them.&lt;/p&gt;

&lt;p&gt;In the next article, we'll explore Abstraction and see how hiding unnecessary complexity allows us to build systems that are easier to use and easier to change.&lt;/p&gt;




</description>
      <category>oop</category>
      <category>encapsulation</category>
      <category>javascript</category>
      <category>classes</category>
    </item>
    <item>
      <title>Factory Functions vs Classes: If Classes Exist, Why Do We Still Use Factory Functions?</title>
      <dc:creator>Ashay Tiwari</dc:creator>
      <pubDate>Wed, 10 Jun 2026 06:03:28 +0000</pubDate>
      <link>https://dev.to/ashay_tiwari_3658168ad5db/factory-functions-vs-classes-if-classes-exist-why-do-we-still-use-factory-functions-6c2</link>
      <guid>https://dev.to/ashay_tiwari_3658168ad5db/factory-functions-vs-classes-if-classes-exist-why-do-we-still-use-factory-functions-6c2</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;❓ A Question That Confused Me&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As we continue learning Object-Oriented Programming, I want to pause for a moment and discuss something that often comes up for JavaScript developers.&lt;/p&gt;

&lt;p&gt;When we hear about OOP, we usually hear about classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&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;name&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&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;We've discussed that classes are used to create objects.&lt;/p&gt;

&lt;p&gt;Fair enough.&lt;/p&gt;

&lt;p&gt;But then we look at JavaScript codebases and see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createUser&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No class.&lt;br&gt;
No constructor.&lt;br&gt;
No new keyword.&lt;/p&gt;

&lt;p&gt;And yet, we're still creating objects.&lt;/p&gt;

&lt;p&gt;This raises a very reasonable question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What is this called? And if these kinds of functions can create objects, why do classes exist?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To answer that, let's first understand the problem developers were trying to solve.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;The Simplest Possible Approach&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Suppose we're building an application and need user objects.&lt;/p&gt;

&lt;p&gt;Our first instinct might be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ashay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ashay@gmail.com&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;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rahul@gmail.com&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;Looks fine.&lt;/p&gt;

&lt;p&gt;Until we realize that every user should also be able to update their email.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ashay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ashay@gmail.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;updateEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newEmail&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;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newEmail&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;Then another user.&lt;/p&gt;

&lt;p&gt;And another.&lt;/p&gt;

&lt;p&gt;And another.&lt;/p&gt;

&lt;p&gt;Soon, we're repeating the same structure everywhere.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The First Problem: Repetition&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The problem isn't creating one object.&lt;/p&gt;

&lt;p&gt;The problem is creating hundreds of similar objects.&lt;/p&gt;

&lt;p&gt;We don't want to keep rewriting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

  &lt;span class="nf"&gt;updateEmail&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="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;over and over again.&lt;/p&gt;

&lt;p&gt;We need a way to create objects consistently.&lt;/p&gt;

&lt;p&gt;This is where factory functions come in.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Factory Functions&lt;/strong&gt;
&lt;/h2&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%2Fbi4yqlpei0quybmn1l0s.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%2Fbi4yqlpei0quybmn1l0s.png" alt="Factory-Functions" width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A factory function is simply a function whose job is to create and return objects.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createUser&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="nx"&gt;email&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="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="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="nf"&gt;updateEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newEmail&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;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newEmail&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now creating users becomes easy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ashay&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;ashay@gmail.com&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;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&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;rahul@gmail.com&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;Instead of manually constructing objects every time, we have a single place responsible for creating them.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Developers Loved Factory Functions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At this point many developers thought:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is great. Why do I need anything more?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And honestly, for many situations, you don't.&lt;/p&gt;

&lt;p&gt;Factory functions provide several benefits naturally.&lt;/p&gt;

&lt;p&gt;Object creation becomes consistent.&lt;/p&gt;

&lt;p&gt;Every user now has the same structure.&lt;/p&gt;

&lt;p&gt;No accidental missing fields.&lt;/p&gt;

&lt;p&gt;No forgotten methods.&lt;/p&gt;

&lt;p&gt;The factory handles everything.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;We Can Hide Internal Details&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine building a counter.&lt;/p&gt;

&lt;p&gt;We don't want other parts of the app directly changing the count.&lt;/p&gt;

&lt;p&gt;Without a factory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anyone can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe that's not what we want.&lt;/p&gt;

&lt;p&gt;With a factory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createCounter&lt;/span&gt;&lt;span class="p"&gt;()&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;count&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;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&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;getValue&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;count&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the internal state is protected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;doesn't affect the actual value.&lt;/p&gt;

&lt;p&gt;This was one of the biggest strengths of factory functions.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;So Why Were Classes Introduced?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At this point factory functions seem pretty good.&lt;/p&gt;

&lt;p&gt;They reduce repetition.&lt;/p&gt;

&lt;p&gt;They create objects.&lt;/p&gt;

&lt;p&gt;They can even hide internal state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So why introduce classes?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because developers encountered a different problem.&lt;/p&gt;

&lt;p&gt;Not object creation.&lt;/p&gt;

&lt;p&gt;Object organization.&lt;/p&gt;

&lt;p&gt;Imagine a large e-commerce system.&lt;/p&gt;

&lt;p&gt;You have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Products&lt;/li&gt;
&lt;li&gt;Orders&lt;/li&gt;
&lt;li&gt;Customers&lt;/li&gt;
&lt;li&gt;Payments&lt;/li&gt;
&lt;li&gt;Discounts&lt;/li&gt;
&lt;li&gt;Shipments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suddenly, there are dozens of related entities interacting with each other.&lt;/p&gt;

&lt;p&gt;Developers wanted a standardized way to describe:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What a Product is&lt;/li&gt;
&lt;li&gt;What an Order is&lt;/li&gt;
&lt;li&gt;What behaviors they have&lt;/li&gt;
&lt;li&gt;How they relate to one another&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Classes provided a structured blueprint for doing this.&lt;/p&gt;

&lt;p&gt;The Important Realization&lt;/p&gt;

&lt;p&gt;The interesting thing is that classes and factory functions are not enemies.&lt;/p&gt;

&lt;p&gt;They're trying to solve similar problems from different angles.&lt;/p&gt;

&lt;p&gt;Factory Functions answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How can I create objects consistently?&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;blockquote&gt;
&lt;p&gt;How can I model and organize complex object-oriented systems?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Both create objects.&lt;br&gt;
Both can be useful.&lt;br&gt;
Both have trade-offs.&lt;/p&gt;

&lt;p&gt;Understanding those trade-offs is far more important than blindly choosing one over the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What Should You Use?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;For small and medium-sized object creation needs, factory functions are often wonderfully simple.&lt;/p&gt;

&lt;p&gt;For larger systems with complex relationships, many teams prefer classes because they provide a more familiar structure.&lt;/p&gt;

&lt;p&gt;The right choice depends on the problem you're solving.&lt;/p&gt;

&lt;p&gt;And that's a recurring theme you'll see throughout this series:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Good software design is rarely about choosing the "best" solution.&lt;/p&gt;

&lt;p&gt;It's about understanding the problem and choosing the most appropriate trade-off.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;⏭️ What's Next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we've explored different ways of creating objects, we can return to Object-Oriented Programming itself.&lt;/p&gt;

&lt;p&gt;In the next article, we'll start exploring the first major OOP principle: Encapsulation.&lt;/p&gt;

&lt;p&gt;We'll look at the problems that arise when an object's internal state can be modified freely and how Encapsulation attempts to solve them.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
      <category>factoryfunction</category>
      <category>classes</category>
    </item>
    <item>
      <title>Why Object-Oriented Programming Was Introduced - Objects and Classes</title>
      <dc:creator>Ashay Tiwari</dc:creator>
      <pubDate>Tue, 09 Jun 2026 08:27:38 +0000</pubDate>
      <link>https://dev.to/ashay_tiwari_3658168ad5db/understanding-the-why-behind-software-design-oop-beyond-definitions-objects-and-classes-42b8</link>
      <guid>https://dev.to/ashay_tiwari_3658168ad5db/understanding-the-why-behind-software-design-oop-beyond-definitions-objects-and-classes-42b8</guid>
      <description>&lt;p&gt;In the previous article, we examined the importance of software design, and how all software engineering principles have been defined to address issues rather than creating more complexity.&lt;/p&gt;

&lt;p&gt;In this article, we will begin with one of the most significant programming paradigms known as Object Oriented Programming (OOP).&lt;/p&gt;

&lt;p&gt;But before moving into definitions, let us examine the issue first.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🤯 The Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine you are building a user system.&lt;/p&gt;

&lt;p&gt;Without OOP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ashay&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;user1Email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ashay@gmail.com&lt;/span&gt;&lt;span class="dl"&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;loginUser1&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;logoutUser1&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 imagine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;10 users&lt;/li&gt;
&lt;li&gt;100 users&lt;/li&gt;
&lt;li&gt;admins&lt;/li&gt;
&lt;li&gt;customers&lt;/li&gt;
&lt;li&gt;sellers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;duplicated&lt;/li&gt;
&lt;li&gt;disconnected&lt;/li&gt;
&lt;li&gt;hard to manage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;data scattered everywhere&lt;/li&gt;
&lt;li&gt;behavior scattered everywhere&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the core problem OOP tries to solve.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🧠 The Core Idea of OOP&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;OOP says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"A real-world entity should keep its own data and behavior together."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;A User should contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;its own properties (name, email)&lt;/li&gt;
&lt;li&gt;its own capabilities (login, logout)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That combination becomes an &lt;strong&gt;object&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;✨ What is an Object REALLY?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;An object is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A self-contained unit of state + behavior.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;State = data&lt;br&gt;
Behavior = actions/functions&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ashay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ashay@gmail.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;login&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;`&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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; logged in`&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;Here:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;name/email&lt;/td&gt;
&lt;td&gt;state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;login()&lt;/td&gt;
&lt;td&gt;behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;combined together&lt;/td&gt;
&lt;td&gt;object&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is the true meaning of an object.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Then Why Do We Need Classes?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now imagine creating 10,000 users.&lt;/p&gt;

&lt;p&gt;You DON'T want:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&lt;/span&gt; &lt;span class="o"&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;const&lt;/span&gt; &lt;span class="nx"&gt;user2&lt;/span&gt; &lt;span class="o"&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;const&lt;/span&gt; &lt;span class="nx"&gt;user3&lt;/span&gt; &lt;span class="o"&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;You need a reusable structure.&lt;br&gt;
That reusable structure is a &lt;strong&gt;class&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is a Class REALLY?
&lt;/h3&gt;

&lt;p&gt;A class is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A factory/template that defines what an object should contain.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&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;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&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;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;login&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;`&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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; logged in`&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 you can create objects easily:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&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;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ashay&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;a@gmail.com&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;user2&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;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&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;r@gmail.com&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;h3&gt;
  
  
  Deep Understanding of &lt;code&gt;new&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is VERY important.&lt;/p&gt;

&lt;p&gt;When you do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user1&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;User&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;new&lt;/code&gt; does several things internally:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates empty object &lt;code&gt;{}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Connects object to class prototype&lt;/li&gt;
&lt;li&gt;Binds &lt;code&gt;this&lt;/code&gt; to new object&lt;/li&gt;
&lt;li&gt;Runs the constructor&lt;/li&gt;
&lt;li&gt;Returns object&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;💡 Important Mental Model&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A class is NOT the actual thing.&lt;/p&gt;

&lt;p&gt;It is only:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;definition&lt;/li&gt;
&lt;li&gt;structure&lt;/li&gt;
&lt;li&gt;contract&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The object is the real runtime entity.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Real World&lt;/th&gt;
&lt;th&gt;OOP&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Building map&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Actual house&lt;/td&gt;
&lt;td&gt;Object&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Human DNA structure&lt;/td&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Actual person&lt;/td&gt;
&lt;td&gt;Object&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Common Beginner Mistake
&lt;/h2&gt;

&lt;p&gt;People think:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means “doing OOP”.&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Real OOP begins when you think:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What responsibility belongs here?&lt;/li&gt;
&lt;li&gt;What data should this object own?&lt;/li&gt;
&lt;li&gt;What should be hidden?&lt;/li&gt;
&lt;li&gt;How should objects communicate?&lt;/li&gt;
&lt;li&gt;Who controls what?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;li&gt;Abstraction&lt;/li&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;li&gt;Dependency Injection&lt;/li&gt;
&lt;li&gt;SOLID principles&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;which we’ll cover one by one.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;⏭️ What's Next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we understand what OOP is and how classes and objects help organize state and behavior, an interesting question naturally arises especially for JavaScript developers.&lt;/p&gt;

&lt;p&gt;If JavaScript already allows us to create objects using plain object literals and factory functions, why do we need classes at all?&lt;/p&gt;

&lt;p&gt;Are classes simply syntactic sugar, or do they solve a different set of problems?&lt;/p&gt;

&lt;p&gt;Before we dive into the core principles of OOP such as Encapsulation, Abstraction, Inheritance, and Polymorphism, we'll take a small detour to explore one of the most common debates in the JavaScript ecosystem:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Factory Functions vs Classes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We'll compare both approaches, understand their trade-offs, and discuss when each one makes sense in real-world applications.&lt;/p&gt;

&lt;p&gt;Because before learning how to design good objects, it's worth understanding the different ways we can create them.&lt;/p&gt;

</description>
      <category>oop</category>
      <category>javascript</category>
      <category>objects</category>
      <category>classes</category>
    </item>
    <item>
      <title>Understanding The "Why" Behind Software Design: Introduction</title>
      <dc:creator>Ashay Tiwari</dc:creator>
      <pubDate>Mon, 08 Jun 2026 07:04:46 +0000</pubDate>
      <link>https://dev.to/ashay_tiwari_3658168ad5db/understanding-the-why-behind-software-design-introduction-41j3</link>
      <guid>https://dev.to/ashay_tiwari_3658168ad5db/understanding-the-why-behind-software-design-introduction-41j3</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;✨ Why Software Design Matters&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you've been developing software for a while, you've probably come across terms like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object-Oriented Programming (OOP)&lt;/li&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;li&gt;Abstraction&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;Polymorphism&lt;/li&gt;
&lt;li&gt;SOLID Principles&lt;/li&gt;
&lt;li&gt;Dependency Injection&lt;/li&gt;
&lt;li&gt;Design Patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are countless articles, videos, and courses explaining these concepts.&lt;/p&gt;

&lt;p&gt;Still, there are plenty of developers, including me, who learn those concepts and forget them very quickly.&lt;/p&gt;

&lt;p&gt;We memorize definitions.&lt;/p&gt;

&lt;p&gt;We learn syntax.&lt;/p&gt;

&lt;p&gt;We understand enough to answer interview questions.&lt;/p&gt;

&lt;p&gt;But we rarely stop and ask:&lt;/p&gt;

&lt;p&gt;Why were these concepts introduced in the first place?&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%2Fqe21mh4eyg8bhwzuv53u.jpeg" 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%2Fqe21mh4eyg8bhwzuv53u.jpeg" alt="Asking Why??" width="552" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That realization is the reason I'm starting this series.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🧐 The Problem With How We Learn Software Design&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When I first learned OOP, I learned things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encapsulation is data hiding.&lt;/li&gt;
&lt;li&gt;Inheritance allows code reuse. &lt;/li&gt;
&lt;li&gt;Polymorphism means many forms.&lt;/li&gt;
&lt;li&gt;Abstraction hides implementation details.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The definitions were easy enough to remember.&lt;/p&gt;

&lt;p&gt;The difficult part was understanding when these concepts actually mattered.&lt;/p&gt;

&lt;p&gt;If someone asked:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why is inheritance considered risky in some situations?&lt;/li&gt;
&lt;li&gt;Why do people say composition is often better?&lt;/li&gt;
&lt;li&gt;Why was the SOLID principle created?&lt;/li&gt;
&lt;li&gt;Why do modern frameworks rely heavily on Dependency Injection?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I could explain the concepts individually, but I couldn't always explain the problems they were trying to solve.&lt;/p&gt;

&lt;p&gt;Over time, I realized that software design concepts are much easier to understand when viewed from a different perspective.&lt;/p&gt;

&lt;p&gt;Instead of asking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What is this concept?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What problem were developers facing that made this concept necessary?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once you understand the problem, the solution becomes much more intuitive.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🛠️ Why Software Design Matters&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When we start learning programming, most applications are small.&lt;/p&gt;

&lt;p&gt;A few functions.&lt;/p&gt;

&lt;p&gt;A few variables.&lt;/p&gt;

&lt;p&gt;A few files.&lt;/p&gt;

&lt;p&gt;Everything feels manageable.&lt;/p&gt;

&lt;p&gt;As projects grow, something interesting happens.&lt;/p&gt;

&lt;p&gt;The challenge is no longer writing code.&lt;/p&gt;

&lt;p&gt;The challenge becomes managing code.&lt;/p&gt;

&lt;p&gt;Features are added.&lt;/p&gt;

&lt;p&gt;Requirements change.&lt;/p&gt;

&lt;p&gt;Teams grow.&lt;/p&gt;

&lt;p&gt;Business logic becomes more complex.&lt;/p&gt;

&lt;p&gt;Code that once felt simple starts becoming difficult to understand and modify.&lt;/p&gt;

&lt;p&gt;At some point, developers begin encountering questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why is this logic duplicated in multiple places?&lt;/li&gt;
&lt;li&gt;Why does changing one feature break another?&lt;/li&gt;
&lt;li&gt;Why is this class responsible for so many things?&lt;/li&gt;
&lt;li&gt;Why is testing this code so difficult?&lt;/li&gt;
&lt;li&gt;Why does every new feature require modifying existing code?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The software still works.&lt;/p&gt;

&lt;p&gt;But it becomes harder to maintain.&lt;/p&gt;

&lt;p&gt;Harder to extend.&lt;/p&gt;

&lt;p&gt;Harder to reason about.&lt;/p&gt;

&lt;p&gt;This is where software design becomes important.&lt;/p&gt;

&lt;p&gt;Software design is not about making code look clever.&lt;/p&gt;

&lt;p&gt;It's about organizing code in a way that allows systems to evolve without becoming unmanageable.&lt;/p&gt;

&lt;p&gt;Most of the concepts we'll discuss throughout this series were created to solve exactly these kinds of problems.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;❓ What This Series Is About&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This series is not going to focus on memorizing definitions.&lt;/p&gt;

&lt;p&gt;Instead, we'll focus on understanding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What problem exists?&lt;/li&gt;
&lt;li&gt;Why the straightforward solution eventually breaks down?&lt;/li&gt;
&lt;li&gt;What concept was introduced to solve it?&lt;/li&gt;
&lt;li&gt;What trade-offs come with that solution?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll use JavaScript and TypeScript examples because that's what I work with most often, but the concepts themselves apply regardless of programming language.&lt;/p&gt;

&lt;p&gt;Whether you're writing Java, C#, Python, Go, JavaScript, or something else entirely, the underlying design principles remain largely the same.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;↔️ A Small Shift in Perspective&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Throughout this series, I'd encourage you to think about concepts differently.&lt;/p&gt;

&lt;p&gt;Instead of learning:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Encapsulation is data hiding."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"What problems occur when every part of the system can freely modify an object's state?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of learning:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Dependency Injection helps decouple code."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Why does directly creating dependencies become painful as applications grow?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answers to those questions are often more valuable than the definitions themselves.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;⏭️ What's Next?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the next article, we'll begin with Object-Oriented Programming itself.&lt;/p&gt;

&lt;p&gt;Not by starting with classes and syntax.&lt;/p&gt;

&lt;p&gt;But by understanding the problems developers were facing before OOP became one of the most influential programming paradigms in software development.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>architecture</category>
      <category>oop</category>
    </item>
    <item>
      <title>Tunneling Made Simple: Exposing Local React and Node Apps with ngrok and LocalTunnel</title>
      <dc:creator>Ashay Tiwari</dc:creator>
      <pubDate>Thu, 01 May 2025 20:09:17 +0000</pubDate>
      <link>https://dev.to/ashay_tiwari_3658168ad5db/tunneling-made-simple-exposing-local-react-and-node-apps-with-ngrok-and-localtunnel-5g1g</link>
      <guid>https://dev.to/ashay_tiwari_3658168ad5db/tunneling-made-simple-exposing-local-react-and-node-apps-with-ngrok-and-localtunnel-5g1g</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;✨ Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Ever wanted to test your local web app on your phone? Share a work-in-progress with a client? Or receive webhooks from services like Stripe or GitHub?&lt;/p&gt;

&lt;p&gt;This blog post is your complete guide to securely exposing local development servers to the internet — whether it's a React.js frontend or a Node.js REST API backend.&lt;/p&gt;

&lt;p&gt;We’ll explore two popular tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔗 LocalTunnel – simple and open-source&lt;/li&gt;
&lt;li&gt;🚀 ngrok – powerful with an interactive dashboard and HTTPS support&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🔍 What You’ll Learn&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In this guide, you’ll:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understand the concept and use cases of local tunneling&lt;/li&gt;
&lt;li&gt;Set up a basic Node.js REST API and React.js app&lt;/li&gt;
&lt;li&gt;Expose both apps securely using LocalTunnel and ngrok&lt;/li&gt;
&lt;li&gt;Learn the pros/cons of each tool&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🛠️ Project Setup (React + Node)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Node.js REST API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Steps to create a basic Express API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir backend-app
cd backend-app
npm init -y
npm install express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create &lt;code&gt;server.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const PORT = 8000;

app.get('/api/greet', (req, res) =&amp;gt; {
  res.json({ message: 'Hello from Backend-App' });
});

app.listen(PORT, () =&amp;gt; {
  console.log(`API running at http://localhost:${PORT}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2️⃣ &lt;strong&gt;React.js Frontend App&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using Create React App:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app frontend-app
cd frontend
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, Using Vite (faster):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest frontend-app -- --template react
cd frontend
npm install
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update &lt;code&gt;App.js&lt;/code&gt; as:&lt;br&gt;
&lt;/p&gt;

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

function App() {

  return &amp;lt;h1&amp;gt;Hello from Frontend-App&amp;lt;/h1&amp;gt;;
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;🌐 What is a Tunneling System?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When you're developing locally (e.g., &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; or &lt;a href="http://127.0.0.1:8000" rel="noopener noreferrer"&gt;http://127.0.0.1:8000&lt;/a&gt;), your apps are only accessible on your machine. If you want someone else — a client, teammate, or even a third-party API like Stripe — to access your local project, you need to expose it to the internet.&lt;/p&gt;

&lt;p&gt;That’s where tunneling tools come in.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A tunneling system creates a secure "bridge" between your local server and a public URL that anyone can access — even from outside your network.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine it like building a secure pipe from your computer to the outside world. You don't have to deploy to a remote server or set up complex networking — just run a single command and you're live!&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%2Ffbgksd1yedo3g9uisyv0.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%2Ffbgksd1yedo3g9uisyv0.png" alt="Tunneling System Workflow" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🚀 Setting Up ngrok to Tunnel Your Local React and Node.js Apps&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s now walk through how to use ngrok to expose both your local backend and frontend to the internet.&lt;/p&gt;

&lt;p&gt;🔧 &lt;strong&gt;Step 1: Install ngrok&lt;/strong&gt;&lt;br&gt;
You can install ngrok globally via npm or download it from the official website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Option 1 – Install via npm:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g ngrok
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Option 2 – Download from website:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to &lt;a href="https://ngrok.com/download" rel="noopener noreferrer"&gt;https://ngrok.com/download&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Download and unzip the binary&lt;/li&gt;
&lt;li&gt;Move it to a directory in your system’s PATH&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🔐 Step 2: Sign up and authenticate ngrok (Optional but recommended)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're using a free ngrok account, you should sign up and connect your account to unlock features like custom subdomains and higher session limits.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sign up at &lt;a href="https://dashboard.ngrok.com" rel="noopener noreferrer"&gt;https://dashboard.ngrok.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Copy your authtoken from the dashboard&lt;/li&gt;
&lt;li&gt;Run the command:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ngrok config add-authtoken &amp;lt;your-token-here&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure your node and react servers are running locally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🌍 Step 3: Expose your servers with ngrok&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ Note: ngrok Free Plan supports only one active tunnel at a time. If you want to expose both the React app and Node API simultaneously, you’ll need a Pro plan or use a different tunneling tool for one of them (e.g., LocalTunnel).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Tunnel the Node.js API (port 8000):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ngrok http 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll get an output like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Forwarding     https://1e3f-106-219-85-57.ngrok-free.app -&amp;gt; http://localhost:8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now access your API from this public URL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tunnel the React frontend (port 3000):&lt;/strong&gt;&lt;br&gt;
If you want to expose the React app instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ngrok http 3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And again, ngrok will provide a public HTTPS URL like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Forwarding     https://6g4f-105-221-92-12.ngrok-free.app -&amp;gt; http://localhost:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;🚀 Setting Up LocalTunnel for your React and Node.js Application&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;After covering &lt;code&gt;ngrok&lt;/code&gt;, let’s explore another tunneling tool: LocalTunnel — a free and open-source option that lets you share your local development environment via a public URL.&lt;/p&gt;

&lt;p&gt;Unlike ngrok, LocalTunnel does not require an account and allows multiple tunnels simultaneously (each on different ports and subdomains).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🔧 Step 1: Install LocalTunnel&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install it globally via npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g localtunnel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure your node and react servers are running locally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🌍 Step 2: Expose your servers with LocalTunnel&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can create tunnels for both apps using unique ports and optional subdomains.&lt;br&gt;
&lt;strong&gt;Tunnel the Node.js API:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lt --port 8000 --subdomain my-node-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a URL like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://my-node-api.loca.lt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Tunnel the React frontend:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lt --port 3000 --subdomain my-react-app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a URL like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://my-react-app.loca.lt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;⚠️ Subdomains are first-come-first-served, and not guaranteed. If someone else is already using the same subdomain, you’ll get an error. If you omit --subdomain, a random one is generated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;🔁 Realtime Code Changes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;LocalTunnel reflects real-time changes, just like ngrok, since it directly proxies your local server. You can continue editing your React or Node app, and the changes are reflected via the public tunnel.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🎉 That’s It!&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;You’ve now exposed your local apps to the world using ngrok or LocalTunnel. You can share those URLs with clients, test on your phone, or integrate with third-party services that need a public endpoint.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🆚 Comparison: ngrok vs. LocalTunnel&lt;/strong&gt;
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;ngrok (Free Tier)&lt;/th&gt;
&lt;th&gt;LocalTunnel&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;🧰 Installation&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;npm install -g ngrok&lt;/code&gt; or native binary&lt;/td&gt;
&lt;td&gt;&lt;code&gt;npm install -g localtunnel&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🔐 Account Required&lt;/td&gt;
&lt;td&gt;✅ Yes (for custom subdomains, dashboard access)&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🔄 Supports Multiple Tunnels&lt;/td&gt;
&lt;td&gt;❌ No (only &lt;strong&gt;1 active tunnel&lt;/strong&gt; at a time)&lt;/td&gt;
&lt;td&gt;✅ Yes (multiple tunnels allowed)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🌐 Custom Subdomain&lt;/td&gt;
&lt;td&gt;✅ Yes (only with account)&lt;/td&gt;
&lt;td&gt;✅ Yes (but not always guaranteed)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🔐 HTTPS Support&lt;/td&gt;
&lt;td&gt;✅ Yes (by default)&lt;/td&gt;
&lt;td&gt;✅ Yes (by default)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🌍 URL Format&lt;/td&gt;
&lt;td&gt;&lt;code&gt;https://&amp;lt;random&amp;gt;.ngrok-free.app&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;https://&amp;lt;random or subdomain&amp;gt;.loca.lt&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🧑‍💻 Dashboard &amp;amp; Inspect Traffic&lt;/td&gt;
&lt;td&gt;✅ Yes (Web dashboard &amp;amp; HTTP request inspector)&lt;/td&gt;
&lt;td&gt;❌ No built-in dashboard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;💻 Real-time Code Updates&lt;/td&gt;
&lt;td&gt;✅ Yes (reflects local changes)&lt;/td&gt;
&lt;td&gt;✅ Yes (reflects local changes)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🕐 Session Duration&lt;/td&gt;
&lt;td&gt;~8 hours (free account), reconnect needed&lt;/td&gt;
&lt;td&gt;Until you manually close the tunnel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;📦 Open Source&lt;/td&gt;
&lt;td&gt;❌ Proprietary&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;💰 Pricing&lt;/td&gt;
&lt;td&gt;Free &amp;amp; paid tiers&lt;/td&gt;
&lt;td&gt;Fully free&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;🌐 Use Case Fit&lt;/td&gt;
&lt;td&gt;Best for &lt;strong&gt;professional demos&lt;/strong&gt;, webhooks, integrations&lt;/td&gt;
&lt;td&gt;Best for &lt;strong&gt;quick testing&lt;/strong&gt;, sharing with teammates&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;🎯 Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In this post, we explored how tunneling tools like ngrok and LocalTunnel help developers expose their local applications to the internet. ngrok offers advanced features like custom subdomains and request inspection, making it ideal for professional use, while LocalTunnel is a simpler, free alternative perfect for quick sharing and testing. Both tools provide a secure and real-time connection to your local environment, so you can choose the one that best fits your needs.&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
