<?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: Debug Diaries</title>
    <description>The latest articles on DEV Community by Debug Diaries (@debugdiariesbyswethap).</description>
    <link>https://dev.to/debugdiariesbyswethap</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%2F1147501%2Fd6c2a112-0c9d-4e43-b32c-401326950367.png</url>
      <title>DEV Community: Debug Diaries</title>
      <link>https://dev.to/debugdiariesbyswethap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/debugdiariesbyswethap"/>
    <language>en</language>
    <item>
      <title>How ChatGPT Works (Simple Explanation for Beginners)</title>
      <dc:creator>Debug Diaries</dc:creator>
      <pubDate>Mon, 20 Apr 2026 09:39:29 +0000</pubDate>
      <link>https://dev.to/debugdiariesbyswethap/how-chatgpt-works-simple-explanation-for-beginners-51ok</link>
      <guid>https://dev.to/debugdiariesbyswethap/how-chatgpt-works-simple-explanation-for-beginners-51ok</guid>
      <description>&lt;p&gt;If you’ve ever wondered what happens when you type a prompt into ChatGPT, this article breaks it down in the simplest way possible.&lt;/p&gt;

&lt;p&gt;Let’s go step by step&lt;/p&gt;




&lt;h2&gt;
  
  
  High-Level Flow
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Input → Input Processing → Context Building → LLM → Output Processing → Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  1. Input Processing
&lt;/h2&gt;

&lt;p&gt;When you enter a prompt:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your input is cleaned and structured&lt;/li&gt;
&lt;li&gt;Previous chat history is added&lt;/li&gt;
&lt;li&gt;Text is converted into &lt;strong&gt;tokens&lt;/strong&gt; (numbers the model understands)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tokens are not always full words—they can be parts of words, spaces, or punctuation.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Context Building (Prompt Augmentation)
&lt;/h2&gt;

&lt;p&gt;Before sending your input to the model, the system adds extra information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hidden &lt;strong&gt;system prompts&lt;/strong&gt; (rules like “be helpful and safe”)&lt;/li&gt;
&lt;li&gt;App-level instructions (tone, format)&lt;/li&gt;
&lt;li&gt;Sometimes external data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This helps guide how the AI should respond.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. LLM Processing (The Brain)
&lt;/h2&gt;

&lt;p&gt;The request is sent to a Large Language Model (LLM).&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Reads the full context (input + history + system instructions)&lt;/li&gt;
&lt;li&gt;Generates a response &lt;strong&gt;token by token&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Uses probability to predict the next word&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important: It doesn’t “think” like a human—it predicts patterns.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Output Processing
&lt;/h2&gt;

&lt;p&gt;Before showing the result:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Safety filters are applied&lt;/li&gt;
&lt;li&gt;Formatting is adjusted (like Markdown)&lt;/li&gt;
&lt;li&gt;The response is finalized&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Flow (Simplified Diagram)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[You]
   ↓
[Input Processing]
   ↓
[Context + Hidden Instructions]
   ↓
[LLM (Prediction Engine)]
   ↓
[Output Filtering &amp;amp; Formatting]
   ↓
[Final Response]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Important Note
&lt;/h2&gt;

&lt;p&gt;Real-world systems like ChatGPT can also include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tool usage (APIs, calculators)&lt;/li&gt;
&lt;li&gt;Retrieval systems (fetching external knowledge)&lt;/li&gt;
&lt;li&gt;Memory layers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a simplified mental model to get started.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Matters
&lt;/h2&gt;

&lt;p&gt;Understanding this helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write better prompts&lt;/li&gt;
&lt;li&gt;Debug AI responses&lt;/li&gt;
&lt;li&gt;Build your own AI applications&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What’s Next?
&lt;/h2&gt;

&lt;p&gt;In the next post, I’ll explain how these models are actually created—from tokens to training to alignment.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>chatgpt</category>
      <category>llm</category>
    </item>
    <item>
      <title>How a Browser Renders a Webpage — And Where React, CSR, and SSR Fit In</title>
      <dc:creator>Debug Diaries</dc:creator>
      <pubDate>Sun, 15 Mar 2026 14:02:36 +0000</pubDate>
      <link>https://dev.to/debugdiariesbyswethap/how-a-browser-renders-a-webpage-and-where-react-csr-and-ssr-fit-in-16f4</link>
      <guid>https://dev.to/debugdiariesbyswethap/how-a-browser-renders-a-webpage-and-where-react-csr-and-ssr-fit-in-16f4</guid>
      <description>&lt;p&gt;When we type a URL in the browser and press &lt;strong&gt;Enter&lt;/strong&gt;, a lot happens behind the scenes before the webpage appears on the screen. Understanding this process is important for frontend developers because it explains &lt;strong&gt;why some websites load instantly while others show a blank screen first&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we’ll walk through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How a browser processes a request&lt;/li&gt;
&lt;li&gt;How the UI is rendered&lt;/li&gt;
&lt;li&gt;Where &lt;strong&gt;React&lt;/strong&gt;, &lt;strong&gt;Client-Side Rendering (CSR)&lt;/strong&gt;, and &lt;strong&gt;Server-Side Rendering (SSR)&lt;/strong&gt; come into play&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  1. The Request Process
&lt;/h2&gt;

&lt;p&gt;When a user enters a URL like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the browser performs several steps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Check Cache
&lt;/h3&gt;

&lt;p&gt;The browser first checks whether the website resources are already stored locally in cache.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: DNS Lookup
&lt;/h3&gt;

&lt;p&gt;If not found, the browser asks the &lt;strong&gt;Domain Name System&lt;/strong&gt; to convert the domain name into an IP address.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;example.com → 93.184.216.34
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Establish Connection
&lt;/h3&gt;

&lt;p&gt;The browser opens a connection with the server using &lt;strong&gt;Transmission Control Protocol&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If the website uses HTTPS, a secure connection is established using &lt;strong&gt;Transport Layer Security&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Send HTTP Request
&lt;/h3&gt;

&lt;p&gt;The browser sends a request like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nf"&gt;GET&lt;/span&gt; &lt;span class="nn"&gt;/index.html&lt;/span&gt; &lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;
&lt;span class="na"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;example.com&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. Server Response
&lt;/h2&gt;

&lt;p&gt;The server sends back resources such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML&lt;/li&gt;
&lt;li&gt;CSS&lt;/li&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;li&gt;Images&lt;/li&gt;
&lt;li&gt;Fonts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These resources are often &lt;strong&gt;sent in chunks (streams)&lt;/strong&gt;, meaning the browser starts processing them &lt;strong&gt;before the entire file finishes downloading&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. HTML Parsing and DOM Creation
&lt;/h2&gt;

&lt;p&gt;Once the browser receives HTML, it begins parsing it immediately.&lt;/p&gt;

&lt;p&gt;The browser converts HTML elements into a tree structure called the &lt;strong&gt;Document Object Model&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Products&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Item 1&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;DOM structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body
 ├── h1
 └── p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This structure allows JavaScript to access and modify elements dynamically.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. CSS Parsing and CSSOM
&lt;/h2&gt;

&lt;p&gt;When the browser encounters CSS files or &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tags, it creates another structure called the &lt;strong&gt;CSS Object Model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This maps selectors to their styles.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&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 browser builds a tree of style rules and applies them to the DOM elements.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Render Tree Creation
&lt;/h2&gt;

&lt;p&gt;The browser combines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DOM&lt;/li&gt;
&lt;li&gt;CSSOM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;to create a &lt;strong&gt;Render Tree&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This tree contains only &lt;strong&gt;visible elements&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Elements excluded include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;meta&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;elements with &lt;code&gt;display: none&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  6. Layout (Reflow)
&lt;/h2&gt;

&lt;p&gt;Next, the browser calculates the &lt;strong&gt;position and size&lt;/strong&gt; of each element.&lt;/p&gt;

&lt;p&gt;It determines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;width and height&lt;/li&gt;
&lt;li&gt;position on the X and Y axis&lt;/li&gt;
&lt;li&gt;how elements flow on the page&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This step ensures the UI fits the &lt;strong&gt;device viewport&lt;/strong&gt; correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Paint
&lt;/h2&gt;

&lt;p&gt;Once layout is calculated, the browser fills visual details such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;colors&lt;/li&gt;
&lt;li&gt;borders&lt;/li&gt;
&lt;li&gt;text&lt;/li&gt;
&lt;li&gt;shadows&lt;/li&gt;
&lt;li&gt;images&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this stage the page becomes visually complete.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Compositing
&lt;/h2&gt;

&lt;p&gt;Some elements are rendered on separate layers, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sticky headers&lt;/li&gt;
&lt;li&gt;popups&lt;/li&gt;
&lt;li&gt;animations&lt;/li&gt;
&lt;li&gt;transformed elements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These layers are sent to the &lt;strong&gt;GPU&lt;/strong&gt; to render efficiently.&lt;/p&gt;

&lt;p&gt;This final step is called &lt;strong&gt;compositing&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Where JavaScript Fits In
&lt;/h2&gt;

&lt;p&gt;When the browser encounters a script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"app.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;HTML parsing &lt;strong&gt;pauses&lt;/strong&gt;, and the browser:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;downloads the JavaScript&lt;/li&gt;
&lt;li&gt;executes it&lt;/li&gt;
&lt;li&gt;resumes HTML parsing&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is why JavaScript can &lt;strong&gt;block page rendering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To avoid blocking, developers use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"app.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"analytics.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; executes immediately after download&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;defer&lt;/code&gt; executes after HTML parsing finishes&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  10. Client-Side Rendering (CSR)
&lt;/h2&gt;

&lt;p&gt;Many modern web apps built with &lt;strong&gt;React&lt;/strong&gt; use &lt;strong&gt;Client-Side Rendering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In CSR, the server sends &lt;strong&gt;minimal HTML&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"main.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser initially sees:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;empty page
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then JavaScript runs and React builds the UI.&lt;/p&gt;

&lt;p&gt;Example React 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="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&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;p&gt;React generates the DOM elements dynamically.&lt;/p&gt;

&lt;h3&gt;
  
  
  User Experience
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;page loads
↓
blank screen
↓
JavaScript loads
↓
UI appears
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  11. Server-Side Rendering (SSR)
&lt;/h2&gt;

&lt;p&gt;Frameworks like &lt;strong&gt;Next.js&lt;/strong&gt; use &lt;strong&gt;Server-Side Rendering&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of sending empty HTML, the server renders the React components and sends fully built HTML.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Products&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Item 1&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the browser displays content &lt;strong&gt;immediately&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  12. Hydration
&lt;/h2&gt;

&lt;p&gt;After SSR HTML loads, JavaScript still downloads.&lt;/p&gt;

&lt;p&gt;React then attaches event handlers to the existing HTML.&lt;/p&gt;

&lt;p&gt;This process is called &lt;strong&gt;Hydration&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Before hydration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button visible but not clickable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After hydration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button becomes interactive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  CSR vs SSR Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;CSR&lt;/th&gt;
&lt;th&gt;SSR&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Initial HTML&lt;/td&gt;
&lt;td&gt;Empty&lt;/td&gt;
&lt;td&gt;Fully rendered&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;First load&lt;/td&gt;
&lt;td&gt;Blank screen&lt;/td&gt;
&lt;td&gt;Content appears instantly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SEO&lt;/td&gt;
&lt;td&gt;Limited&lt;/td&gt;
&lt;td&gt;Better&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rendering location&lt;/td&gt;
&lt;td&gt;Browser&lt;/td&gt;
&lt;td&gt;Server&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;Understanding how the browser renders a webpage helps frontend developers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;optimize performance&lt;/li&gt;
&lt;li&gt;avoid render-blocking issues&lt;/li&gt;
&lt;li&gt;improve user experience&lt;/li&gt;
&lt;li&gt;choose the right rendering strategy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CSR&lt;/strong&gt; is great for highly interactive applications, while &lt;strong&gt;SSR&lt;/strong&gt; improves initial load performance and SEO.&lt;/p&gt;

&lt;p&gt;Modern frameworks like &lt;strong&gt;Next.js&lt;/strong&gt; combine both approaches to deliver the best user experience.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>JavaScript Explained: Set, WeakSet, Map &amp; WeakMap – What They Are and When to Use Them</title>
      <dc:creator>Debug Diaries</dc:creator>
      <pubDate>Tue, 17 Jun 2025 14:01:20 +0000</pubDate>
      <link>https://dev.to/debugdiariesbyswethap/javascript-explained-set-weakset-map-weakmap-what-they-are-and-when-to-use-them-4jp5</link>
      <guid>https://dev.to/debugdiariesbyswethap/javascript-explained-set-weakset-map-weakmap-what-they-are-and-when-to-use-them-4jp5</guid>
      <description>&lt;p&gt;JavaScript isn’t just about arrays and objects anymore. As applications grow complex, we need smarter ways to store and manage data. That's where &lt;strong&gt;Set, WeakSet, Map,&lt;/strong&gt; and &lt;strong&gt;WeakMap&lt;/strong&gt; come in.&lt;/p&gt;

&lt;p&gt;These built-in data structures provide more powerful and flexible options for working with collections. Let’s dive deep into each one, understand how they work, and when to use them in real-world scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set – A Collection of Unique Values
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;
A Set is a special type of collection in JavaScript that lets you store unique values — no duplicates allowed.&lt;br&gt;
You can think of a Set like a checklist where each item can appear only once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stores unique values only.&lt;/li&gt;
&lt;li&gt;Values can be of &lt;strong&gt;any data type&lt;/strong&gt; – numbers, strings, objects, etc.&lt;/li&gt;
&lt;li&gt;Maintains the &lt;strong&gt;insertion order&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Provides utility methods like &lt;code&gt;.add()&lt;/code&gt;, &lt;code&gt;.has()&lt;/code&gt;, &lt;code&gt;.delete()&lt;/code&gt;, and &lt;code&gt;.clear()&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fruits = new Set();
fruits.add('apple');
fruits.add('banana');
fruits.add('apple'); // Duplicate, won’t be added again

console.log(fruits); // Set(2) { 'apple', 'banana' }
console.log(fruits.has('banana')); // true

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

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;When to Use:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need a list of unique items (e.g., tags, filters).&lt;/li&gt;
&lt;li&gt;You want to easily check for the presence of a value.&lt;/li&gt;
&lt;li&gt;You want to remove duplicates from an array:&lt;/li&gt;
&lt;/ul&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 plaintext"&gt;&lt;code&gt;const arr = [1, 2, 2, 3];
const uniqueArr = [...new Set(arr)];
console.log(uniqueArr); // [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  WeakSet – Sets with Weak References
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;WeakSet&lt;/code&gt; is similar to a &lt;code&gt;Set&lt;/code&gt;, but it only stores objects and holds them "weakly," meaning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The object references don’t prevent garbage collection.&lt;/li&gt;
&lt;li&gt;If no other references to the object exist, it will be automatically removed from the WeakSet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only objects (not primitives) can be stored.&lt;/li&gt;
&lt;li&gt;Items are held weakly and may be garbage collected.&lt;/li&gt;
&lt;li&gt;Not iterable – you can't loop through a WeakSet.&lt;/li&gt;
&lt;li&gt;Useful for memory-efficient temporary storage.&lt;/li&gt;
&lt;/ul&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 plaintext"&gt;&lt;code&gt;const person = { name: 'Alice' };
const visitedUsers = new WeakSet();

visitedUsers.add(person);
console.log(visitedUsers.has(person)); // true

// Later...
// person = null; // If this happens, the object may be removed from WeakSet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to Use:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracking objects without creating memory leaks, like keeping track of DOM elements or users currently logged in.&lt;/li&gt;
&lt;li&gt;You don’t need to iterate the collection.&lt;/li&gt;
&lt;li&gt;You want automatic cleanup when the object is no longer used elsewhere.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Map – Key-Value Pairs with Powerful Features
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;
A &lt;code&gt;Map&lt;/code&gt; is a collection of key-value pairs where &lt;strong&gt;keys can be any data type&lt;/strong&gt; – unlike regular objects which only allow strings or symbols.&lt;/p&gt;

&lt;p&gt;It’s like an enhanced &lt;strong&gt;Object&lt;/strong&gt; but with predictable ordering and better performance for frequent additions/removals.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Any value (object or primitive) can be used as a key.&lt;/li&gt;
&lt;li&gt;Maintains insertion order of key-value pairs.&lt;/li&gt;
&lt;li&gt;Provides easy methods: &lt;code&gt;.set()&lt;/code&gt;, &lt;code&gt;.get()&lt;/code&gt;, &lt;code&gt;.has()&lt;/code&gt;, &lt;code&gt;.delete()&lt;/code&gt;, &lt;code&gt;.clear()&lt;/code&gt;, and &lt;code&gt;.size&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&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 plaintext"&gt;&lt;code&gt;const userMap = new Map();
const user1 = { id: 1 };
userMap.set('name', 'John');
userMap.set(user1, 'Premium Member');

console.log(userMap.get('name')); // John
console.log(userMap.get(user1));  // Premium Member
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to Use:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need keys that are not just strings.&lt;/li&gt;
&lt;li&gt;You require frequent key-value pair manipulation.&lt;/li&gt;
&lt;li&gt;You need guaranteed order of entries.&lt;/li&gt;
&lt;li&gt;Ideal for building dictionaries, caching data, or implementing LRU caches.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  WeakMap – Object Keys with Weak References
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is it?&lt;/strong&gt;&lt;br&gt;
A &lt;code&gt;WeakMap&lt;/code&gt; is just like a Map, but with some constraints:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Only objects can be used as keys.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Those keys are held &lt;strong&gt;weakly&lt;/strong&gt;, allowing them to be garbage collected.&lt;/li&gt;
&lt;li&gt;It is &lt;strong&gt;not iterable&lt;/strong&gt; and has limited methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Features:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only object keys (no strings, numbers).&lt;/li&gt;
&lt;li&gt;Automatically cleans up when the key object is no longer referenced.&lt;/li&gt;
&lt;li&gt;Supports only &lt;code&gt;.set()&lt;/code&gt;, &lt;code&gt;.get()&lt;/code&gt;, &lt;code&gt;.has()&lt;/code&gt;, and &lt;code&gt;.delete()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&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 plaintext"&gt;&lt;code&gt;const secretData = new WeakMap();
let user = { name: 'Tom' };

secretData.set(user, { accessLevel: 'admin' });
console.log(secretData.get(user)); // { accessLevel: 'admin' }

user = null; // Now, the user object and its secret data can be garbage collected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to Use:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Storing &lt;strong&gt;private data&lt;/strong&gt; related to DOM elements or users.&lt;/li&gt;
&lt;li&gt;You want &lt;strong&gt;memory-efficient&lt;/strong&gt; key-value storage.&lt;/li&gt;
&lt;li&gt;You don’t need to enumerate keys.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;While JavaScript's built-in objects like arrays and plain objects are great, &lt;strong&gt;Set, WeakSet, Map,&lt;/strong&gt; and &lt;strong&gt;WeakMap&lt;/strong&gt; give you more power and flexibility when managing data — especially as your applications grow in complexity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Set&lt;/strong&gt; and &lt;strong&gt;Map&lt;/strong&gt; when you need iterable collections with full control.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;WeakSet&lt;/strong&gt; and &lt;strong&gt;WeakMap&lt;/strong&gt; for &lt;strong&gt;memory-sensitive&lt;/strong&gt; or &lt;strong&gt;private&lt;/strong&gt; data management.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mastering these will help you write cleaner, more efficient, and scalable JavaScript code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>react</category>
    </item>
    <item>
      <title>Understanding SOLID Principles in Frontend Development (with React Examples)</title>
      <dc:creator>Debug Diaries</dc:creator>
      <pubDate>Wed, 21 May 2025 09:58:36 +0000</pubDate>
      <link>https://dev.to/debugdiariesbyswethap/understanding-solid-principles-in-frontend-development-with-react-examples-7ao</link>
      <guid>https://dev.to/debugdiariesbyswethap/understanding-solid-principles-in-frontend-development-with-react-examples-7ao</guid>
      <description>&lt;p&gt;As frontend applications grow more complex, maintaining clean, scalable code becomes challenging. That’s where &lt;strong&gt;SOLID principles&lt;/strong&gt; come in — a set of five design principles aimed at building better software. Though originally coined for object-oriented programming, they apply well to &lt;strong&gt;frontend development&lt;/strong&gt;, especially when working with &lt;strong&gt;React.js&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this blog, let’s break down each principle in simple terms with relatable React examples.&lt;/p&gt;

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

&lt;p&gt;SOLID is an acronym:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;S – Single Responsibility Principle&lt;/li&gt;
&lt;li&gt;O – Open/Closed Principle&lt;/li&gt;
&lt;li&gt;L – Liskov Substitution Principle&lt;/li&gt;
&lt;li&gt;I – Interface Segregation Principle&lt;/li&gt;
&lt;li&gt;D – Dependency Inversion Principle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s explore each one.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Single Responsibility Principle (SRP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A component should do one thing only.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// UI Component
function UserCard({ user }) {
  return &amp;lt;div&amp;gt;{user.name}&amp;lt;/div&amp;gt;;
}

// Data Fetching Hook
function useUserData(userId) {
  const [user, setUser] = useState(null);
  useEffect(() =&amp;gt; {
    fetch(`/api/users/${userId}`).then(res =&amp;gt; res.json()).then(setUser);
  }, [userId]);
  return user;
}

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

&lt;/div&gt;



&lt;p&gt;Each function does one job — one for UI, one for data. This separation makes the code easier to test, debug, and reuse.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Open/Closed Principle (OCP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Components should be open for extension, but closed for modification.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Button({ variant = 'primary', children }) {
  return &amp;lt;button className={`btn ${variant}`}&amp;gt;{children}&amp;lt;/button&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;You can add new button styles (&lt;code&gt;secondary&lt;/code&gt;, &lt;code&gt;danger&lt;/code&gt;, etc.) without modifying the Button component. This prevents bugs and keeps components flexible.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Liskov Substitution Principle (LSP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A child component should work in place of its parent without breaking the app.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Button({ onClick, children }) {
  return &amp;lt;button onClick={onClick}&amp;gt;{children}&amp;lt;/button&amp;gt;;
}

function DangerButton(props) {
  return &amp;lt;Button {...props} className="danger" /&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;DangerButton&lt;/code&gt; behaves just like &lt;code&gt;Button&lt;/code&gt;. If you replace &lt;code&gt;Button&lt;/code&gt; with &lt;code&gt;DangerButton&lt;/code&gt;, your app will still work — fulfilling the same expectations.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Interface Segregation Principle (ISP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Components should only receive the props they actually use.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function UserSummary({ name, email }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;{name}&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;{email}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Passing only required props keeps components simple and reduces the chance of misuse or confusion when reusing them.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Dependency Inversion Principle (DIP)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Depend on abstractions, not concrete implementations.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Login({ authService }) {
  const handleLogin = () =&amp;gt; authService.login();
  return &amp;lt;button onClick={handleLogin}&amp;gt;Login&amp;lt;/button&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;Instead of using a specific API method directly, &lt;code&gt;Login&lt;/code&gt; depends on an &lt;code&gt;authService&lt;/code&gt;. This allows easy mocking for tests and swapping out logic (e.g., for Google login or a mock server).&lt;/p&gt;

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

&lt;p&gt;By applying SOLID principles, you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce bugs&lt;/li&gt;
&lt;li&gt;Improve code reusability&lt;/li&gt;
&lt;li&gt;Make your frontend easier to test and extend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even if you're building a small app, following these principles leads to cleaner, more professional code. Start with &lt;strong&gt;Single Responsibility&lt;/strong&gt;, and gradually adopt the rest as your projects grow.&lt;/p&gt;

</description>
      <category>solidprinciples</category>
      <category>react</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Creating Custom Constraints: Writing Your Own @NotNull Annotation</title>
      <dc:creator>Debug Diaries</dc:creator>
      <pubDate>Mon, 28 Apr 2025 05:43:09 +0000</pubDate>
      <link>https://dev.to/debugdiariesbyswethap/creating-custom-constraints-writing-your-own-notnull-annotation-3hda</link>
      <guid>https://dev.to/debugdiariesbyswethap/creating-custom-constraints-writing-your-own-notnull-annotation-3hda</guid>
      <description>&lt;p&gt;When building Java applications, especially with frameworks like Spring or Jakarta EE, validation plays a critical role. You might be familiar with annotations like &lt;code&gt;@NotNull&lt;/code&gt;, &lt;code&gt;@Size&lt;/code&gt;, or &lt;code&gt;@Email&lt;/code&gt;. But what if you need something more custom? Maybe you want a special kind of "NotNull" check — one that's tailored for your domain or offers better error messages.&lt;/p&gt;

&lt;p&gt;In this post, we’ll walk through creating a custom constraint annotation, similar to &lt;code&gt;@NotNull&lt;/code&gt;.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Create a Custom Constraint?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Specific validation logic&lt;/strong&gt;: Maybe you want to check not only for null but also for empty strings, or to validate conditionally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom error messages&lt;/strong&gt;: Tailor the messages for your application’s tone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusable across the codebase&lt;/strong&gt;: Keep code clean and consistent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain-specific validations&lt;/strong&gt;: "Name must not be blank", "User ID must not be empty", etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Step 1: Create the Custom Annotation
&lt;/h2&gt;

&lt;p&gt;Let’s create a simple @ConditionalNotNull annotation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Documented
@Constraint(validatedBy = ConditionalNotNullValidator.class)
@Target({ METHOD, FIELD, ANNOTATION_TYPE, PARAMETER })
@Retention(RUNTIME)
public @interface ConditionalNotNull {

    String message() default "This field cannot be null";

    Class&amp;lt;?&amp;gt;[] groups() default {};

    Class&amp;lt;? extends Payload&amp;gt;[] payload() default {};
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@Constraint&lt;/code&gt; points to the Validator class that will contain the actual validation logic.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Target&lt;/code&gt; specifies where the annotation can be used (fields, parameters, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Retention(RUNTIME)&lt;/code&gt; means the annotation is available at runtime for validation frameworks.

&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 2: Implement the Validator
&lt;/h2&gt;

&lt;p&gt;Now, create a validator class &lt;code&gt;ConditionalNotNullValidator&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class ConditionalNotNullValidator implements ConstraintValidator&amp;lt;ConditionalNotNull, Object&amp;gt; {

    @Override
    public void initialize(ConditionalNotNull constraintAnnotation) {
        // You can access annotation attributes here if needed
    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        return value != null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;isValid&lt;/code&gt; method returns &lt;code&gt;true&lt;/code&gt; if the value passes validation.&lt;/li&gt;
&lt;li&gt;Here, we simply check if the value is not &lt;code&gt;null&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 3: Use the Custom Annotation
&lt;/h2&gt;

&lt;p&gt;Now you can use @ConditionalNotNull just like &lt;code&gt;@NotNull&lt;/code&gt;!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class UserDTO {

    @ConditionalNotNull(message = "Username must not be null!")
    private String username;

    @ConditionalNotNull
    private String email;

    // getters and setters
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you validate UserDTO, the &lt;code&gt;@CondtionalNotNull&lt;/code&gt; constraints will automatically kick in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Extend It Further
&lt;/h2&gt;

&lt;p&gt;You can enhance &lt;code&gt;@ConditionalNotNull&lt;/code&gt; to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check for &lt;strong&gt;non-empty&lt;/strong&gt; strings or collections&lt;/li&gt;
&lt;li&gt;Add &lt;strong&gt;conditional validatio&lt;/strong&gt;n (validate only if another field has a certain value)&lt;/li&gt;
&lt;li&gt;Support &lt;strong&gt;customized error codes&lt;/strong&gt; for better API responses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example to check non-empty Strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
    if (value == null) {
        return false;
    }
    if (value instanceof String) {
        return !((String) value).trim().isEmpty();
    }
    return true;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;@ConditionalNotNull&lt;/code&gt; not only checks for null but also ensures strings aren't just blank spaces!&lt;/p&gt;

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

&lt;p&gt;Creating a custom constraint like &lt;code&gt;@NotNull&lt;/code&gt; is straightforward but very powerful. It keeps your code cleaner, validations reusable, and applications more maintainable.&lt;/p&gt;

&lt;p&gt;By combining annotations and validator classes, you can create any kind of validation logic tailored specifically to your project’s needs.&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>springboot</category>
      <category>jakarta</category>
    </item>
    <item>
      <title>Why Promise.all() Isn’t Always the Best Choice for Independent API Calls</title>
      <dc:creator>Debug Diaries</dc:creator>
      <pubDate>Tue, 22 Apr 2025 14:28:13 +0000</pubDate>
      <link>https://dev.to/debugdiariesbyswethap/why-promiseall-isnt-always-the-best-choice-for-independent-api-calls-kdf</link>
      <guid>https://dev.to/debugdiariesbyswethap/why-promiseall-isnt-always-the-best-choice-for-independent-api-calls-kdf</guid>
      <description>&lt;p&gt;Async/await is powerful — but it’s easy to assume it magically makes your code fast and reliable. I’ve seen this first-hand in real-world scenarios.&lt;/p&gt;

&lt;p&gt;One common issue when working with multiple API calls is &lt;strong&gt;sequential execution&lt;/strong&gt;, which can lead to unnecessarily long load times. For example, consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const response1 = await fetchA();
const response2 = await fetchB();
const response3 = await fetchC();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, each API call waits for the previous one to finish before starting, which means the total response time is the sum of all the individual call times&lt;/p&gt;

&lt;p&gt;To overcome this, we can run the API calls &lt;strong&gt;in parallel&lt;/strong&gt; instead of sequentially. The solution is to use &lt;code&gt;Promise.all()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [response1, response2, response3] = await Promise.all([
  fetchA(),
  fetchB(),
  fetchC(),
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The total response time dropped immediately. Instead of waiting for each other, all three requests were made simultaneously.&lt;/p&gt;

&lt;p&gt;That felt like a huge win. But here’s where things can get tricky…&lt;br&gt;
&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Hidden Risk with &lt;code&gt;Promise.all()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;While &lt;code&gt;Promise.all()&lt;/code&gt; looks like the perfect solution to run tasks in parallel, it has a major caveat:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If any one of the promises rejects, the whole &lt;code&gt;Promise.all()&lt;/code&gt; rejects — even if the other calls succeed.&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [response1, response2, response3] = await Promise.all([
  fetchA(),
  fetchB(), // let's say this fails
  fetchC(),
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;fetchB()&lt;/code&gt; throws an error, you won’t get the results of &lt;code&gt;fetchA()&lt;/code&gt; or &lt;code&gt;fetchC()&lt;/code&gt;, even if they succeeded. The entire batch fails.&lt;/p&gt;

&lt;p&gt;So if your API calls are &lt;strong&gt;independent&lt;/strong&gt; and you still want results from the ones that succeed, &lt;code&gt;Promise.all()&lt;/code&gt; may not be the best fit.&lt;br&gt;
&lt;br&gt;&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Safer Alternative: &lt;code&gt;Promise.allSettled()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;When you care about &lt;strong&gt;completing all tasks&lt;/strong&gt;, regardless of success or failure, use &lt;code&gt;Promise.allSettled()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const results = await Promise.allSettled([
  fetchA(),
  fetchB(),
  fetchC(),
]);

results.forEach((result, index) =&amp;gt; {
  if (result.status === 'fulfilled') {
    console.log(`Response ${index + 1}:`, result.value);
  } else {
    console.error(`Error in API ${index + 1}:`, result.reason);
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  When to Use What?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;Promise.all()&lt;/code&gt;&lt;/strong&gt; when &lt;strong&gt;all calls must succeed&lt;/strong&gt; for the task to proceed.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;&lt;code&gt;Promise.allSettled()&lt;/code&gt;&lt;/strong&gt; when &lt;strong&gt;calls are independent&lt;/strong&gt; and &lt;strong&gt;can fail individually&lt;/strong&gt; without affecting the rest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use individual &lt;code&gt;try/catch&lt;/code&gt; blocks&lt;/strong&gt; around each await call when you &lt;strong&gt;need granular error handling&lt;/strong&gt; for each API response.&lt;/li&gt;
&lt;/ul&gt;



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

&lt;p&gt;Async/await makes async logic cleaner, but that doesn’t guarantee efficiency or fault-tolerance. Knowing &lt;strong&gt;when to parallelize&lt;/strong&gt; and &lt;strong&gt;how to handle failures&lt;/strong&gt; is key.&lt;/p&gt;

&lt;p&gt;So the next time you’re making multiple API calls:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check if they’re &lt;strong&gt;independent&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Decide how you want to handle partial failures&lt;/li&gt;
&lt;li&gt;Choose &lt;code&gt;Promise.all()&lt;/code&gt;, &lt;code&gt;Promise.allSettled()&lt;/code&gt;, or a manual &lt;code&gt;await&lt;/code&gt; based on that&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tiny changes. Huge impact.&lt;/p&gt;




&lt;p&gt;✍️ Have you faced similar async challenges in your codebase? Let’s chat in the comments!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>api</category>
    </item>
  </channel>
</rss>
