<?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: Singh1106</title>
    <description>The latest articles on DEV Community by Singh1106 (@singh1106).</description>
    <link>https://dev.to/singh1106</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F705103%2F5945a21c-f52e-4ec0-9d79-cf575a2b3121.jpeg</url>
      <title>DEV Community: Singh1106</title>
      <link>https://dev.to/singh1106</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/singh1106"/>
    <language>en</language>
    <item>
      <title>In a Perfect World, We’d Only Need 200 and 400</title>
      <dc:creator>Singh1106</dc:creator>
      <pubDate>Thu, 20 Mar 2025 07:38:09 +0000</pubDate>
      <link>https://dev.to/singh1106/in-a-perfect-world-wed-only-need-200-and-400-56p6</link>
      <guid>https://dev.to/singh1106/in-a-perfect-world-wed-only-need-200-and-400-56p6</guid>
      <description>&lt;h2&gt;
  
  
  The Problem: Why Are There So Many HTTP status codes and Why Are they so confusing?!
&lt;/h2&gt;

&lt;p&gt;I once set out to build a simple REST API. All I had to do was return a response with the right HTTP status code. Simple, right? Wrong.&lt;/p&gt;

&lt;p&gt;My first instinct was to follow the "success or failure" approach. If things worked, I’d return 200 OK. If things didn’t work, I’d return 400 Bad Request. Done.&lt;/p&gt;

&lt;p&gt;Then, reality slapped me in the face. A senior engineer came along and asked:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Why are you returning 400 when it’s clearly a 422 Unprocessable Entity? And why are you not using 409 Conflict when there’s a duplicate entry? Oh, and don’t forget 304 Not Modified for caching. Also, rate limiting? 429 Too Many Requests."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And I just stood there, staring at my screen, questioning my life choices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Examples of the Madness
&lt;/h3&gt;

&lt;p&gt;Here’s a real-world example of how confusing HTTP status codes can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;500 Internal Server Error: Did my backend crash? Is my database down? Or did my load balancer just decide that today was not the day?&lt;/li&gt;
&lt;li&gt;404 Not Found: Is the endpoint missing, or is my routing broken? Or did I just typo my URL?&lt;/li&gt;
&lt;li&gt;401 Unauthorized vs. 403 Forbidden: Ah, yes. So I’m not authorized? But wait, am I not authorized because I’m missing credentials (401)? Or do I have credentials, but I’m just not worthy (403)?&lt;/li&gt;
&lt;li&gt;429 Too Many Requests: Ah, I see. My API is punishing me for being too enthusiastic.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Are These Status Codes Even a Thing?
&lt;/h3&gt;

&lt;p&gt;At this point, you might be asking, "Why do we even need all these?" I asked the same thing. Wouldn’t it be easier if we just had:&lt;/p&gt;

&lt;p&gt;✅ 200 OK for success❌ 400 Bad Request for failure&lt;/p&gt;

&lt;p&gt;Done. Simple. Clean. No headaches.&lt;/p&gt;

&lt;p&gt;But then reality struck again. HTTP status codes exist to help machines make decisions quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Secret Behind Why This Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Browsers need caching (304 Not Modified) so they don’t waste bandwidth downloading the same thing over and over.&lt;/li&gt;
&lt;li&gt;Load balancers need to know (502 Bad Gateway) when a backend is dead so they can reroute traffic.&lt;/li&gt;
&lt;li&gt;APIs need rate limiting (429 Too Many Requests) so users don’t take down the entire system.&lt;/li&gt;
&lt;li&gt;Search engines need redirects (301 Moved Permanently) so they know where to send users.&lt;/li&gt;
&lt;li&gt;Clients need to know when to retry (503 Service Unavailable means "try again later").&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By forcing clients to check the status code first instead of parsing the response body, we make APIs, browsers, and entire distributed systems faster and more efficient.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Minimal Set of HTTP Status Codes You Actually Need
&lt;/h3&gt;

&lt;p&gt;If we trim the fat, here’s what really matters:&lt;/p&gt;

&lt;p&gt;🙃 Instruction Informational Codes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;100 Continue - Server has started getting your data of the request, continue to do so.&lt;/li&gt;
&lt;li&gt;101 Client is requesting protocol switch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ Success Codes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;200 OK - Everything’s fine.&lt;/li&gt;
&lt;li&gt;201 Created - You made something new.&lt;/li&gt;
&lt;li&gt;204 No Content - Success, but there’s nothing to return.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📢 Redirection Codes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;301 Moved Permanently - This link changed forever.&lt;/li&gt;
&lt;li&gt;304 Not Modified - Use your cache, dude.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🚨 Client Error Codes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;400 Bad Request - Your request is broken.&lt;/li&gt;
&lt;li&gt;401 Unauthorized - Log in first.&lt;/li&gt;
&lt;li&gt;403 Forbidden - You’re logged in, but still not allowed.&lt;/li&gt;
&lt;li&gt;404 Not Found - It doesn’t exist.&lt;/li&gt;
&lt;li&gt;429 Too Many Requests - Slow down!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💥 Server Error Codes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;500 Internal Server Error - The backend is on fire.&lt;/li&gt;
&lt;li&gt;502 Bad Gateway - The backend is dead.&lt;/li&gt;
&lt;li&gt;503 Service Unavailable - The backend is taking a break.&lt;/li&gt;
&lt;li&gt;504 Gateway Timeout - The backend is slow.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Final Thoughts: Do We Still Hate Status Codes?
&lt;/h3&gt;

&lt;p&gt;Okay, so HTTP status codes are confusing, but they exist for a reason. They keep the internet running efficiently.&lt;/p&gt;

&lt;p&gt;Would it be simpler to just return 200 for success and 400 for failure? Maybe. But then browsers, load balancers, CDNs, and search engines would break.&lt;/p&gt;

&lt;p&gt;So while I still hate memorizing them, I have to admit—they’re useful. Now, if you’ll excuse me, I need to go debug another 500 error that my API just threw. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why zustand over redux?</title>
      <dc:creator>Singh1106</dc:creator>
      <pubDate>Thu, 20 Oct 2022 14:49:38 +0000</pubDate>
      <link>https://dev.to/singh1106/why-zustand-over-redux-28h7</link>
      <guid>https://dev.to/singh1106/why-zustand-over-redux-28h7</guid>
      <description>&lt;p&gt;So as my first post, so hoping for a lot and lot of positive criticism. I will write about why I like zustand over redux. Both of them are in a way, exactly same, both are unidirectional state managers, but why is zustand better (more like why I like zustand more), keep reading to know.. haha, big suspense movies' fan here.&lt;/p&gt;

&lt;p&gt;So we will start with an example of how to make a simple counter in redux:&lt;/p&gt;

&lt;p&gt;Let's first of all create a react app.&lt;br&gt;
&lt;code&gt;npx create-react-app redux-tut&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and now installing the dependencies needed&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install @redux/toolkit react-redux&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have to install these 2 dependencies, to be able to use redux toolkit, which is supposed to be the latest and with least boilerplate.&lt;/p&gt;

&lt;p&gt;Now in redux toolkit, one store has many slices, and every one of those slices, has their reducers and their actions and everything.&lt;/p&gt;

&lt;p&gt;Now we will create a new slice for our counter. &lt;/p&gt;

&lt;p&gt;Lets create a store folder in our src folder and a counterSlice.js folder in our store folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0uqNsvvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5p6vbrlenmsg5ej8hkh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0uqNsvvg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5p6vbrlenmsg5ej8hkh.png" alt="Redux Toolkit Slices store folder structure" width="444" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and paste this in your counterSlice.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createSlice } from "@reduxjs/toolkit";

export const slice = createSlice({
  name: "counter",
  initialState: {
    value: 0
  },
  reducers: {
    increment: (state) =&amp;gt; {
      state.value += 1;
    },
    decrement: (state) =&amp;gt; {
      state.value -= 1;
    },
    incrementByAmount: (state, action) =&amp;gt; {
      state.value += action.payload;
    }
  }
});

export const { increment, decrement, incrementByAmount } = slice.actions;
export const selectCount = (state) =&amp;gt; state.counter.value;

export default slice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in this counterSlice, we have 3 actions, increment and decrement by 1 and increment by value, and we can set the value.&lt;/p&gt;

&lt;p&gt;and lets create a store and import this slice into that store.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./counterSlice";

export default configureStore({
  reducer: {
    counter: counterReducer
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and now creating the Counter.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
  decrement,
  increment,
  incrementByAmount,
  selectCount,
} from "../store/counterSlice";

export function Counter() {
  const count = useSelector(selectCount);
  const dispatch = useDispatch();
  const [incrementAmount, setIncrementAmount] = useState("2");

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button
          aria-label="Increment value"
          onClick={() =&amp;gt; dispatch(increment())}
        &amp;gt;
          +
        &amp;lt;/button&amp;gt;
        &amp;lt;span&amp;gt;{count}&amp;lt;/span&amp;gt;
        &amp;lt;button
          aria-label="Decrement value"
          onClick={() =&amp;gt; dispatch(decrement())}
        &amp;gt;
          -
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div className&amp;gt;
        &amp;lt;input
          aria-label="Set increment amount"
          value={incrementAmount}
          onChange={(e) =&amp;gt; setIncrementAmount(e.target.value)}
        /&amp;gt;
        &amp;lt;button
          onClick={() =&amp;gt;
            dispatch(incrementByAmount(Number(incrementAmount) || 0))
          }
        &amp;gt;
          Add Amount
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and last but not the least, we have to import this store in the in the provider that we have to keep at the top most level, so as a parent of App.&lt;/p&gt;

&lt;p&gt;so go to the index.js for your app and.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import store from "./store/store";
import { Provider } from "react-redux";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;Provider store={store}&amp;gt;
      &amp;lt;App /&amp;gt;
    &amp;lt;/Provider&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;add this provider.&lt;/p&gt;

&lt;p&gt;You can check this non-css redux-tut &lt;a href="https://codesandbox.io/s/crazy-wind-w67ihu?file=/src/store/store.js:0-178"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now seems easy af anyway, but lets have a look at the competitor, zustand, which has typescript support by the way.&lt;/p&gt;

&lt;p&gt;Will start from the basics, lets create &lt;br&gt;
&lt;code&gt;npx create-react-app zustand-tut&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and the dependicies&lt;br&gt;
&lt;code&gt;npm install zustand&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;yes thats right, thats all the dependicies we need.&lt;/p&gt;

&lt;p&gt;now lets create a store. This time the folder structure is.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PIBKYfJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r390mvzoohar27kokanh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PIBKYfJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r390mvzoohar27kokanh.png" alt="Zustand store folder structure" width="516" height="602"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and now paste this in store/counterStore.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import create from "zustand";

export const useCounterStore = create((set) =&amp;gt; ({
  count: 0,
  incrementBy1: () =&amp;gt; set((state) =&amp;gt; ({ count: state.count + 1 })),
  decrementBy1: () =&amp;gt; set((state) =&amp;gt; ({ count: state.count - 1 })),
  incrementByVal: (val) =&amp;gt; set((state) =&amp;gt; ({ count: state.count + val }))
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and in counter.js, this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import { useCounterStore } from "../store/counterStore";

export function Counter() {
  const { count, incrementBy1, decrementBy1, incrementByVal } = useCounterStore(
    (state) =&amp;gt; state
  );
  const [incrementVal, setIncrementVal] = useState(5);

  const incrementByValHandler = (e) =&amp;gt; {
    incrementByVal(Number(incrementVal));
  };
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;button aria-label="Increment value" onClick={incrementBy1}&amp;gt;
          +
        &amp;lt;/button&amp;gt;
        &amp;lt;span&amp;gt;{count}&amp;lt;/span&amp;gt;
        &amp;lt;button aria-label="Decrement value" onClick={decrementBy1}&amp;gt;
          -
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div className&amp;gt;
        &amp;lt;input
          aria-label="Set increment amount"
          value={incrementVal}
          onChange={(e) =&amp;gt; setIncrementVal(e.target.value)}
        /&amp;gt;
        &amp;lt;button onClick={incrementByValHandler}&amp;gt;Add Amount&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;And here we dont have to wrap it in provider as well.&lt;/p&gt;

&lt;p&gt;and thats it. Lets try this on &lt;a href="https://codesandbox.io/s/recursing-hamilton-ri8zud?file=/src/components/Counter.js:0-933"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now as we can see, much less boilerplate, no dispatches, no providers, much more readable, and last but not least, much less code. Now 1 thing that is left is performance, for that I will write another article, in the mean time, if you wanna just trust my word, I like zustand more..&lt;/p&gt;

&lt;p&gt;Thank you everyone.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Different type of state managers' articles coming soon.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0i48cQRr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzkq0pw7n848gyw3uaqb.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0i48cQRr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzkq0pw7n848gyw3uaqb.jpeg" alt="yay zustaaand" width="549" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

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