<?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: Akbar Julian Khatibi</title>
    <description>The latest articles on DEV Community by Akbar Julian Khatibi (@ajkhatibi).</description>
    <link>https://dev.to/ajkhatibi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1081076%2Fedd39b34-5903-441e-87fb-a98c41667f00.jpeg</url>
      <title>DEV Community: Akbar Julian Khatibi</title>
      <link>https://dev.to/ajkhatibi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajkhatibi"/>
    <language>en</language>
    <item>
      <title>Understanding Liskov Substitution Principle (LSP) in TypeScript and React</title>
      <dc:creator>Akbar Julian Khatibi</dc:creator>
      <pubDate>Thu, 27 Jul 2023 21:03:47 +0000</pubDate>
      <link>https://dev.to/ajkhatibi/understanding-liskov-substitution-principle-lsp-in-typescript-and-react-25c1</link>
      <guid>https://dev.to/ajkhatibi/understanding-liskov-substitution-principle-lsp-in-typescript-and-react-25c1</guid>
      <description>&lt;p&gt;Welcome to another article in the series where we demystify the SOLID principles. Today we'll focus on the Liskov Substitution Principle (LSP), an essential part of writing maintainable, robust code in object-oriented programming.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Liskov Substitution Principle?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Liskov Substitution Principle, named after Barbara Liskov, states that objects in a program should be replaceable with instances of their subtypes without altering the correctness of the program.&lt;/p&gt;

&lt;p&gt;In simpler terms, it means that a class should be able to reference a base or derived class without affecting its behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is LSP important?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When properly implemented, the Liskov Substitution Principle promotes code reusability and enhances the robustness of the software system. LSP reduces the likelihood of bugs and makes your code easier to understand and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Liskov Substitution Principle in TypeScript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start with a simple example in TypeScript to illustrate the Liskov Substitution Principle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;fly&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="nx"&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;The bird is flying&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Duck&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Bird&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;makeBirdFly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bird&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Bird&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;bird&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;makeBirdFly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Bird&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: The bird is flying&lt;/span&gt;
&lt;span class="nx"&gt;makeBirdFly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Duck&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: The bird is flying&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a &lt;code&gt;Bird&lt;/code&gt; class and a &lt;code&gt;Duck&lt;/code&gt; class that extends &lt;code&gt;Bird&lt;/code&gt;. The &lt;code&gt;makeBirdFly&lt;/code&gt; function takes a &lt;code&gt;Bird&lt;/code&gt; instance as a parameter and invokes the fly method. As per the LSP, you can replace the &lt;code&gt;Bird&lt;/code&gt; instance with a &lt;code&gt;Duck&lt;/code&gt; instance without affecting the correctness of the &lt;code&gt;makeBirdFly&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Applying LSP in React&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let's consider a scenario where the Liskov Substitution Principle is violated: We can create a &lt;code&gt;Clickable&lt;/code&gt; type that includes an &lt;code&gt;onClick&lt;/code&gt; prop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ClickableProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ClickableProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Label doesn't have an onClick prop, because it's not meant to be clickable&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Label&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&amp;gt;&lt;/span&gt;&lt;span class="err"&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, we might have some logic that requires handling a click event on a component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ClickHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ClickableProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&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;handleClick&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClickHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;components&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// We'll render the component into a "virtual DOM" to trigger the onClick&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would work fine with &lt;code&gt;Button&lt;/code&gt;, but if we try to use &lt;code&gt;Label&lt;/code&gt; in this way, TypeScript won't compile because &lt;code&gt;Label&lt;/code&gt; doesn't have an &lt;code&gt;onClick&lt;/code&gt; prop. This demonstrates the Liskov Substitution Principle: Label cannot be used in place of &lt;code&gt;Button&lt;/code&gt; because it doesn't fulfil the contract of having an &lt;code&gt;onClick&lt;/code&gt; handler.&lt;/p&gt;

&lt;p&gt;We've prevented the issue at compile-time, thanks to TypeScript's type system. This prevents us from running into a runtime error where our function tries to call &lt;code&gt;onClick&lt;/code&gt; on a &lt;code&gt;Label&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note: We're using a &lt;code&gt;render&lt;/code&gt; function to "virtually" trigger the &lt;code&gt;onClick&lt;/code&gt; in this contrived example. In a real-world scenario, you would more likely pass these components around, include them in JSX, and let the user interact with them to trigger any click handlers.&lt;/p&gt;

&lt;p&gt;Let's consider another example of a base &lt;code&gt;Button&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;onClick&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="nx"&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;Button clicked&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="nx"&gt;render&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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;onClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;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, if we were to create a &lt;code&gt;SubmitButton&lt;/code&gt; that extends &lt;code&gt;Button&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;SubmitButton&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;onClick&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="nx"&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;Submitting...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&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;According to LSP, any instance where &lt;code&gt;Button&lt;/code&gt; is used, &lt;code&gt;SubmitButton&lt;/code&gt; should also be able to be used without issues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SubmitButton&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;&lt;strong&gt;Applying LSP in React with Functional Components&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's consider the base &lt;code&gt;Button&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&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;Button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;Button&lt;/code&gt; component is a simple functional component that receives an &lt;code&gt;onClick&lt;/code&gt; callback and &lt;code&gt;children&lt;/code&gt; as props, and renders a button.&lt;/p&gt;

&lt;p&gt;Now, we'll create a &lt;code&gt;SubmitButton&lt;/code&gt; that extends &lt;code&gt;Button&lt;/code&gt; in behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Button&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;SubmitButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="nx"&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;Submitting...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;onClick&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;SubmitButton&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this &lt;code&gt;SubmitButton&lt;/code&gt;, we have an additional &lt;code&gt;console.log&lt;/code&gt; in the &lt;code&gt;handleClick&lt;/code&gt; function which will run before any other &lt;code&gt;onClick&lt;/code&gt; behavior passed to the &lt;code&gt;SubmitButton&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;According to LSP, any place where &lt;code&gt;Button&lt;/code&gt; is used, &lt;code&gt;SubmitButton&lt;/code&gt; should also be able to be used without causing any issues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;SubmitButton&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./SubmitButton&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;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="nx"&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;Button clicked&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;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SubmitButton&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;SubmitButton&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/SubmitButton&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, both &lt;code&gt;Button&lt;/code&gt; and &lt;code&gt;SubmitButton&lt;/code&gt; are used interchangeably, and &lt;code&gt;SubmitButton&lt;/code&gt; performs some additional behavior before running the passed &lt;code&gt;onClick&lt;/code&gt; behavior, following the Liskov Substitution Principle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flaws&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Liskov Substitution Principle (LSP) is a useful guideline in object-oriented programming that helps ensure that a subclass can replace its superclass without causing incorrect behavior or errors. However, like all principles, it's not without its limitations or criticisms. Here are some of the potential issues:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Overly Restrictive:&lt;/strong&gt; LSP can be seen as too restrictive, especially in dynamic languages or those with more flexible type systems. Sometimes, certain subclasses inherently have different behaviors or properties that make them incompatible with the superclass, even if they share a majority of their characteristics. Strict adherence to LSP can limit the design of such classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Misinterpretation and Over-Engineering:&lt;/strong&gt; LSP is often misunderstood, and a strict interpretation can lead to over-engineering. Developers might be tempted to write more complex code just to fulfill the LSP when simpler, more straightforward code could solve the problem more efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Doesn't Fully Solve Inheritance Problems:&lt;/strong&gt; Even with LSP, problems with inheritance can still occur. For instance, a subclass might need only part of the superclass's functionality, or it might require a different implementation that doesn't fit neatly into the superclass's method signatures.&lt;/p&gt;

&lt;p&gt;Consider an application that has a &lt;code&gt;Button&lt;/code&gt; component with a click handler. Now, you want to add a &lt;code&gt;DisabledButton&lt;/code&gt; that looks the same as &lt;code&gt;Button&lt;/code&gt;, but doesn't respond to clicks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ButtonProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ButtonProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&amp;gt;&lt;/span&gt;&lt;span class="err"&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;DisabledButton&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ButtonProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;disabled&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Can&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;t click me&amp;lt;/button&amp;gt;;
};

// Using the components
&amp;lt;Button onClick={() =&amp;gt; console.log(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt; &lt;span class="nx"&gt;clicked&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;)} /&amp;gt;
&amp;lt;DisabledButton onClick={() =&amp;gt; console.log(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;Disabled&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;clicked&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;)} /&amp;gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where it gets tricky with the Liskov Substitution Principle. According to LSP, a subclass should be substitutable for its superclass without causing any issues. In this case, even though &lt;code&gt;DisabledButton&lt;/code&gt; and &lt;code&gt;Button&lt;/code&gt; share the same interface (&lt;code&gt;ButtonProps&lt;/code&gt;), they behave differently. If you were to replace &lt;code&gt;Button&lt;/code&gt; with &lt;code&gt;DisabledButton&lt;/code&gt; in your code, the &lt;code&gt;onClick&lt;/code&gt; prop wouldn't function as expected, which violates the LSP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wrapping Up&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Liskov Substitution Principle is a powerful principle that helps us to write more robust and maintainable code. By adhering to this principle, we ensure that our codebase stays flexible and less prone to bugs.&lt;/p&gt;

&lt;p&gt;Remember, SOLID principles are guidelines, not hard rules. There will be times when following these principles might not be practical. However, understanding these principles can guide you towards writing better code.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding the Open-Closed Principle in TypeScript and React</title>
      <dc:creator>Akbar Julian Khatibi</dc:creator>
      <pubDate>Mon, 17 Jul 2023 19:25:15 +0000</pubDate>
      <link>https://dev.to/ajkhatibi/understanding-the-open-closed-principle-in-typescript-and-react-1i2n</link>
      <guid>https://dev.to/ajkhatibi/understanding-the-open-closed-principle-in-typescript-and-react-1i2n</guid>
      <description>&lt;p&gt;The Open-Closed Principle (OCP) is one of the five principles of SOLID that guides developers in creating maintainable and scalable code. According to the principle, software entities such as classes, modules, and functions should be open for extension but closed for modification. This principle minimizes the risk of introducing bugs or breaking existing functionality when adding new features. Although SOLID was formulated for object-oriented languages, its principles, including OCP, can be applied to other paradigms such as TypeScript and React.&lt;/p&gt;

&lt;p&gt;In TypeScript, the Open-Closed Principle can be achieved using interfaces and class inheritance. An interface defines a set of rules that classes can implement, allowing them to be open for extension. The classes can be used interchangeably in the program, keeping the other parts of the program closed for modification.&lt;/p&gt;

&lt;p&gt;For instance, let's consider a geometry program that calculates the area of different shapes. In the example below, the getArea function needs modification to support new shapes, which violates the Open-Closed Principle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getArea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;kind&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="nx"&gt;length&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;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;square&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;return&lt;/span&gt; &lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="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;A better approach would be to create a Shape interface that each shape can implement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;area&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Square&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;constructor&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;length&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="nx"&gt;area&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;length&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&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;With this approach, each shape defines its area method, and the getArea function is not required. New shapes can be added without modifying existing code, adhering to the Open-Closed Principle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Note About Typescript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Even though TypeScript is primarily for type checking and doesn't directly impact the runtime behavior of the code, it can still catch potential bugs at compile time rather than at runtime.&lt;/p&gt;

&lt;p&gt;Moreover, interfaces can guide the design of your code by specifying contracts that classes must adhere to. In this case, the Shape interface is saying that any shape must have an area method that returns a number. This encourages a consistent design where different shape classes can be treated in a uniform way.&lt;/p&gt;

&lt;p&gt;So, while TypeScript interfaces don't have a direct impact on the resulting JavaScript code, they have a significant impact on the TypeScript code's correctness, maintainability, and design.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Examples In React&lt;/strong&gt;&lt;br&gt;
In React, the Open-Closed Principle can be applied by creating components that accept props and children. This way, the components can be extended by passing different props and children without modifying the component's code.&lt;/p&gt;

&lt;p&gt;For example, a List component can accept an array of items and a function to render each item (renderItem), making the component highly reusable and extendable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;List&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;renderItem&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;renderItem&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ul&lt;/span&gt;&lt;span class="err"&gt;&amp;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;With this component, we can create a list of strings, a list of numbers, a list of complex objects, etc., by passing a different renderItem function and items array. We can extend the List component's functionality without modifying its code, keeping with the Open-Closed Principle.&lt;/p&gt;

&lt;p&gt;Remember, adhering to the Open-Closed Principle can help you create robust and maintainable code in TypeScript and React. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>typescript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding React and React Native Through the Principle of Single Responsibility</title>
      <dc:creator>Akbar Julian Khatibi</dc:creator>
      <pubDate>Thu, 18 May 2023 20:24:38 +0000</pubDate>
      <link>https://dev.to/ajkhatibi/understanding-react-and-react-native-through-the-principle-of-single-responsibility-3pd5</link>
      <guid>https://dev.to/ajkhatibi/understanding-react-and-react-native-through-the-principle-of-single-responsibility-3pd5</guid>
      <description>&lt;p&gt;When building a complex web or mobile application, it's critical to consider the underlying principles that can guide the architecture and design of your app. One such fundamental principle, rooted deeply in computer science, is the Single Responsibility Principle (SRP). This principle is an integral part of SOLID principles of software design and applies directly to how we can effectively use React and React Native in our projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the Single Responsibility Principle?&lt;/strong&gt;&lt;br&gt;
The Single Responsibility Principle states that a class should have only one reason to change. This principle, introduced by Robert C. Martin, commonly known as Uncle Bob, emphasizes that a class or a module should focus on a single task.&lt;/p&gt;

&lt;p&gt;While SRP was originally coined for class-based object-oriented programming, the principle is applicable in a broader sense, especially in the world of JavaScript and, more specifically, React and React Native.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Applying SRP to React and React Native&lt;/strong&gt;&lt;br&gt;
React and React Native are JavaScript libraries used for building user interfaces. React is mainly used for web development, while React Native is used for mobile application development.&lt;/p&gt;

&lt;p&gt;When using these libraries, SRP can be effectively applied through the use of components. A component in React or React Native should ideally be responsible for rendering a specific part of the UI and handling the interactions within that part. By making each component responsible for a single task, we bring the benefits of SRP to our projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits of Using SRP in React and React Native&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Increased Maintainability&lt;br&gt;
When components are responsible for a single task, they become more straightforward and easier to maintain. If a feature needs to be updated or a bug needs to be fixed, developers can quickly locate the relevant component and make the necessary changes without affecting the rest of the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enhanced Testability&lt;br&gt;
Single responsibility components simplify unit testing. Each component can be tested in isolation, ensuring that it performs its primary function correctly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reusability&lt;br&gt;
SRP allows components to be generic and therefore more reusable. By ensuring that a component has a single responsibility, it can often be used across different parts of an application or even across different projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Simplified Reasoning&lt;br&gt;
Components with a single responsibility are easier to understand. Developers can look at a component and quickly understand what it does, which simplifies both development and code reviews.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;An Example&lt;/strong&gt;&lt;br&gt;
Consider a profile page on a social media app developed using React Native. Instead of creating one large component responsible for rendering the entire page, you could break down the page into smaller components, each with its own responsibility. You might have one component for the profile picture, another for the user's basic info, one for the list of posts, and so on. Each of these components would have a single responsibility, making them easier to develop, test, and maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Balancing SRP: Potential Drawbacks&lt;/strong&gt;&lt;br&gt;
While the Single Responsibility Principle brings numerous benefits, like any principle, it should not be applied blindly. There are some potential drawbacks that developers need to be aware of when trying to adhere strictly to SRP:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Over-Engineering&lt;/strong&gt;&lt;br&gt;
The most common pitfall when applying SRP is over-engineering. If taken to an extreme, every minor function of an application might end up in a separate component. This can lead to an unnecessary proliferation of components, making the codebase harder to navigate and understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Increased Complexity&lt;/strong&gt;&lt;br&gt;
While SRP can simplify individual components, it may also inadvertently increase the overall complexity of your application. Managing data flow between numerous smaller components can be more challenging than in a system with fewer, larger components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Performance Overhead&lt;/strong&gt;&lt;br&gt;
Creating too many components can also lead to performance overhead. Every React component comes with a certain amount of overhead for React's virtual DOM diffing algorithm, and creating too many components can slow down the rendering process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Striking a Balance&lt;/strong&gt;&lt;br&gt;
The key to effectively using the Single Responsibility Principle in React and React Native is balance. It's essential to consider the complexity of the component and the likelihood of change. Not every piece of logic needs to be its own component; sometimes, it's better to group related logic together for the sake of readability and performance.&lt;/p&gt;

&lt;p&gt;The Single Responsibility Principle is a tool, not a rule. Use it to guide your decisions and inform your architecture, but don't let it dictate your every move. Each project is unique and requires a thoughtful approach to strike the right balance between component simplicity, maintainability, performance, and overall complexity.&lt;/p&gt;

&lt;p&gt;Remember, the ultimate goal of any principle or pattern is to improve the quality of the code and the productivity of the developers. If strict adherence to a principle is counterproductive, it may be necessary to reconsider its application.&lt;/p&gt;

&lt;p&gt;By understanding the potential pitfalls and benefits of the Single Responsibility Principle, developers can make better design decisions, creating applications that are both robust and manageable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The Single Responsibility Principle, while simple, is incredibly powerful. It can be effectively applied to improve the design and architecture of applications built with React or React Native. By understanding and using this principle, we can create applications that are more maintainable, testable, reusable, and easier to reason about.&lt;/p&gt;

&lt;p&gt;So next time you're working on a React or React Native project, consider the Single Responsibility Principle. It just might make your work easier and your code better.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Building Large-Scale React Native Applications: A Modular Approach</title>
      <dc:creator>Akbar Julian Khatibi</dc:creator>
      <pubDate>Fri, 12 May 2023 02:44:41 +0000</pubDate>
      <link>https://dev.to/ajkhatibi/building-large-scale-react-native-applications-a-modular-approach-468k</link>
      <guid>https://dev.to/ajkhatibi/building-large-scale-react-native-applications-a-modular-approach-468k</guid>
      <description>&lt;p&gt;In the world of React Native application development, creating small applications is straightforward. But as the project grows, managing it can become quite a challenge. In this article, we'll discuss a modular approach for building large-scale React Native applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Need for Modularization
&lt;/h2&gt;

&lt;p&gt;As a React Native application grows, so does its complexity. Components become interconnected, state management gets complicated, and before you know it, your codebase turns into a tangled mess. This is where the concept of modularization comes in.&lt;/p&gt;

&lt;p&gt;Modularization involves breaking down your application into separate, independent modules. Each module is responsible for a specific feature or functionality of your application and operates independently of the others. This approach offers several benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Easier Code Management:&lt;/strong&gt; By breaking down your application into smaller parts, the codebase becomes easier to manage and understand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusability:&lt;/strong&gt; Modules can be reused across different parts of the application or even in different projects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallel Development:&lt;/strong&gt; Different teams can work on different modules simultaneously, which can speed up the development process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementing a Modular Structure
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Splitting Up Your Application&lt;/strong&gt;&lt;br&gt;
The first step in creating a modular React Native application is to decide how to split up your application. A good approach is to divide your application based on its features. For example, if you're building a social media app, you could have separate modules for user authentication, posting, commenting, and so on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Creating a Directory Structure&lt;/strong&gt;&lt;br&gt;
For each module, create a separate directory in your project. A typical module directory might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/my-module
  /components
  /services
  /state
  index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this structure:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;components&lt;/code&gt; directory holds all the React components related to that module.&lt;br&gt;
&lt;code&gt;services&lt;/code&gt; directory contains any services that the module might need, such as API calls.&lt;br&gt;
&lt;code&gt;state&lt;/code&gt; directory includes all state management code (like Redux or MobX) for that module.&lt;br&gt;
&lt;code&gt;index.js&lt;/code&gt; is the entry point for the module.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Implementing Navigation&lt;/strong&gt;&lt;br&gt;
In a modular structure, each module should define its own navigation. React Navigation, a popular navigation library for React Native, supports this perfectly. Each module can export its own navigator, which can then be imported into the main navigator.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Managing State&lt;/strong&gt;&lt;br&gt;
For state management, consider using Redux or MobX. Both of these libraries support a modular structure. Each module can define its own actions and reducers (in Redux) or observables and actions (in MobX), which can then be combined at the application level.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pitfalls to Avoid&lt;/strong&gt;&lt;br&gt;
While modularization can greatly improve your development process, there are a few common pitfalls to avoid:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Over-Modularization:&lt;/strong&gt; While it's important to separate concerns, creating too many modules can make your codebase hard to navigate. Aim for a balance between separation of concerns and simplicity.&lt;br&gt;
&lt;strong&gt;- Tightly Coupled Modules:&lt;/strong&gt; Modules should be as independent as possible. If you find that one module is heavily dependent on another, consider whether they should actually be a single module.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Building large-scale applications is a significant undertaking, but with a modular approach, the process can become much more manageable. By breaking your application down into smaller, independent parts, you can create a codebase that is easier to understand, more reusable, and more conducive to parallel development. It's a win-win for everyone involved!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>react</category>
      <category>reactnative</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
