<?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: Ashutosh Kumar</title>
    <description>The latest articles on DEV Community by Ashutosh Kumar (@raxraj).</description>
    <link>https://dev.to/raxraj</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%2F534454%2Fff74ea49-b65f-4c05-9405-6528a444903a.jpg</url>
      <title>DEV Community: Ashutosh Kumar</title>
      <link>https://dev.to/raxraj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raxraj"/>
    <language>en</language>
    <item>
      <title>The Importance of Choosing the Right Data Types for Database Optimization</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Fri, 18 Jul 2025 05:19:36 +0000</pubDate>
      <link>https://dev.to/raxraj/the-importance-of-choosing-the-right-data-types-for-database-optimization-ch6</link>
      <guid>https://dev.to/raxraj/the-importance-of-choosing-the-right-data-types-for-database-optimization-ch6</guid>
      <description>&lt;p&gt;When designing a database, it’s tempting to focus on the big-picture aspects: tables, relationships, and queries. However, one seemingly minor decision can have a massive impact on your application's performance and scalability, choosing the right data types for your columns.&lt;/p&gt;

&lt;p&gt;In this article, we'll explore why the selection of data types matters, how it affects your database, and some best practices for making optimal choices, peppered with code snippets and a dash of developer humour 🧑‍💻😂.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Data Types Matter
&lt;/h2&gt;

&lt;p&gt;Every column in a database table must be assigned a data type (such as &lt;code&gt;INT&lt;/code&gt;, &lt;code&gt;VARCHAR&lt;/code&gt;, &lt;code&gt;DATE&lt;/code&gt;, etc.). The choice you make directly influences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Smaller, well-chosen data types reduce memory and storage usage, leading to faster queries and better index efficiency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integrity:&lt;/strong&gt; Proper data types enforce valid data, reducing errors and ensuring accuracy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; Efficient data types help your database scale more gracefully as data volume grows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Choosing the wrong data type is like wearing winter boots to the beach. Technically, it works, but it's going to get uncomfortable fast!&lt;/em&gt; 🏖️🥾&lt;/p&gt;




&lt;h2&gt;
  
  
  How Data Type Choice Affects Optimization
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Storage Efficiency&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Choosing the smallest data type that can reliably store your data minimizes the amount of disk space used. For example, using &lt;code&gt;TINYINT&lt;/code&gt; instead of &lt;code&gt;INT&lt;/code&gt; for a column that only stores numbers from 0 to 100 saves three bytes per row.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Storing a flag as INT. That's 4 bytes for a yes/no question!&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="nb"&gt;INT&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;Better Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- TINYINT to the rescue: 1 byte is plenty!&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="nb"&gt;TINYINT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Some databases even support BOOLEAN, which is perfect for true/false.&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="nb"&gt;BOOLEAN&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Moral: Don’t bring a bazooka (INT) to a pillow fight (BOOLEAN)!&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Index Performance&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Indexes are vital for fast queries. Smaller data types mean smaller indexes, which fit better into memory (RAM) and are faster to scan.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Not so efficient: indexing a long string&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Much better: use a fixed length CHAR for SKU codes (if always same length)&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;sku&lt;/span&gt; &lt;span class="nb"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Using VARCHAR(255) for a 5-character code is like buying a mansion for your pet hamster. He'll get lost in there.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  3. &lt;strong&gt;Query Speed&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Databases can process smaller data types faster. Arithmetic operations, comparisons, and joins all benefit from efficient data representation.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Using BIGINT for small numbers is overkill and slows things down&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="nb"&gt;BIGINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;total_amount&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- INT is enough for most IDs (unless you’re Amazon)&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;total_amount&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Unless your user base is bigger than the population of Earth, stick with INT!&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  4. &lt;strong&gt;Data Integrity&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Choosing the right type also enforces valid data. For example, using &lt;code&gt;DATE&lt;/code&gt; for dates (instead of &lt;code&gt;VARCHAR&lt;/code&gt;) ensures only valid dates are stored and enables date-specific functions and comparisons.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Anyone can type anything here. "Banana" is NOT a valid date.&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;event_date&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- This keeps your data honest!&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;event_date&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Your future self will thank you when you’re not debugging why someone’s birthday is set to "tomorrow".&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  5. &lt;strong&gt;ENUMs and Constraints: Keep Data Predictable&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Without ENUM: Anything goes&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- With ENUM: Only what you allow&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="nb"&gt;ENUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'todo'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'in_progress'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'done'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;If only life had ENUMs, no more ambiguous "it's complicated" statuses!&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  More Real-World Code Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Storing Money: Use DECIMAL, Not FLOAT
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Oops! FLOAT can lead to rounding errors. Not what you want for money!&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="nb"&gt;FLOAT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- DECIMAL is designed for currency. Your accountant says 'thanks'!&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;payments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;You wouldn’t pay someone $99.999999 instead of $100, would you?&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Storing IP Addresses: Use INET (if available)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Old school: storing as string works, but wastes space and is harder to query&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;logins&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;ip_address&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- Modern: Use INET for compact storage and validation (Postgres example)&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;logins&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;ip_address&lt;/span&gt; &lt;span class="n"&gt;INET&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Don’t Over-allocate String Lengths
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Do you really need 255 characters for a country code?&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;locations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;country_code&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;-- 2 chars is enough for ISO country codes!&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;locations&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;country_code&lt;/span&gt; &lt;span class="nb"&gt;CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Remember, every byte counts, and your DBA is watching!&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practices for Data Type Selection
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Know Your Data Ranges&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Analyze the expected range and format of your data before choosing a type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Be Specific, Not Generic&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Don’t use &lt;code&gt;VARCHAR(255)&lt;/code&gt; everywhere just for convenience. Use &lt;code&gt;CHAR&lt;/code&gt; for fixed-length codes, and use precise numeric types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Avoid Over-Allocation&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Don’t use &lt;code&gt;BIGINT&lt;/code&gt; if &lt;code&gt;INT&lt;/code&gt; suffices, or &lt;code&gt;TEXT&lt;/code&gt; if a short string is enough.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Leverage Database Features&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Use enumerated types (like &lt;code&gt;ENUM&lt;/code&gt; in MySQL or &lt;code&gt;CHECK&lt;/code&gt; constraints in PostgreSQL) for columns with a limited set of valid values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Consider Future Growth&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Leave some space for growth, but don’t overdo it. If you expect your user count to never exceed 100,000, &lt;code&gt;MEDIUMINT&lt;/code&gt; may be better than &lt;code&gt;BIGINT&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Data Types and Their Uses
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Data Type&lt;/th&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;INT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Whole numbers (IDs, counts)&lt;/td&gt;
&lt;td&gt;4 bytes, -2B to 2B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;TINYINT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Small numbers (flags, ratings)&lt;/td&gt;
&lt;td&gt;1 byte, -128 to 127&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;VARCHAR(n)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Variable-length strings&lt;/td&gt;
&lt;td&gt;Use for text, but size matters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CHAR(n)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Fixed-length strings&lt;/td&gt;
&lt;td&gt;Codes, hashes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;DATE&lt;/code&gt;, &lt;code&gt;TIME&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Dates and times&lt;/td&gt;
&lt;td&gt;Use for temporal data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;BOOLEAN&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;True/false flags&lt;/td&gt;
&lt;td&gt;May be stored as TINYINT or BIT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DECIMAL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Exact numeric (currency)&lt;/td&gt;
&lt;td&gt;Avoids rounding errors&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




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

&lt;p&gt;The next time you create or modify a database schema, don’t overlook the importance of selecting the right data types. Your choices will impact everything from performance and scalability to integrity and maintainability.&lt;/p&gt;

&lt;p&gt;Want to see the difference for yourself? Try benchmarking queries against tables with different data types, and watch how performance and storage requirements change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optimize smart, choose the right types!&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;em&gt;And remember: “With great power (of data types) comes great responsibility.”&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Have tips or stories about optimizing databases? Share them in the comments below! Or just tell us your wildest VARCHAR(255) horror story... We’re listening!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>database</category>
    </item>
    <item>
      <title>Do We Really Need Another Framework?</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Thu, 12 Jun 2025 15:04:00 +0000</pubDate>
      <link>https://dev.to/raxraj/do-we-really-need-another-framework-4pag</link>
      <guid>https://dev.to/raxraj/do-we-really-need-another-framework-4pag</guid>
      <description>&lt;p&gt;Let's face it, the JavaScript ecosystem has gone absolutely bonkers. Just when you've finally wrapped your head around React hooks, someone drops a tweet about how "&lt;code&gt;Qwik&lt;/code&gt; is the future" and suddenly you're questioning your entire career choice. Sound familiar? Welcome to framework fatigue—the special kind of exhaustion that only web developers truly understand.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"The average enterprise application now uses 14.5 JavaScript libraries and frameworks; &lt;a href="https://medium.com/%40TechnologyDiaries/dont-get-lost-in-the-javascript-ecosystem-the-hottest-frameworks-of-2025-react-vue-angular-and-7fcc714913f4" rel="noopener noreferrer"&gt;71% of developers report experiencing 'framework fatigue'&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  When "Hello World" Feels Like Climbing Everest
&lt;/h2&gt;

&lt;p&gt;Remember when you first learned JavaScript? Those simple days of document.getElementById() and feeling like a wizard when you made something move on screen? Fast forward to today, where setting up a "simple project" means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choosing between 37 frameworks&lt;/li&gt;
&lt;li&gt;Installing 4,328 dependencies&lt;/li&gt;
&lt;li&gt;Configuring webpack (or wait, should I use Vite now? Or Turbopack? Or...)&lt;/li&gt;
&lt;li&gt;Staring blankly at your terminal as error messages scroll by&lt;/li&gt;
&lt;li&gt;Questioning all your life choices&lt;/li&gt;
&lt;/ol&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%2Fvxnfqgbjnnnp3moz0dlm.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%2Fvxnfqgbjnnnp3moz0dlm.png" alt="Engineer Overwhelmed with frameworks" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's no wonder we're tired. We're not just building websites anymore—we're maintaining an entire ecosystem of tools just to begin with a project.&lt;/p&gt;

&lt;h2&gt;
  
  
  The FOMO Is Real (And It's Spectacular)
&lt;/h2&gt;

&lt;p&gt;Nothing triggers developer anxiety quite like a Medium article titled "Why [Your Current Framework] Is Dead." Suddenly, that project you've spent months mastering feels like a career liability. The subreddit threads don't help either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Anyone still using React in 2025 is basically writing COBOL."&lt;/li&gt;
&lt;li&gt;"Switched to Svelte and now I can build in 3 hours what took 3 weeks in Angular."&lt;/li&gt;
&lt;li&gt;"My cat learned Solid.js and now she's making $250k at a start-up."&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Fear-of-being-left-behind creates self-fulfilling prophecies because developers abandon perfectly good tools to avoid being obsoleted.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This isn't just annoying—it's psychologically damaging. We're creating a generation of developers who feel perpetually behind despite working harder than ever to keep up.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why Is Nobody Talking About How AI Is Just a Statistical Model—And Most Code Is a Mess?</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Sun, 20 Apr 2025 21:50:21 +0000</pubDate>
      <link>https://dev.to/raxraj/why-is-nobody-talking-about-how-ai-is-just-a-statistical-model-and-most-code-is-a-mess-5g29</link>
      <guid>https://dev.to/raxraj/why-is-nobody-talking-about-how-ai-is-just-a-statistical-model-and-most-code-is-a-mess-5g29</guid>
      <description>&lt;p&gt;AI is revolutionizing software development. Tools like GitHub Copilot and ChatGPT are assisting developers in writing code faster than ever. But amidst this excitement, a critical question arises:&lt;/p&gt;

&lt;p&gt;Are we scaling excellence, or just automating mediocrity?&lt;/p&gt;

&lt;h2&gt;
  
  
  How AI Code Generation Works?
&lt;/h2&gt;

&lt;p&gt;At their core, AI code generation tools utilize machine learning and natural language processing to interpret prompts and generate corresponding code. They are trained on vast repositories of code, enabling them to understand syntax, structure, and common coding practices. When a developer inputs a prompt, the model predicts the next most likely code segment, akin to how autocomplete functions in text editors. This approach doesn't guarantee &lt;strong&gt;optimal or innovative solutions&lt;/strong&gt;; instead,_ it often reproduces what's most common in the training data._ &lt;a href="https://www.wired.com/story/github-commercial-ai-tool-built-open-source-code/?utm_source=chatgpt.com" rel="noopener noreferrer"&gt;For instance, GitHub's Copilot has been observed to generate code snippets that closely resemble existing open-source code, raising concerns about originality and potential licensing issues.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Reality of "Most Code"
&lt;/h2&gt;

&lt;p&gt;It's an open secret in the developer community: most codebases are cluttered with technical debt, outdated practices, and quick fixes. When AI models are trained on such data, they risk perpetuating these issues. As highlighted by &lt;a href="https://arc.dev/talent-blog/impact-of-ai-on-code/" rel="noopener noreferrer"&gt;GitClear's analysis of over 153 million lines of code&lt;/a&gt;, there's evidence suggesting a "downward pressure on code quality" with the increased use of AI code assistants.&lt;/p&gt;

&lt;h2&gt;
  
  
  Security Vulnerabilities in AI-Generated Code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. High Incidence of Insecure Code:
&lt;/h3&gt;

&lt;p&gt;A study by &lt;a href="https://www.wired.com/story/github-commercial-ai-tool-built-open-source-code/" rel="noopener noreferrer"&gt;NYU researchers revealed&lt;/a&gt; that approximately 40% of code produced by GitHub Copilot in certain scenarios contained security vulnerabilities. This underscores the potential risks of relying solely on AI for code generation without thorough human oversight.&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%2Fukni2twmx4cw9dzd796o.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%2Fukni2twmx4cw9dzd796o.png" alt="AI Security Back-doors" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Leakage of Sensitive Information
&lt;/h3&gt;

&lt;p&gt;GitHub Copilot has been observed to suggest &lt;a href="https://blog.gitguardian.com/github-copilot-security-and-privacy/?utm_source=chatgpt.com" rel="noopener noreferrer"&gt;code snippets that inadvertently include sensitive information&lt;/a&gt;, such as API keys and credentials. This poses a significant risk, as such information can be exploited by malicious actors. ​&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Propagation of Existing Vulnerabilities
&lt;/h3&gt;

&lt;p&gt;AI models trained on vast code repositories may inadvertently learn and reproduce existing vulnerabilities. This means that insecure coding patterns can be perpetuated, leading to widespread security issues in AI-generated code. &lt;/p&gt;

&lt;h2&gt;
  
  
  Navigating the AI Coding Landscape
&lt;/h2&gt;

&lt;p&gt;To harness the benefits of AI while mitigating risks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Code Reviews: Always subject AI-generated code to thorough human reviews.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Continuous Learning: Encourage developers to understand and question AI suggestions, fostering a culture of learning and critical thinking.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Training Data Scrutiny: Advocate for AI models trained on high-quality, vetted codebases to ensure better output.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;AI is a powerful tool in modern software development, but it's not infallible. Recognizing its limitations and potential pitfalls is crucial. By combining AI capabilities with human judgment and best practices, we can strive for code that's not just functional, but also robust and maintainable.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>ai</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Unleashing the Power of Utility Types in TypeScript</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Sun, 10 Nov 2024 05:36:21 +0000</pubDate>
      <link>https://dev.to/raxraj/unleashing-the-power-of-utility-types-in-typescript-38fg</link>
      <guid>https://dev.to/raxraj/unleashing-the-power-of-utility-types-in-typescript-38fg</guid>
      <description>&lt;p&gt;Hey there, TypeScript warrior! 💪 So, you’re knee-deep in TypeScript, and maybe you’ve wondered, &lt;em&gt;"Why is there so much code just for managing types?"&lt;/em&gt; Don’t worry—you’re not alone. We’re all on this wild, statically-typed ride together.&lt;/p&gt;

&lt;p&gt;Today, we’re diving into some of TypeScript’s coolest tools, Utility Types. These are like those little cheat codes in a video game that help you speed up and simplify your code. So, let’s roll up our sleeves and take a fun tour of the most useful utility types in TypeScript—and how they can make your code way less of a headache.&lt;/p&gt;

&lt;h2&gt;
  
  
  Whaaat? More types.
&lt;/h2&gt;

&lt;p&gt;Imagine you’re trying to work with TypeScript types without writing everything from scratch (because who’s got time for that?). Utility types let you create new types by transforming or reusing parts of other types. Think of them as those fancy kitchen gadgets that slice and dice without needing any extra effort. They save time, reduce redundancy, and make your code a whole lot easier to manage. Let’s break them down one by one!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Partial&amp;lt;Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Does&lt;/strong&gt;: Makes all the properties in a type optional. Yeah, it’s that easy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why You’ll Love It&lt;/strong&gt;: Perfect for those times when you need some of an object, but not all of it.&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;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;id&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="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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&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="nx"&gt;newValues&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="o"&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="c1"&gt;// Update only the properties you need, no need to bring everything.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="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;Ash&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt; &lt;span class="c1"&gt;// Only updating name, thanks to Partial!&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;Required&amp;lt;Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Does&lt;/strong&gt;: Makes all properties in a type mandatory. TypeScript’s little way of saying, “No skipping any fields!”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why You’ll Love It&lt;/strong&gt;: Sometimes, optional just isn’t an option. You need everything locked and loaded, especially if certain properties were optional before.&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;UserWithOptionalFields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&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="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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Required&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserWithOptionalFields&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="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="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;John&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;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="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// All properties are now required!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;Record&amp;lt;Keys, Type&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Does&lt;/strong&gt;: Creates an object type with the keys Keys and values Type. Think of it as TypeScript’s “create your own dictionary” feature.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why You’ll Love I&lt;/strong&gt;t: This type is your best friend when you want a quick-and-easy way to map out data, especially if you know the keys ahead of time.&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;Role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;guest&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;rolePermissions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="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="na"&gt;admin&lt;/span&gt;&lt;span class="p"&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;read&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;write&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;delete&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&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;read&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;write&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;guest&lt;/span&gt;&lt;span class="p"&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;read&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;Pick&amp;lt;Type, Keys&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Doe&lt;/strong&gt;s: Takes an existing type and picks only the properties you specify. Think of it as TypeScript’s way of saying, “Let’s keep this short and sweet.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why You’ll Love It&lt;/strong&gt;: Perfect for creating subsets of an existing type without dragging everything along, and cause it is one of my favourites.&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;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;id&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="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="nl"&gt;age&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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;UserPreview&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Pick&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&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;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;Omit&amp;lt;Type, Keys&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Does&lt;/strong&gt;: It’s the opposite of Pick. It lets you exclude specific properties from a type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why You’ll Love It&lt;/strong&gt;: Great when you have a type, but there’s just one pesky field you don’t want.&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;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;id&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="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="nl"&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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PublicUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&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;password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;Exclude&amp;lt;Type, ExcludedUnion&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Does&lt;/strong&gt;: Removes certain types from a union. It’s the “kick out of the group chat” of TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why You’ll Love It&lt;/strong&gt;: It’s super useful when you’ve got a big union but only need to handle some cases.&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;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;NonLoadingStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;Extract&amp;lt;Type, Union&amp;gt;&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What It Does&lt;/strong&gt;: The twin sibling of Exclude, Extract keeps only the types that match a given union.&lt;/p&gt;

&lt;p&gt;Why You’ll Love It: This type narrows down a union type to only relevant pieces.&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;Status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LoadingStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Extract&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loading&lt;/span&gt;&lt;span class="dl"&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;// Just "loading"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! 🎉 While &lt;code&gt;Extract&lt;/code&gt; and &lt;code&gt;Pick&lt;/code&gt;may sound alike, they’re uniquely suited for different jobs: &lt;code&gt;Extract&lt;/code&gt;is your filter for union types, while &lt;code&gt;Pick&lt;/code&gt;lets you cherry-pick properties from object types. The same goes for their counterparts, &lt;code&gt;Exclude&lt;/code&gt;and &lt;code&gt;Omit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These utility types are small but mighty! They save time, reduce code redundancy, and keep things tidy. So next time you’re wrestling with types, remember these helpers. They’ll be there to simplify your TypeScript journey and make it a little more fun. Happy coding, and may your types always be exactly what you need! 🚀&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Scaling Node.js to Infinity and Beyond: Cluster and Load Balancing for Galactic Performance.</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Thu, 03 Aug 2023 15:45:07 +0000</pubDate>
      <link>https://dev.to/raxraj/scaling-nodejs-to-infinity-and-beyond-cluster-and-load-balancing-for-galactic-performance-g60</link>
      <guid>https://dev.to/raxraj/scaling-nodejs-to-infinity-and-beyond-cluster-and-load-balancing-for-galactic-performance-g60</guid>
      <description>&lt;p&gt;Node.js, the superhero of web development, has swooped in to save the day for building high-performance and scalable web applications. With its lightning-fast event-driven, non-blocking powers, it tackles even the most demanding concurrent connections. But just like our favourite caped crusader, Node.js realises that even superheroes need a sidekick when the going gets tough. That's where the dynamic duo of cluster and load balancing steps in, working hand in hand to give Node.js the ability to scale to infinity and beyond, achieving performance that's out of this world. Let's dive into the exciting world of scaling Node.js to infinity and beyond!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Node.js Cluster: Harnessing the Power of Multiprocessing
&lt;/h2&gt;

&lt;p&gt;When it comes to scaling and parallel processing, Node.js has a trusty sidekick built right in: the Cluster module. This powerful companion enables developers create a cluster of worker processes that share the same server port. With its superpowers of leveraging multiprocessor systems, the Cluster module enables your application to tap into the full potential of every CPU core, distributing the workload among the team. Let's have a look on how we can utilise this &lt;code&gt;Robin&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;First of all, we import the necessary modules for our server application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cluster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cluster&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;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&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;numCPUs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;os&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;cpus&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We import the cluster, express, and os modules.&lt;/p&gt;

&lt;p&gt;After importing the required modules, we'll use the &lt;code&gt;cluster&lt;/code&gt; module to form a worker process cluster.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isMaster&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create a worker process for each CPU core&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;numCPUs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fork&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Event listener for when a worker process exits&lt;/span&gt;
  &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;exit&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;worker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signal&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="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; died`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Fork a new worker to replace the exited one&lt;/span&gt;
    &lt;span class="nx"&gt;cluster&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fork&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 start by verifying if the current process is the master process through &lt;code&gt;cluster.isMaster&lt;/code&gt;. The master process handles the management and creation of worker processes.&lt;/p&gt;

&lt;p&gt;If the current process is the master process, we can initiate the creation of the cluster. In this case, we aim to have one process per CPU core, and we utilise the 'exit' event emitted by the &lt;code&gt;cluster&lt;/code&gt; module to automatically restart any process that exits unexpectedly. This way, the desired number of worker processes is consistently maintained.&lt;/p&gt;

&lt;p&gt;If the current process is not the master process, it should execute the main code, which could involve running the Express server or any other relevant tasks for the application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create an Express app&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="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// Define routes&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&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;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, world!&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;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8000&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="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="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; started`&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;When the SIGTERM signal is received, we implement graceful server shutdown. This involves closing the server, allowing ongoing requests to complete, and terminating the worker process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// Gracefully handle server shutdown&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SIGTERM&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;close&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="s2"&gt;`Worker &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; terminated`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&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;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;h2&gt;
  
  
  2. Intergalactic Load Balancing: Handling Massive Traffic Flows
&lt;/h2&gt;

&lt;p&gt;In the realm of Node.js applications, load balancing emerges as a pivotal technique for achieving scalability. By intelligently distributing incoming requests across multiple servers or worker processes, this approach optimises resource utilisation, safeguarding against bottlenecks that could impede performance.&lt;/p&gt;

&lt;p&gt;Load balancing is a technique that distributes traffic across multiple servers. This can help to improve the performance and availability of your application.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Importance of Load Balancing in Node.js
&lt;/h3&gt;

&lt;p&gt;Node.js is known for its event-driven, non-blocking I/O model, which makes it very efficient at handling concurrent connections. Well, even though it's great at that, there's a point where it can get overwhelmed if there are &lt;strong&gt;&lt;u&gt;&lt;em&gt;too many people&lt;/em&gt;&lt;/u&gt;&lt;/strong&gt; trying to use the application all at once. That's where load balancing comes into play.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing Load Balancer in Express + Node.js
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First, make sure you have &lt;code&gt;Express&lt;/code&gt; and &lt;code&gt;http-proxy-middleware&lt;/code&gt; installed. If not, install them by running the following command;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express http-proxy-middleware

&lt;span class="c"&gt;# or&lt;/span&gt;

yarn add express http-proxy-middleware

&lt;span class="c"&gt;# or&lt;/span&gt;

pnpm &lt;span class="nb"&gt;install &lt;/span&gt;express http-proxy-middleware
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In your &lt;code&gt;main.js&lt;/code&gt; file, where we have the logic to spin-off our server, import the required modules
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createProxyMiddleware&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http-proxy-middleware&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;ul&gt;
&lt;li&gt;Now import &lt;code&gt;express&lt;/code&gt; and create an app. Also define the target servers. These target servers are instances of your Node.js application that are running on different ports
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&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;targetServers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:3000&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="s1"&gt;http://localhost:3001&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="s1"&gt;http://localhost:3002&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This array contains the URLs of the target servers (instances of your Node.js server) that will handle the incoming requests. In this example, we assume there are three target servers running on ports 3000, 3001, and 3002.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up the load balancer middleware using the &lt;code&gt;createProxyMiddleware&lt;/code&gt; function.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;loadBalancer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createProxyMiddleware&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;targetServers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;changeOrigin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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;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;Proxy error:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&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;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&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="s1"&gt;text/plain&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something went wrong. Please try again later.&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;ul&gt;
&lt;li&gt;The &lt;code&gt;loadBalancer&lt;/code&gt; middleware thus created can be used to apply Load Balancing on our express server. Now just listen to all the ports using load balancer.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Use the load balancer middleware for all incoming requests&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loadBalancer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Start the load balancer server on port 8080&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&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="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="s2"&gt;`Load balancer with random strategy listening on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The one we have implemented above is called &lt;code&gt;Random Load Balancing&lt;/code&gt;. The world of load balancing offers a variety of other techniques, each with its own strengths and advantages. In the upcoming article, we will dive into these alternative methods to further enhance the performance and scalability of our Node.js applications&lt;/p&gt;

&lt;p&gt;In conclusion, Load balancing and clustering are a powerful duo in Node.js, improving performance and scalability. Load balancing shares incoming requests among servers, while clustering utilizes CPU cores effectively. Together, they create an efficient and resilient system capable of handling high traffic. If you need more help, feel free to ask!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>From Linux to MacOS: A Journey of Surprising Delight</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Mon, 03 Jul 2023 16:14:33 +0000</pubDate>
      <link>https://dev.to/raxraj/from-linux-to-macos-a-journey-of-surprising-delight-219e</link>
      <guid>https://dev.to/raxraj/from-linux-to-macos-a-journey-of-surprising-delight-219e</guid>
      <description>&lt;p&gt;In a world where opinions clash and technology fandoms thrive, I found myself as an unlikely critic of all things Apple. From their UI decisions on iPhones to their closed ecosystem, I was known to roll my eyes at the mere mention of macOS. But life has a funny way of challenging our preconceived notions. In a surprising turn of events, I recently embarked on a journey that led me to switch from Linux to macOS. Expecting a bumpy road ahead, I braced myself for the worst. However, what unfolded was a delightful surprise that left me grinning like a Cheshire cat. Join me on this whimsical journey as I share how macOS charmed its way into my heart, turning this once skeptical critic into a happy convert.&lt;/p&gt;

&lt;h2&gt;
  
  
  Day 1: A Leap of Faith
&lt;/h2&gt;

&lt;p&gt;As I unboxed my shiny new MacBook Air, a wave of uncertainty washed over me. Holding it in my hands, I couldn't help but ponder the decision I had made. Why would I trade in my powerful, open-source Linux machine, armed with 16GB RAM, a whopping 512GB SSD, and a trusty NVIDIA graphics card, for this seemingly corporate contraption with a mere 8GB of RAM?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fj2bwafy4jpupakuayhun.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fj2bwafy4jpupakuayhun.png" alt="Macbook"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With a deep breath, I reminded myself that sometimes stepping out of our comfort zones can lead to unexpected adventures. As I powered on the MacBook Air, I decided to keep an open mind and give macOS a fair chance to prove its worth. After all, who knows what hidden surprises this seemingly modest machine might have in store for me?&lt;/p&gt;

&lt;p&gt;One of the remarkable aspects of transitioning from Linux to macOS was the surprisingly smooth setup process. Unlike the intricate steps and time-consuming configurations often required on my Linux machines, setting up my Mac proved to be remarkably efficient and hassle-free.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fb89idildnwo2omp71gt2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fb89idildnwo2omp71gt2.jpg" alt="Apple Boot screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reflecting on this experience, I realized the value of a system that prioritizes user-friendly setup without compromising functionality. The ability to swiftly get my development environment up and running allowed me to focus more on my work and less on troubleshooting technicalities&lt;/p&gt;

&lt;h2&gt;
  
  
  Day 2 to 5: An unforeseen hiccup
&lt;/h2&gt;

&lt;p&gt;As I ventured further into my transition from Linux to macOS, I encountered an unexpected hiccup during one of my Node.js projects. Connecting to an Oracle database required Oracle Library, but unfortunately, there were no compatible versions available for the Mac M1 architecture.&lt;/p&gt;

&lt;p&gt;The realization of this limitation brought forth a momentary sense of frustration. The smooth setup experience I had previously praised now revealed a gap in compatibility and availability. It served as a reminder that no operating system is without its constraints and that transitioning between platforms often entails a degree of adaptation. This limitation hindered my progress, but I remained determined to find a solution. I discovered Rosetta—a remarkable feature. Rosetta seamlessly runs software designed for previous architectures on the M1 chip, overcoming compatibility issues effortlessly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Flny9tnvzof2fuwfp1csd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flny9tnvzof2fuwfp1csd.png" alt="Changed Architecture using Rosetta"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Realizing the power of Rosetta was a highlight of my journey. It showcased Apple's dedication to a seamless transition and user satisfaction. The ability to switch architectures with ease demonstrated Apple's engineering prowess and attention to detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Day 5 to 20
&lt;/h2&gt;

&lt;p&gt;During this phase, I discovered an array of developer-friendly features that made my JavaScript development experience truly delightful.&lt;/p&gt;

&lt;p&gt;I discovered a powerful macOS-exclusive terminal called "warp" designed for JavaScript developers. It offered features like an AI assistant, similar to "ask warp," and VS Code-like shortcuts and commands. Warp's capabilities deserve a separate blog to delve into its full potential and how it enhances the developer's workflow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fvrznla59sk2l2lyxd20g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fvrznla59sk2l2lyxd20g.png" alt="Warp Terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://app.warp.dev/referral/Z963WL" rel="noopener noreferrer"&gt;Install Warp in your MacOS&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Day 21 to 40
&lt;/h2&gt;

&lt;p&gt;As I become more accustomed to the operating system, I'm starting to appreciate its unique features, even though some take a bit of getting used to. For example, the Command button being positioned differently from the standard leftmost position initially seemed strange, but now I find it intriguing.&lt;/p&gt;

&lt;p&gt;However, I do miss the Windows button that I used in Ubuntu to quickly view all my running apps. Its absence in MacOS makes it a bit challenging for me to navigate between applications efficiently. Additionally, the gestures initially felt unfamiliar and didn't make much sense, but I'm gradually understanding their purpose and finding them more intuitive.&lt;/p&gt;

&lt;p&gt;One thing that still surprises me is the complete absence of Alt+F4, a familiar keyboard shortcut for closing windows. Instead, Extreme Delight relies on the Command+Q combination, which not only closes the active window but also closes all other windows of the same application. It took me some time to adjust to this behavior, as I often found myself missing the simplicity of Alt+F4.&lt;/p&gt;

&lt;p&gt;And not only the good things missing, but the bad things missing too hihi the start menu with it sluggish search bar, The almost always bugging out task bar area and a itching edge dominance/defaulting.&lt;/p&gt;

&lt;p&gt;The switch from Windows to macOS not only brings the absence of certain positive features but also provides relief from some frustrating aspects. Gone is the sluggish Start Menu search and occasional taskbar bugs that Windows users might have encountered. Additionally, there is no dominant push for a specific browser, unlike Microsoft Edge in Windows. Overall, macOS offers a more stable and reliable experience while respecting user preferences and providing a smoother interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F1ywtw20pbujll89qol19.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1ywtw20pbujll89qol19.jpg" alt="Bad Windows Search Bar"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The last 20 days
&lt;/h2&gt;

&lt;p&gt;During my last 20 days in macOS after transitioning from Ubuntu, there have been several standout features that I absolutely love. The touchpad has been a true delight, with its incredible haptic feedback and seamless navigation. I also appreciate the Safari integration with a fingerprint-based password manager, which adds convenience and security to my browsing experience. &lt;/p&gt;

&lt;p&gt;And let's not forget about the zsh shell, which is like having a witty and lightning-fast personal assistant in the command line. It's the only shell that can tell a "ls" from an "ls -al" joke without missing a beat.&lt;/p&gt;

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

&lt;p&gt;In conclusion, my transition to macOS from Ubuntu has been a positive and enjoyable experience. I have discovered numerous features that I genuinely love, such as the responsive touchpad, seamless window transitions, and the convenience of Safari's fingerprint-based password manager. The zsh shell has been a delight to use, adding a touch of humour and efficiency to my command-line interactions. Whether it's the fast-moving mouse pointer or the quick access to the side bar for fun shortcuts, macOS has truly added an element of fun to my computing life and the streamlined installation process, macOS has exceeded my expectations. Overall, these past 60 days have been a delightful journey, filled with both productivity and laughter&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>productivity</category>
      <category>programming</category>
    </item>
    <item>
      <title>Code Like a Craftsman: Best Practices for Writing Elegant and Maintainable Code</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Thu, 25 May 2023 13:40:27 +0000</pubDate>
      <link>https://dev.to/raxraj/code-like-a-craftsman-best-practices-for-writing-elegant-and-maintainable-code-2l0f</link>
      <guid>https://dev.to/raxraj/code-like-a-craftsman-best-practices-for-writing-elegant-and-maintainable-code-2l0f</guid>
      <description>&lt;p&gt;Writing messy code is like joining the dark side of the Force. It may seem tempting at first, with its promises of quick results and powerful abilities. But just like the dark side, it ultimately leads to chaos, confusion, and bugs.&lt;/p&gt;

&lt;p&gt;Instead, embrace the Jedi way of clean code. Let go of your attachments to shortcuts and hacks, and focus on the fundamentals. In this article we are gonna look into these fundamentals of clean code that will help you become a Jedi Master of programming. So, get ready to wield your lightsaber of clean code and let's embark on this epic journey together!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Use Descriptive variable names
&lt;/h2&gt;

&lt;p&gt;Let's face it, choosing variable names can be a daunting task. It's like naming your newborn baby, except you have to name it dozens of times a day. But fear not, young padawan, for there is a simple solution: choose names that make sense!&lt;/p&gt;

&lt;p&gt;Think about it, if you saw a variable named "a" in your code, would you have any idea what it represents? It could be an integer, a string, or even a Jedi mind trick for all you know.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;d&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;b&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;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;d&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;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&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;z&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="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="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can you understand the purpose and functionality of this function by looking at the variable names used within it? I hope the answer is 'no', otherwise I'll be afraid that you're leaning towards the dark side of the force and have a strong ability to comprehend poorly named functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculateTotalPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numItems&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pricePerItem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;taxRate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;discountRate&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;subtotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numItems&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;pricePerItem&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;tax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;subtotal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;taxRate&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;discount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;subtotal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;discountRate&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;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;subtotal&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;tax&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;discount&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;total&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;totalPrice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;calculateTotalPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.08&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.05&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="nx"&gt;totalPrice&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 example, we've used variable names so descriptive that even a confused alien from another galaxy would understand what they represent. And let's not overlook the function name that's so precisely chosen. Even Darth Vader would have a hard time using the Force to obscure the purpose of this function!&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Letting Clarity Prevail Over Comments
&lt;/h2&gt;

&lt;p&gt;Comments should not be the first choice for describing what the code does. They should be used sparingly and only when absolutely necessary. Clean code aims to be self-explanatory, relying on meaningful variable and function names, along with a logical code structure, to convey its purpose. Comments can be helpful for explaining complex algorithms, unusual solutions, potential pitfalls, or interactions with external systems. However, it's crucial to keep comments up to date and ensure they add real value. Clean code speaks for itself, while comments should only supplement understanding when essential.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Perform a binary search on the sorted array&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;left&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="c1"&gt;// Initialize the left boundary of the search range&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&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;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Initialize the right boundary of the search range&lt;/span&gt;

    &lt;span class="c1"&gt;// Continue searching while the left boundary is less than or equal to the right boundary&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;right&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;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&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="c1"&gt;// Calculate the middle index&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;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// If the middle value matches the target, we have found the target value&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&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;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// If the middle value is less than the target, the target value is in the right half of the array&lt;/span&gt;
            &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Update the left boundary to be mid + 1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// If the middle value is greater than the target, the target value is in the left half of the array&lt;/span&gt;
            &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Update the right boundary to be mid - 1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// If the target value is not found in the array, return -1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The existing comments in the code unnecessarily restate information that is already apparent from the code itself. The purpose of variable initialisation and the logic of the binary search algorithm can be easily understood by reading the code without relying on excessive comments.&lt;/p&gt;

&lt;p&gt;What if I tell you that this code can look a lot more simple and clean without even changing anything in the code itself? That would be &lt;em&gt;Legen - wait for it - dary. Legendary.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Perform a binary search on the sorted array&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;binarySearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;left&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&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;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;right&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;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Found the target value at index mid&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;mid&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;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Target value is in the right half of the array&lt;/span&gt;
            &lt;span class="nx"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Target value is in the left half of the array&lt;/span&gt;
            &lt;span class="nx"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Target value not found in the array&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/fSYYG9MSibrmPSuIJ0/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/fSYYG9MSibrmPSuIJ0/giphy.gif" width="480" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Comments like these help to demystify complex algorithms, making them more approachable and understandable for developers who may encounter the code later on. They provide valuable explanations and guide the reader through intricate logic, making it easier to follow and maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  3.  Embrace the Power of Short and Focused Functions
&lt;/h2&gt;

&lt;p&gt;Remember what Yoda said, "Size matters does not." This is especially true for functions - long, convoluted ones can be like navigating through a dense asteroid field. Instead, break them down into smaller, focused functions with descriptive names. Think of each function like a droid in your own personal Star Wars saga, working together to achieve a common goal.&lt;/p&gt;

&lt;p&gt;By using this approach, you'll benefit from greater modularity, reusability, and easier testing. You'll be able to see the code's flow like Obi-Wan saw the Force. So, follow in the footsteps of the Jedi, and let your functions be like lightsabers - short, elegant, and powerful.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Follow the SOLID Principle
&lt;/h2&gt;

&lt;p&gt;The SOLID principles are a set of five design principles introduced by Robert C. Martin (Uncle Bob) to guide developers in writing clean, modular, and maintainable code. These principles are widely regarded as fundamental in software development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;SRP (Single Responsibility Principle): A class should have only one reason to change, with a single responsibility.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;OCP (Open-Closed Principle): Software entities should be open for extension but closed for modification, allowing for behavior extension without changing existing code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;LSP (Liskov Substitution Principle): Subtypes should be substitutable for their base types without altering the correctness of the program.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ISP (Interface Segregation Principle): Clients should not be forced to depend on interfaces they don't use; interfaces should be specific and tailored to clients' needs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DIP (Dependency Inversion Principle): High-level modules should not depend on low-level modules; both should depend on abstractions, promoting loose coupling.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Unit Tests are the way
&lt;/h2&gt;

&lt;p&gt;Imagine you're a Jedi Knight tasked with constructing a lightsaber. To ensure its reliability and effectiveness, you'd want to test each component individually. In software development, a unit test is like wielding a lightsaber in a controlled environment, examining each piece's functionality.&lt;br&gt;
By writing automated unit tests, you channel your inner Jedi and gain confidence in the behavior of your code. Just as Jedi Knights rely on their lightsabers in battle, you can rely on your unit tests to catch bugs early on, acting as your loyal companions in the ongoing fight against software defects.&lt;/p&gt;

&lt;p&gt;In conclusion, embracing the ways of the Jedi and prioritizing clean code leads to software that is as clear as a Jedi's mind. It allows developers to effortlessly understand, modify, and maintain their code, avoiding the dark side of bugs and confusion. Collaboration among developers becomes as harmonious as a Jedi Council, and the Force of clean code saves time and effort in the long run. By following these Jedi principles and honing their skills in writing clean code, developers can become true Jedi Masters, ensuring the quality and success of their development projects. May the clean code be with you!&lt;br&gt;
May the code be with you, always.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/MQ0YzAeAN03T0tHJip/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/MQ0YzAeAN03T0tHJip/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Building a Custom React Hook for Data Fetching: A Practical Guide</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Wed, 10 May 2023 14:53:10 +0000</pubDate>
      <link>https://dev.to/raxraj/building-a-custom-react-hook-for-data-fetching-a-practical-guide-3jjc</link>
      <guid>https://dev.to/raxraj/building-a-custom-react-hook-for-data-fetching-a-practical-guide-3jjc</guid>
      <description>&lt;p&gt;React hooks have been around for a little while now, but they're still an incredibly powerful tool for reusing stateful logic in your React components. And if you're really feeling adventurous, you can even create your very own custom hooks!&lt;/p&gt;

&lt;p&gt;With custom hooks, you can turn those pesky repetitive patterns into a super-cool function that you can use over and over again in all your different components. It's like having a secret weapon in your coding arsenal!&lt;/p&gt;

&lt;p&gt;One common use case for custom hooks is data fetching, and in this tutorial, we're going to create a custom hook that uses the &lt;a href="https://jsonplaceholder.typicode.com"&gt;JSONPlaceholder&lt;/a&gt; API to fetch some data and display it in our application. With this custom hook, you'll be able to fetch data with ease, and even better, you can reuse it across different components in your app. So grab your coding hat and let's get started! &lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create React App
&lt;/h2&gt;

&lt;p&gt;Alrighty then, let's get this React party started! To create a new React app, you'll want to pop open your terminal and run this little command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-react-app custom-hook
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a new React app in a directory called &lt;code&gt;custom-hook&lt;/code&gt;. Once the installation is complete, navigate to the new directory by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;custom-hook
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Create the hook file
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;useDataFetching.js&lt;/code&gt; file in the src folder of your project to match the required folder structure. After navigating to the src folder in a code editor or terminal, create the file and verify that the folder structure of your project is correct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;custom-hook/
  README.md
  node_modules/
  package.json
  src/
    App.js
    useDataFetching.js
    components/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Code
&lt;/h2&gt;

&lt;p&gt;Custom hooks are essentially just functions that follow a specific naming convention, where the name of the function starts with the prefix &lt;code&gt;use&lt;/code&gt;. This naming convention is important, because it tells React that the function is a hook, and it can be used with the &lt;code&gt;useState&lt;/code&gt;, &lt;code&gt;useEffect&lt;/code&gt;, and other built-in hooks.&lt;/p&gt;

&lt;p&gt;We will be using the useState hook to define state variables for &lt;code&gt;error&lt;/code&gt;, &lt;code&gt;loading&lt;/code&gt;, and &lt;code&gt;data&lt;/code&gt;. These three state variables, that contain essential information about the API call. The first variable indicates whether the API call was successful or not. The second variable lets the component know if the data is still loading or if it's already available. The third variable contains the actual data that the component needs to render. In a nutshell, this hook acts as a helper function that simplifies API call handling and enables components to easily access and utilize the data they need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&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;function&lt;/span&gt; &lt;span class="nx"&gt;useDataFetching&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&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;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&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;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&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;useDataFetching&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fetch data from the API when the component mounts, we utilise the &lt;code&gt;useEffect&lt;/code&gt; hook. Since this hook runs asynchronously, we need to mark the function as &lt;code&gt;async&lt;/code&gt; and utilise &lt;code&gt;await&lt;/code&gt; to handle the response. By doing so, we ensure that the response is properly handled before continuing with the rest of the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&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;function&lt;/span&gt; &lt;span class="nx"&gt;useDataFetching&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&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;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&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;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;useEffect&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;try&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;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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;json&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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="nx"&gt;fetchData&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="nx"&gt;url&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;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&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;useDataFetching&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Utilise the Custom Hook
&lt;/h2&gt;

&lt;p&gt;Now that we've created our custom hook, let's use it in our application.&lt;/p&gt;

&lt;p&gt;Open the App.js file and replace the default content with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;useDataFetching&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;./useDataFetching&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="nx"&gt;App&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useDataFetching&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/posts&lt;/span&gt;&lt;span class="dl"&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;loading&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;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Loading&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;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&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;error&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;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&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;post&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;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&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;h3&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;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&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;/h3&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;p&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;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&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;/p&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="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;To see your custom hook in action just execute the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this tutorial, we took a stroll through the park of custom React hooks for data fetching. Using this hook is like having a magical wand that can help eliminate code redundancy and enhance the recyclability of your code. I also showed you how to use the hook in a simple example component. And just like how you can teach an old dog new tricks, you can learn to create custom hooks that can simplify your React development journey! So don't be afraid to experiment and create your own hooks - who knows, you might just discover a new hack to make your code more efficient and your React skills more wizardly!&lt;/p&gt;

&lt;p&gt;And as always&lt;br&gt;
Thanks for Reading!!!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>react</category>
    </item>
    <item>
      <title>Asynchronous JavaScript: From Callbacks to Async/Await - A Journey Through Evolution</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Mon, 01 May 2023 14:29:55 +0000</pubDate>
      <link>https://dev.to/raxraj/asynchronous-javascript-from-callbacks-to-asyncawait-a-journey-through-evolution-5578</link>
      <guid>https://dev.to/raxraj/asynchronous-javascript-from-callbacks-to-asyncawait-a-journey-through-evolution-5578</guid>
      <description>&lt;p&gt;Asynchronous programming in JavaScript can be a bit of a funky headache, especially when dealing with complex code. In the early days of JavaScript, callbacks were the groovy go-to method for handling asynchronous operations.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you prefer to learn through videos, check out this resource that offers a clear explanation of these concepts using examples &lt;a href="https://youtu.be/STA-6p0aL1k"&gt; From Callback to async/await: A Journey through Asynchronous Javascript &lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/Q8OnVOm51ws5BotlE3/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/Q8OnVOm51ws5BotlE3/giphy.gif" alt="callback" width="480" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Callbacks
&lt;/h2&gt;

&lt;p&gt;Callbacks are functions that are passed as arguments to other functions and are invoked when an asynchronous operation completes. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// simulate an asynchronous operation&lt;/span&gt;
  &lt;span class="nx"&gt;setTimeout&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;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&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="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="c1"&gt;// use the getData function with a callback&lt;/span&gt;
&lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: Hello World!&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dealing with multiple levels of nesting in callbacks can be a real headache, leading to a phenomenon known as "callback hell." It can be a real hassle when writing complex code in JavaScript. To help you understand this concept better, let me show you an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;setTimeout&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;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&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;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;setTimeout&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;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;!&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="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="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="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="c1"&gt;// use the getData function with a callback&lt;/span&gt;
&lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: Hello&lt;/span&gt;
  &lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: World&lt;/span&gt;
    &lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: !&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;&lt;a href="https://i.giphy.com/media/aSVMuNPkLN8QAfwIGD/giphy-downsized-large.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/aSVMuNPkLN8QAfwIGD/giphy-downsized-large.gif" alt="3rd callback 4th callback 5th callback" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Promises
&lt;/h2&gt;

&lt;p&gt;Thankfully, the introduction of Promises offered a more elegant solution to handling asynchronous code. Promises represent a value that may not be available yet, but will be at some point in the future. With Promises, we can chain asynchronous operations and handle errors more efficiently. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getData&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;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World!&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="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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// use the getData function with Promises&lt;/span&gt;
&lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: Hello World!&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="s2"&gt;`Error fetching data: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While Promises are a significant improvement over callbacks, they still resulted in long chains of then and catch statements, which could make the code hard to read and understand. Not so fun, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  async/await
&lt;/h2&gt;

&lt;p&gt;And then came the dynamic duo - async and await! These two made a game-changing entry, allowing developers to write asynchronous code that reads like synchronous code. It's like having a superhero sidekick! Instead of chaining Promises, await can be used to pause the function execution until the Promise resolves. This results in cleaner, more readable code that's easier to maintain and debug. How cool is that?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/data&lt;/span&gt;&lt;span class="dl"&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;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="s2"&gt;`Error fetching data: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// use the getData function with async/await&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;getData&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: fetched data&lt;/span&gt;
&lt;span class="p"&gt;})();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, async/await significantly simplifies asynchronous programming in JavaScript. No more callback hell, no more long chains of then and catch statements. Just clean, concise code that's easy to read and understand.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why upgrade to Yarn Berry?</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Thu, 27 Apr 2023 21:56:02 +0000</pubDate>
      <link>https://dev.to/raxraj/why-upgrade-to-yarn-berry-2pdk</link>
      <guid>https://dev.to/raxraj/why-upgrade-to-yarn-berry-2pdk</guid>
      <description>&lt;p&gt;Yarn is a package manager that has gained popularity in the JavaScript community due to its speed, reliability, and ease of use. It was initially released by Facebook in 2016 as an alternative to npm, the default package manager for NodeJS. From that point onwards, it just kept growing. &lt;/p&gt;

&lt;p&gt;Yarn has been a staple in the JavaScript community for years, but it just outdid itself with the release of &lt;strong&gt;&lt;u&gt;&lt;em&gt;Yarn Berry&lt;/em&gt;&lt;/u&gt;&lt;/strong&gt;. It's like Yarn on &lt;code&gt;steroids&lt;/code&gt;, and it's packing some serious punches. So, why upgrade to Yarn Berry, you ask? Let's dive in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What changed?
&lt;/h2&gt;

&lt;p&gt;Let me tell you a little story about how Yarn Berry came to be. It all started with one brave soul who decided to take on the pesky &lt;code&gt;node_modules&lt;/code&gt; directory. You know, that clunky beast that can get as heavy as a black hole in your project and suck it in if you're not careful. Say goodbye to the old days of node_modules with Yarn Berry! Thanks to its global cache, you can now enjoy faster and more efficient package installations without worrying about disk space or cluttered file systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trust me. It is fast.
&lt;/h2&gt;

&lt;p&gt;Yarn v3 is like a superhero, swooping in to save the day with its Berrylicious superfast speed! Its new architecture and Plug'n'Play system. But why trust me, when you can look at it yourself.&lt;/p&gt;

&lt;p&gt;I'm gonna install &lt;code&gt;dotenv&lt;/code&gt;, &lt;code&gt;express&lt;/code&gt;, &lt;code&gt;passport&lt;/code&gt; into my project folder for each of the package manager, and after that I'm just gonna run &lt;code&gt;yarn install&lt;/code&gt; in both of these, to see which runs faster.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fqdr6ew9ulqfu6y9314fs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fqdr6ew9ulqfu6y9314fs.png" alt="Yarn v1 took 160ms to install"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well that was fast, only 160ms. Way to go Yarn v1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F8hijpahz3gwrf0v4rz8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F8hijpahz3gwrf0v4rz8g.png" alt="Yarn berry was just 64ms"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was only 64ms, Less than half of v1.&lt;br&gt;
So, to all the doubters out there, I say this: Trust me, I know what I'm talking about. Don't let your trust issues get in the way of experiencing the magic of Yarn Berry.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is even this &lt;code&gt;Plug'n'Play&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Think of the "Plug'n'Play" system in Yarn berry like a shared pantry in a big office building. Instead of every employee bringing in their own snacks and drinks and storing them in their individual cubicles, there's a central pantry where everyone can access the same goodies. This saves space in individual cubicles, avoids duplicate snacks, and makes it easier for everyone to find what they need without having to search through their own personal stash.&lt;/p&gt;

&lt;p&gt;Similarly, with Yarn berry, instead of each project having its own "pantry" of packages in the node_modules directory, they all share the same "pantry" in the global cache. This saves space on your computer, avoids duplicate packages, and makes it faster to install the packages you need for your project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fr3udv9veqfkdsdfdlpik.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fr3udv9veqfkdsdfdlpik.png" alt="Yarn plug'n'play diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Does it really helps in minimising dependency collision?
&lt;/h2&gt;

&lt;p&gt;In Yarn v3, workspaces are an important feature that allows you to work with multiple packages as if they were a single project. This is particularly useful if you have a large codebase with many interdependent packages, or if you're working on a monorepo.&lt;/p&gt;

&lt;p&gt;With Yarn v3, you can define a workspace in your project's package.json file, which lists all the packages you want to work with. Yarn will then link all these packages together so that you can work on them as if they were a single project.&lt;/p&gt;

&lt;p&gt;One of the benefits of this approach is that you can share dependencies between packages. For example, if you have two packages that both depend on the same version of React, you can define React as a dependency in your root package.json file, and then both packages will use the same version of React.&lt;/p&gt;

&lt;p&gt;No more conflicts between package versions, as Yarn v3 keeps everything in line like a drill sergeant. Plus, you'll have less duplication in your codebase, as you only need to install each package once. It's like decluttering your closet, but for your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cache Me If You Can: Yarn v3's Groovy Package Caching System
&lt;/h2&gt;

&lt;p&gt;Yarn v3 comes with a new and improved caching system that makes package caching more efficient and effective than ever before. The caching system is smarter and more optimised. Yarn v3's caching system is always up-to-date, so you'll never feel like you're stuck in the past with outdated packages. It's like having a personal package stylist that always keeps you on trend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Yarn's Got It Together: Improved Stability!
&lt;/h2&gt;

&lt;p&gt;Yarn v3, also known as "Berry" is the result of years of experience and hard work. With this new version, the Yarn team has finally been able to fix longstanding design issues that had been plaguing the previous version, known as "Classic."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F1y4penbc7qgszhdti330.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1y4penbc7qgszhdti330.png" alt="Stable Yarn"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks to a streamlined resolution pipeline, improved data structures, and the inclusion of workspaces as core components, Yarn v3 is significantly less likely to suffer from incorrect assumptions and other design flaws. In short, Yarn v3 is a modern marvel that developers can rely on for stable and efficient package management.&lt;/p&gt;

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

&lt;p&gt;In a nutshell, upgrading to Yarn v3 Berry offers a wide range of benefits, including improved performance, workspace management, and error reporting. If these reasons don't sway you, then I'm not sure what will! So, don't be hesitant, embrace the upgrade and experience the positive impact on your project's dependencies management. Let's get groovy and upgrade to Yarn v3 Berry!&lt;/p&gt;

&lt;p&gt;And as always, Thanks for reading!!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>node</category>
    </item>
    <item>
      <title>npm v/s yarn v/s pnpm</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Mon, 24 Apr 2023 14:27:30 +0000</pubDate>
      <link>https://dev.to/raxraj/npm-vs-yarn-vs-pnpm-fo8</link>
      <guid>https://dev.to/raxraj/npm-vs-yarn-vs-pnpm-fo8</guid>
      <description>&lt;p&gt;Ready to talk package managers? You know, those tools that make your life as a developer so much easier (or sometimes, more frustrating)? Well, we're here to compare the three big players: &lt;code&gt;npm&lt;/code&gt;, &lt;code&gt;Yarn&lt;/code&gt;, and &lt;code&gt;pnpm&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's kind of like choosing your favourite type of pizza - they're all great in their own way, but which one is the best? &lt;br&gt;
Well that's exactly what we're going to do in this blog. Buckle up and grab your favourite snack (pizza or nachos, anyone?) because we're about to dive into the details of each one. Get ready for some fun and maybe even a few surprises along the way!&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Speed
&lt;/h2&gt;

&lt;p&gt;I decided to put the installation speed of npm, Yarn, and pnpm to the test. I installed Express, Passport, and dotenv using each package manager and waited impatiently for the installations to finish. I was so impressed by how fast they were that I started to suspect they might have secretly installed themselves while I wasn't looking! But in all seriousness, the results were quite interesting. Keep reading to find out which package manager reigns supreme when it comes to installation speed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Npm&lt;br&gt;
&lt;a href="https://media.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%2Fxuftpyxndn41i305nrsl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fxuftpyxndn41i305nrsl.png" alt="npm install in a folder with package.lock"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Yarn&lt;br&gt;
&lt;a href="https://media.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%2Fsphdf8gpc2gp3aa5xzsd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fsphdf8gpc2gp3aa5xzsd.png" alt="yarn install in a folder with yarn.lock"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pnpm&lt;br&gt;
&lt;a href="https://media.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%2Flvx02emtgfxjfywl0v87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flvx02emtgfxjfywl0v87.png" alt="pnpm install in a folder with pnpm-lock.yaml"&gt;&lt;/a&gt; With its handy lockfile feature, Yarn speeds up installation time significantly. Sure, npm has been making improvements to its installation speed lately, but it still can't keep up with the lightning-fast Yarn. And while pnpm is also faster than npm, it's not quite as speedy as Yarn. Now, I know what you're thinking - it's just a small difference in speed, right? But trust me, when you're dealing with a massive number of dependencies, Yarn's speed will blow your mind!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. Dependency Resolution
&lt;/h2&gt;

&lt;p&gt;Alright, time for a quick lesson in dependency resolution. Yarn, our speedy friend from before, uses a deterministic algorithm that ensures the same set of dependencies are installed on all machines. It's like a reliable GPS that never leads you astray. On the other hand, npm's flat dependency structure can sometimes lead to conflicts. It's like a bad GPS that takes you through the wrong route and leaves you stranded. And then there's pnpm, our adventurous buddy that likes to live life on the edge. It installs dependencies by sharing packages between projects, which can be faster, but can also cause conflicts. It's like a GPS that takes you on a scenic route, but occasionally leaves you lost in the wilderness. I would want to give you a good-old-long example here, but then that will make this blog &lt;code&gt;lengthy &amp;amp;&amp;amp; boring&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But Hey, I have a much simpler explanation. Let's take an example package, "PackageX" (let's call it the POWER PUFF PACKAGE). If you have version 3.0.1 of this package installed and your package.json file specifies a range of 3.x.x, then if you reinstall it using npm, it will keep the same version (no upgrade to 3.4.0, which is the latest version). However, if you use Yarn, it will always install the latest matching version, even if you delete the lockfile. This makes Yarn more consistent in its behaviour, ensuring that you get the latest version of the package every time.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Offline Mod (OG)
&lt;/h2&gt;

&lt;p&gt;Yarn and pnpm are two package managers that come with a feature that many developers find handy: offline mode. With Yarn or pnpm, you can install packages even if you don't have an internet connection, as long as you have already downloaded the necessary files. This feature can be especially useful if you are working on a project while traveling or in an area with spotty internet connection. However, if you are using npm, you won't have the luxury of an offline mode. That means you'll need a stable internet connection in order to install packages, which can be a bit of a bummer for those who are often on the go.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. Security
&lt;/h2&gt;

&lt;p&gt;Yarn's security features are nothing to be knitted about! With its use of checksums to ensure package integrity and a built-in command to audit packages for vulnerabilities, Yarn is a real yarn-spinner when it comes to security. Meanwhile, npm and pnpm's security features are similar but not as tightly knit as Yarn's.&lt;/p&gt;

&lt;p&gt;Yarn's checksum feature ensures that the installed package matches the package distributed on the registry. This prevents malicious actors from tampering with the package during transit. To verify a package's checksum, you can run the following command in Yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn check &lt;span class="nt"&gt;--integrity&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to checksums, Yarn also has a command for auditing packages for security vulnerabilities. This command checks for known security vulnerabilities in all installed packages and their dependencies. It also suggests steps for resolving any vulnerabilities found.&lt;/p&gt;

&lt;p&gt;npm also has a similar npm audit command, but it's not as advanced as Yarn's audit feature. pnpm, on the other hand, relies on npm's security features and doesn't have any additional security measures like checksums.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Custom Registries
&lt;/h2&gt;

&lt;p&gt;Listen, if you're anything like me, you don't want to be stuck with just the default registries. Thankfully, all three package managers offer some level of support for custom registries. But let's be real: Yarn has the VIP treatment when it comes to private registries. And with its selective version resolution feature, you can cherry-pick the best dependencies from your favorite registry while still keeping things cozy with the default one. Sorry npm and pnpm, but Yarn's got the hookup.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Check out the &lt;a href="https://dev.to/raxraj/why-upgrade-to-yarn-berry-2pdk"&gt;Why upgrade to Yarn Berry?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fdb8cbte4cle872tya4ao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fdb8cbte4cle872tya4ao.png" alt="Ranking Yarn tops, pnpm follows, npm lags"&gt;&lt;/a&gt;Now, I know what you're thinking. 'Oh great, here comes the biased opinion.' But hey, it is what it is. After analyzing all the features and functionality of Yarn, npm, and pnpm, I gotta say, Yarn takes the cake, baby! It's faster than a cheetah on caffeine, has security features that could make a CIA agent jealous, and can handle custom registries like a boss. Don't get me wrong, npm and pnpm are cool and all, but they're just not on the same level as Yarn. So, if you want to be the coolest kid on the block (or in the dev world), use Yarn for all your JavaScript projects. Trust me, your code will thank you.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>npm</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to get rid of mental Block as a web developer?</title>
      <dc:creator>Ashutosh Kumar</dc:creator>
      <pubDate>Sun, 09 Apr 2023 15:41:22 +0000</pubDate>
      <link>https://dev.to/raxraj/how-to-get-rid-of-mental-block-11dn</link>
      <guid>https://dev.to/raxraj/how-to-get-rid-of-mental-block-11dn</guid>
      <description>&lt;p&gt;Are you a web developer struggling with a pesky mental block that's getting in the way of your creativity and productivity? Don't worry, we've all been there! In this article by me (a developer who probably gets the highest number of mental blocks per week), we'll discuss some fun and effective ways to shake off that mental block and get back into the groove of coding. So, grab a cup of coffee and let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Take a break &amp;amp; ....
&lt;/h2&gt;

&lt;p&gt;Sometimes, the best way to overcome a mental block is to take a break. It might seem counterintuitive to step away from your work when you're feeling stuck, but often a change of scenery or a few minutes of downtime can do wonders for your creativity. As a web developer, I've encountered my fair share of mental blocks, especially when working with tricky APIs like the Meta Graph API. Let's just say that the developers behind Meta Graph API have received some less-than-flattering feedback from me. But when I find myself stuck and frustrated, I've discovered a simple and effective solution: &lt;strong&gt;&lt;em&gt;Eminem&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4QJN9Sg1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oh8qtykp6h4r3sf3ount.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4QJN9Sg1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oh8qtykp6h4r3sf3ount.jpg" alt="Image description" width="800" height="594"&gt;&lt;/a&gt;&lt;br&gt;
So, take a break! Step away from your computer, go outside for some &lt;em&gt;fresh air&lt;/em&gt;, or do something completely unrelated to coding. Taking a break can give your mind a chance to reset and come back with a fresh perspective.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Connect with your peers
&lt;/h2&gt;

&lt;p&gt;Collaboration and seeking help are essential ways to overcome a mental block. As a web developer, you are part of a vast and supportive community that can help you solve coding mental blocks. You can collaborate with other developers, seek help through online communities, or engage in coding forums. Such collaboration not only helps you solve coding problems but also offers learning opportunities and builds new connections. Don't hesitate to ask for help the next time you feel stuck. &lt;strong&gt;&lt;em&gt;The developer community is always willing to lend a hand.&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ARTNTEUD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhlg2bm7kpgcpsrxfkqj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ARTNTEUD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhlg2bm7kpgcpsrxfkqj.jpg" alt="Person with his peers helping him in a problem" width="800" height="530"&gt;&lt;/a&gt;&lt;br&gt;
Personally, I've found that asking for help from my team is the most effective way to overcome a mental block as a web developer. Simply inviting a colleague to join me at my desk and help me work through the problem can make all the difference. However, collaboration doesn't have to be limited to your immediate team. You can also seek help from the larger developer community through online forums, Stack-overflow, Slack channels, and social media groups. By reaching out and asking for help, you can tap into a wealth of knowledge and expertise to help you get unstuck and back on track.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Breaking the problems into smaller sub-problems
&lt;/h2&gt;

&lt;p&gt;As a web developer, it's common to face &lt;code&gt;complex coding problems&lt;/code&gt; that can seem overwhelming. When you're feeling stuck, one effective way to overcome mental blocks is to break the problem down into smaller parts.&lt;/p&gt;

&lt;p&gt;Start by identifying the smallest possible task you can complete that will help move you closer to solving the larger problem. Once you've completed that task, move on to the next smallest task, and so on, until you've solved the problem as a whole.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8USsf-8r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mip3f41zd77ihy8fpw16.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8USsf-8r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mip3f41zd77ihy8fpw16.jpg" alt="How to break down to smaller problems" width="800" height="394"&gt;&lt;/a&gt;&lt;br&gt;
Breaking the problem into smaller parts can help you feel less overwhelmed and more in control. Plus, each completed task can give you a sense of accomplishment and momentum, which can help motivate you to keep going.&lt;/p&gt;

&lt;p&gt;So, the next time you're facing a complex coding problem, try breaking it down into smaller parts. &lt;strong&gt;&lt;em&gt;By tackling the problem one step at a time, you can overcome mental blocks and solve even the most challenging coding problems.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Try out a different approach
&lt;/h2&gt;

&lt;p&gt;Think of it as a game of chess. If you keep making the same moves, your opponent will see through your strategy and you'll be left in a stalemate. But, if you switch up your game plan, you might just catch your opponent off guard and score a win.&lt;/p&gt;

&lt;p&gt;The same goes for coding problems. If you've been banging your head against the wall trying to solve a front-end issue, try switching gears and focusing on the back-end.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Take a walk in NATURE
&lt;/h2&gt;

&lt;p&gt;Sometimes, a quick stroll outside in the beauty of nature can be just what you need to clear your mind and find inspiration. Research has shown that spending time in nature can help reduce stress levels, boost creativity, and improve cognitive function. So, instead of banging your head against your desk, consider taking a break and going for a walk in the park or a nearby nature trail. You may find that a change of scenery is exactly what you need to overcome your mental block and find the solution to your coding problem.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xq2xcBdQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsz1b7nn4k3swm6l7jy2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xq2xcBdQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsz1b7nn4k3swm6l7jy2.jpg" alt="Happy programmer walking in nature" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;As we conclude, it's essential to understand that there's no one-size-fits-all solution when it comes to overcoming mental blocks in web development. Each developer has their unique approach that helps them tackle these blocks and bring out their creative side.&lt;/p&gt;

&lt;p&gt;From taking a break to jamming out to Eminem or collaborating with team members, there are various methods that developers use to overcome mental blocks. Some even take the unconventional route by taking a walk in nature, seeking inspiration from art, or meditating.&lt;/p&gt;

&lt;p&gt;The key is to be patient, persistent, and open to new ideas. Don't hesitate to try out different methods until you find the one that resonates with you. Remember, mental blocks are a natural part of the creative process, and even the most successful developers face them from time to time.&lt;/p&gt;

&lt;p&gt;So, embrace your unique approach and keep pushing forward until you find the perfect solution that works for you.&lt;br&gt;
And as always, Happy Coding!&lt;/p&gt;

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