<?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: Mohammad Talim</title>
    <description>The latest articles on DEV Community by Mohammad Talim (@md-talim).</description>
    <link>https://dev.to/md-talim</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%2F1175254%2F7d70241e-8c0f-4bbe-ac9f-488b07f6d865.png</url>
      <title>DEV Community: Mohammad Talim</title>
      <link>https://dev.to/md-talim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/md-talim"/>
    <language>en</language>
    <item>
      <title>I Built a Distributed Task Queue from Scratch with Go and PostgreSQL</title>
      <dc:creator>Mohammad Talim</dc:creator>
      <pubDate>Fri, 02 Oct 2026 18:35:59 +0000</pubDate>
      <link>https://dev.to/md-talim/i-built-a-distributed-task-queue-from-scratch-with-go-and-postgresql-c6j</link>
      <guid>https://dev.to/md-talim/i-built-a-distributed-task-queue-from-scratch-with-go-and-postgresql-c6j</guid>
      <description>&lt;p&gt;I built &lt;strong&gt;Dhara&lt;/strong&gt;, a distributed task queue for Go, using PostgreSQL as the only queue and source of truth. No Redis, no Kafka, no RabbitMQ.&lt;/p&gt;

&lt;p&gt;In this post I'll cover why I built it, how a task moves through the system, the design decisions that mattered (transactional enqueue and &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;), a performance bug that made my queue 8x slower than it should have been, and what I'm building next.&lt;/p&gt;

&lt;p&gt;Code: &lt;a href="https://github.com/Md-Talim/dhara" rel="noopener noreferrer"&gt;github.com/Md-Talim/dhara&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I built a distributed task queue
&lt;/h2&gt;

&lt;p&gt;Honestly, I don't remember exactly why I decided to build this.&lt;/p&gt;

&lt;p&gt;What I do remember is how I got there. I'd been asking AI models about my next project with very vague prompts like "give me hard backend engineering project ideas", and since the model had my past context and history, it kept giving me a lot of ideas. Two of them showed up every single time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a distributed task queue&lt;/li&gt;
&lt;li&gt;a distributed task orchestration system&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By then I had already built Redis, a shell, an HTTP server, an interpreter, and some parts of Git, all from scratch, as part of the CodeCrafters challenges. I'm very fond of building things from scratch to understand them better, and I like taking on hard challenges. So building a task queue from scratch felt like the obvious next exercise.&lt;/p&gt;

&lt;p&gt;It turned out to be one of the best decisions I've made. I learned about concurrency, failure handling, a few core ideas of distributed systems, designing client-side APIs, transactions, and I did a hell of a lot of debugging along the way.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a task queue actually is
&lt;/h2&gt;

&lt;p&gt;A task queue is, well, a queue of tasks that need to be executed.&lt;/p&gt;

&lt;p&gt;Why do we need one? Because not every piece of work needs to happen during the client's request. Some work can be done later, or on a schedule. For performance and a good user experience, anything that &lt;em&gt;can&lt;/em&gt; run asynchronously &lt;em&gt;should&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A few examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sending a welcome email after sign up&lt;/li&gt;
&lt;li&gt;processing images&lt;/li&gt;
&lt;li&gt;converting an uploaded video into different resolutions (like YouTube does with 480p, 720p, and so on)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The mental model is just the queue data structure you already know. Producers insert tasks at the back (enqueue), and workers take them out from the front when it's time to execute them.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4z90sszfmghh836ltq87.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4z90sszfmghh836ltq87.png" alt="Basic task queue" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The architecture and how a task moves through the system
&lt;/h2&gt;

&lt;p&gt;When I started this project, I had very little idea of what a task queue actually was. I hadn't used a real one in any of my projects either. Plenty of task queues are built on top of Redis or a message broker like Kafka, but I decided to do it all from scratch (I'll explain what "all" means later in this post).&lt;/p&gt;

&lt;p&gt;A basic task queue needs these components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tasks:&lt;/strong&gt; the actual units of work. Each task has a name (type) and a payload, and each type has a handler that knows how to execute it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Queue:&lt;/strong&gt; where tasks are inserted, and where they wait until it's time to execute them. I used &lt;strong&gt;PostgreSQL&lt;/strong&gt; as the queue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Workers:&lt;/strong&gt; they take tasks out of the queue and run the matching handlers. In my implementation workers are simple goroutines, but they can be deployed separately too. This is also where most of the "distributed" part lives.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then there are the components that take it from a toy to something closer to production grade:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reaper:&lt;/strong&gt; a separate background process (a goroutine here) that finds tasks stuck on a dead worker and puts them back in the queue so they can run again.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API server:&lt;/strong&gt; we need a way to talk to the system: create tasks, list them, cancel them, retry them. My first version was only an HTTP server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client library:&lt;/strong&gt; the other way to use Dhara is to import it in your Go code and manage tasks with function calls. Ease of use wasn't my main reason for building it, though. It was about &lt;strong&gt;transactions&lt;/strong&gt;, and it ended up changing the whole design. I'll get to that in the section on &lt;code&gt;EnqueueTx&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzv1qngls5xamuafgxz6z.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzv1qngls5xamuafgxz6z.png" alt="Dhara architecture" width="800" height="544"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A task goes through a few states in its life:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It's created as &lt;code&gt;PENDING&lt;/code&gt; (via the API or the client library).&lt;/li&gt;
&lt;li&gt;A worker claims it and it becomes &lt;code&gt;RUNNING&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the handler succeeds, it's &lt;code&gt;COMPLETED&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the handler fails (or the worker dies), it goes back to &lt;code&gt;PENDING&lt;/code&gt; to be retried, until it runs out of attempts, and then it's &lt;code&gt;DEAD&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A task can also be &lt;code&gt;CANCELED&lt;/code&gt; through the API while it's pending or running.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4yxddjnypj8enyyylvi0.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4yxddjnypj8enyyylvi0.png" alt="Task lifecycle" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  PostgreSQL as the source of truth
&lt;/h2&gt;

&lt;p&gt;I used Postgres for two main reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I knew Postgres.&lt;/li&gt;
&lt;li&gt;The way I designed the whole system, it solves a very real problem.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using PostgreSQL as the &lt;em&gt;only&lt;/em&gt; queue was a deliberate decision. To understand the problem, imagine I had used Kafka instead. Take an e-commerce example. The typical flow would be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The client performs some business logic (a sign up, a product order).&lt;/li&gt;
&lt;li&gt;The client saves the data in their own database in a transaction (user details, order details, and so on).&lt;/li&gt;
&lt;li&gt;The client publishes a message to a Kafka topic like "send confirmation email", which a consumer later picks up and turns into an actual email.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;See the problem? Steps 2 and 3 talk to two different systems, and there's no transaction that covers both. This is known as the &lt;strong&gt;dual write problem&lt;/strong&gt;. When one of them fails, you get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The database transaction fails, but the Kafka publish succeeds:&lt;/strong&gt; the user gets an email for an order that doesn't exist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The database succeeds, but the Kafka publish fails:&lt;/strong&gt; the order exists, but no email goes out, and the user is left wondering whether the order was even placed.&lt;/li&gt;
&lt;/ul&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fruayv0p541b7s37gljyk.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fruayv0p541b7s37gljyk.png" alt="Dual write problem" width="751" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the problem Postgres solves here. If the task is just a row in the &lt;em&gt;same&lt;/em&gt; database as your business data, then inserting the task can be part of the &lt;em&gt;same transaction&lt;/em&gt; as your business queries. Either both commit, or neither does. This pattern is usually called the &lt;strong&gt;transactional outbox&lt;/strong&gt;, and Dhara basically bakes it into the queue itself.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb19fwmc2zgz13w39qz8g.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb19fwmc2zgz13w39qz8g.png" alt="Single transaction" width="618" height="735"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Of course there's a trade-off. Postgres isn't a purpose-built message broker, so it won't match Kafka's raw throughput. But for the vast majority of applications, correctness here matters way more than squeezing out the last bit of throughput.&lt;/p&gt;

&lt;h2&gt;
  
  
  Atomic task claiming with &lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The next problem every task queue has to solve: when multiple workers are pulling from the same queue, &lt;strong&gt;how do you make sure a task isn't claimed concurrently by multiple worker?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a serious problem. If two workers get the same task, that task runs twice, and you can imagine what happens when the task is "charge the customer".&lt;/p&gt;

&lt;p&gt;This is where pessimistic locking comes in, and the nice part is that I didn't need a separate lock service. Postgres gives it to us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;FOR UPDATE&lt;/code&gt;:&lt;/strong&gt; locks the selected rows. If another transaction already holds the lock, it &lt;em&gt;blocks&lt;/em&gt; until the lock is released, and then re-evaluates the row to make sure it still matches the query.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;SKIP LOCKED&lt;/code&gt;:&lt;/strong&gt; this is the one I used (&lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;). Instead of waiting, it simply &lt;em&gt;skips&lt;/em&gt; the rows that are already locked.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With plain &lt;code&gt;FOR UPDATE&lt;/code&gt;, 20 workers would all line up behind the same row, waiting on each other. With &lt;code&gt;SKIP LOCKED&lt;/code&gt;, each worker just grabs the next task that nobody else has touched. (The Postgres docs even call out queue-like tables as the use case for this.)&lt;/p&gt;

&lt;p&gt;Here's a simplified version of what claiming looks like:&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="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'RUNNING'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;locked_by&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;locked_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;started_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;attempts&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="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'PENDING'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;run_at&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;run_at&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
    &lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;SKIP&lt;/span&gt; &lt;span class="n"&gt;LOCKED&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;RETURNING&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_retries&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idempotency_key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once a worker claims a task, another worker can't concurrently claim that same pending task. And a worker never sits waiting on a locked row; it just gets one of the tasks that isn't already taken.&lt;/p&gt;

&lt;h2&gt;
  
  
  Worker concurrency, retries, and stale-task recovery
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Workers
&lt;/h3&gt;

&lt;p&gt;Workers are goroutines: lightweight units of execution in Go, very cheap to start (even compared to a thread). Each worker is its own goroutine, and the loop looks like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Try to claim a task.&lt;/li&gt;
&lt;li&gt;If one is found, execute it, then immediately try to claim the next one.&lt;/li&gt;
&lt;li&gt;If none is found, wait for the poll interval (1 second by default, configurable) and try again.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9y1933dfudpt43i6kaw3.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9y1933dfudpt43i6kaw3.png" alt="Worker Loop" width="800" height="775"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that this is a &lt;strong&gt;polling&lt;/strong&gt; mechanism: every worker keeps asking the database "anything for me?". Next on my list is a &lt;strong&gt;push&lt;/strong&gt; mechanism using Postgres &lt;code&gt;LISTEN/NOTIFY&lt;/code&gt;, which is another reason I picked Postgres over other databases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retries
&lt;/h3&gt;

&lt;p&gt;In distributed systems you can't avoid failures, so you design the system to keep working even when things fail. In Dhara there are a few mechanisms for that.&lt;/p&gt;

&lt;p&gt;When a task fails for any reason (the handler returned an error, the worker crashed, a database or network call failed), it gets retried. Each time a worker claims a task, the attempt count goes up, and the task can be retried until it hits the max number of attempts (configurable).&lt;/p&gt;

&lt;p&gt;Retries use &lt;strong&gt;exponential backoff with full jitter&lt;/strong&gt;. The backoff gives a struggling downstream service some breathing room, and the jitter randomizes &lt;em&gt;when&lt;/em&gt; each retry fires, so a thousand failed tasks don't all come back at the exact same second (the "thundering herd" problem). When a task runs out of attempts, it goes to the &lt;code&gt;DEAD&lt;/code&gt; state, and you can manually retry it through the API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Heartbeats and the reaper
&lt;/h3&gt;

&lt;p&gt;What if the worker doesn't fail gracefully, and just dies in the middle of a task? Nothing returns an error. The task just sits in &lt;code&gt;RUNNING&lt;/code&gt; forever.&lt;/p&gt;

&lt;p&gt;That's where heartbeats come in. While a handler is running, a background goroutine periodically sets &lt;code&gt;locked_at&lt;/code&gt; and &lt;code&gt;updated_at&lt;/code&gt; to &lt;code&gt;now()&lt;/code&gt; for that task (matched by task id and worker id). It's basically the worker saying "still alive, still working on it".&lt;/p&gt;

&lt;p&gt;If the worker dies, the heartbeat stops. Then the &lt;strong&gt;reaper&lt;/strong&gt;, which scans on an interval, looks for &lt;code&gt;RUNNING&lt;/code&gt; tasks that haven't had a heartbeat within the stuck threshold. It puts those tasks back to &lt;code&gt;PENDING&lt;/code&gt; so another worker can claim them, or moves them to &lt;code&gt;DEAD&lt;/code&gt; if they're out of attempts.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3e2zq9ki74pj74priqvc.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3e2zq9ki74pj74priqvc.png" alt="Heartbeat &amp;amp; Reaper" width="799" height="528"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One important consequence: Dhara gives &lt;strong&gt;at-least-once&lt;/strong&gt; execution. A worker could do half of the work (say, send the email) and die before marking the task as done. The reaper will requeue it, and it will run again. So handlers should be &lt;strong&gt;idempotent&lt;/strong&gt;, meaning running them twice should be safe. This is also why I added idempotency keys at enqueue time, so the same enqueue request replayed twice doesn't create two tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Graceful shutdown
&lt;/h3&gt;

&lt;p&gt;When Dhara receives &lt;code&gt;SIGINT&lt;/code&gt; or &lt;code&gt;SIGTERM&lt;/code&gt;, workers stop claiming new tasks and give in-flight tasks a configurable amount of time to finish. After that, the connection pool is drained. Without this, every deploy would kill running tasks mid-way and leave the reaper to clean up the mess.&lt;/p&gt;

&lt;h2&gt;
  
  
  What "from scratch" actually meant
&lt;/h2&gt;

&lt;p&gt;Earlier I said I decided to do it all from scratch. Here is what I meant.&lt;/p&gt;

&lt;p&gt;Dhara has only three dependencies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pgx&lt;/code&gt;, the PostgreSQL driver&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;google/uuid&lt;/code&gt;, for generating UUIDs&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;vow&lt;/code&gt;, my own database migration runner&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything else is the Go standard library. The HTTP API runs on &lt;code&gt;net/http&lt;/code&gt;, so there is no web framework or router. Logging uses &lt;code&gt;slog&lt;/code&gt;. The Prometheus metrics endpoint is also written by hand, without the official client library. The format is just plain text: a couple of comment lines describing each metric, then a line with the metric name and its current value. Counters like &lt;code&gt;tasks_enqueued_total&lt;/code&gt; and gauges like &lt;code&gt;tasks_by_status&lt;/code&gt; are only a few lines each.&lt;/p&gt;

&lt;p&gt;The migration runner is the best example of this. Dhara needed a way to set up its database tables, and instead of picking an existing tool, I built one. It started as a small part of Dhara and grew into a project of its own: &lt;a href="https://github.com/Md-Talim/vow" rel="noopener noreferrer"&gt;Vow&lt;/a&gt;. It runs your SQL files in order, uses PostgreSQL advisory locks so two instances of a service can't migrate at the same time, and refuses to run if an already applied migration was renamed or deleted. Dhara's migrations run through Vow. I'll write about what I learned about database migrations in a separate post.&lt;/p&gt;

&lt;p&gt;I'm not saying you should build everything yourself. I didn't write a Postgres driver or a UUID generator. I just built the parts I was curious about, and used libraries for the rest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benchmarking and what the results taught me
&lt;/h2&gt;

&lt;p&gt;I'd seen people use throughput and latency numbers as evidence that a backend project was serious.&lt;/p&gt;

&lt;p&gt;I didn't know how to benchmark a system like this, so I took some help from AI to set up &lt;a href="https://k6.io/" rel="noopener noreferrer"&gt;k6&lt;/a&gt; load tests against the HTTP API. The important part is that I didn't just trust HTTP response codes. I used Postgres as the source of truth for what &lt;em&gt;actually&lt;/em&gt; happened to each task.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setup:&lt;/strong&gt; 20 workers, a handler that simulates 50-200ms of I/O, a laptop with an i3 (11th gen) / 8GB RAM / 500GB SSD, and a max of 25 database connections.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finding a real bottleneck
&lt;/h3&gt;

&lt;p&gt;Before running anything, I did the math. 20 workers, each finishing a task in ~125ms on average, should give roughly &lt;strong&gt;160 tasks/sec&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The first measured result: &lt;strong&gt;~20 tasks/sec&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's 8x lower than expected, so something was wrong. The cause was in my claim loop. A worker only attempted &lt;em&gt;one&lt;/em&gt; claim per poll tick, no matter how quickly the task finished:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;ticker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;processNext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&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;So 20 workers × 1 claim per second (the 1s poll interval) = 20 tasks/sec. The math matched the measurement almost exactly. A worker would finish a 100ms task and then sit idle for the remaining 900ms.&lt;/p&gt;

&lt;p&gt;The fix: keep claiming continuously while there's work available, and fall back to the poll interval only when the queue is empty. After the fix: &lt;strong&gt;~148 tasks/sec&lt;/strong&gt;, within a few percent of my original prediction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Results
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Submission rate&lt;/th&gt;
&lt;th&gt;Worker utilization&lt;/th&gt;
&lt;th&gt;p50&lt;/th&gt;
&lt;th&gt;p95&lt;/th&gt;
&lt;th&gt;p99&lt;/th&gt;
&lt;th&gt;Task loss&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;100 tasks/sec&lt;/td&gt;
&lt;td&gt;~67% (headroom)&lt;/td&gt;
&lt;td&gt;181ms&lt;/td&gt;
&lt;td&gt;299ms&lt;/td&gt;
&lt;td&gt;380ms&lt;/td&gt;
&lt;td&gt;0 / 6,001&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;~148 tasks/sec&lt;/td&gt;
&lt;td&gt;~100% (saturation)&lt;/td&gt;
&lt;td&gt;1.30s&lt;/td&gt;
&lt;td&gt;2.03s&lt;/td&gt;
&lt;td&gt;2.06s&lt;/td&gt;
&lt;td&gt;0 / 9,001&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Latency here is end-to-end (&lt;code&gt;completed_at - created_at&lt;/code&gt;), not just how long the HTTP enqueue took.&lt;/p&gt;

&lt;h3&gt;
  
  
  What this taught me
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Measure the thing that matters, not the thing that's easy to measure.&lt;/strong&gt; Enqueue latency stayed under 10ms (p99) in every run, even the one that was bottlenecked at 20 tasks/sec. If I'd only looked at HTTP response times, I would have thought everything was fine. I only caught the bug by checking task state in Postgres.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Predict before you measure.&lt;/strong&gt; Having a number in my head (~160/sec) is the only reason I knew 20/sec was wrong. Without a prediction, I might have just accepted it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Saturation is not failure.&lt;/strong&gt; At ~148 tasks/sec the latency jumps, but that's queueing delay from running at the edge of worker capacity, not broken correctness. Every run completed 100% of tasks. I also ran a burst test that deliberately oversubmitted (120 tasks/sec against the then-20/sec ceiling), and once workers caught up, the system fully drained a 5,800-task backlog with zero loss.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To be clear, I'm not claiming 148 tasks/sec is fast. It's bounded by 20 workers and the handler's duration, on a laptop, with a simulated handler. What matters to me is that the measured number matched the prediction, and that no task was lost. You can reproduce everything with the scripts in the &lt;a href="https://github.com/Md-Talim/dhara/tree/main/benchmarks" rel="noopener noreferrer"&gt;&lt;code&gt;benchmarks&lt;/code&gt;&lt;/a&gt; folder, including full methodology in &lt;code&gt;RESULTS.md&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The decisions that changed my implementation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The poll loop
&lt;/h3&gt;

&lt;p&gt;That's the bug above. Benchmarks changed my worker loop, and it's a good reminder that you don't know how your concurrent code behaves until you load it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Building the client library, and why &lt;code&gt;EnqueueTx&lt;/code&gt; exists
&lt;/h3&gt;

&lt;p&gt;My first version of Dhara only had an HTTP server. You &lt;code&gt;POST /api/v1/tasks&lt;/code&gt; and the task gets created. Simple, language-agnostic, works.&lt;/p&gt;

&lt;p&gt;But remember the whole reason I chose Postgres: the task insert should be atomic with the business writes. With an HTTP API, that's impossible. Your application writes the order in &lt;em&gt;its&lt;/em&gt; transaction, then makes an HTTP call, which inserts the task in &lt;em&gt;another&lt;/em&gt; transaction on another connection. There's no way to put both under one commit. If your app crashes between the two, you're right back to the dual write problem I was trying to escape.&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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb6dywt8raxutd0rlyusx.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb6dywt8raxutd0rlyusx.png" alt="HTTP vs EnqueueTx" width="800" height="651"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So I built a client library, and the key function is &lt;code&gt;EnqueueTx&lt;/code&gt;. It takes &lt;em&gt;your&lt;/em&gt; transaction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;pool&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// your business logic&lt;/span&gt;
&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;`INSERT INTO orders (id, total) VALUES ($1, $2)`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;42.00&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;// the task is committed only if the order is&lt;/span&gt;
&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;EnqueueTx&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"send_email"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EmailPayload&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;To&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;      &lt;span class="s"&gt;"user@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Subject&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"Order #"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;orderID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Commit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Roll back the transaction and the task never existed. Commit it and both are there, atomically.&lt;/p&gt;

&lt;p&gt;Two details I'm happy with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Idempotent enqueue without poisoning your transaction.&lt;/strong&gt; Enqueue uses &lt;code&gt;INSERT ... ON CONFLICT DO NOTHING&lt;/code&gt; with an idempotency key. In Postgres, a failed statement aborts the entire transaction, so if a duplicate insert raised an error, it would take down the caller's whole transaction. Instead, a replay quietly returns the existing task, with &lt;code&gt;Duplicate: true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One code path.&lt;/strong&gt; The HTTP server and the worker binaries are now thin wrappers built from the library. They can't drift from it, because they &lt;em&gt;are&lt;/em&gt; it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I'd build next
&lt;/h2&gt;

&lt;p&gt;This project got me really interested in distributed systems, and in the finer details of concurrency and handling failures. So I'm going deeper: I'm taking MIT's distributed systems course, reading the papers, going through the lectures, and implementing the labs.&lt;/p&gt;

&lt;p&gt;The labs mainly come down to three things: MapReduce, a replicated and sharded key/value store, and Raft. I'm focusing on these for now, and the KV store and Raft will be my next flagship projects.&lt;/p&gt;

&lt;p&gt;I should be honest about one thing: in Dhara, the workers scale out horizontally, but PostgreSQL itself is still a single point of failure unless you run it with replication and failover. Understanding how machines agree on shared state, even when some of them fail, is a big part of why I want to go through Raft properly.&lt;/p&gt;

&lt;p&gt;For Dhara itself, the roadmap is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;replacing constant polling with &lt;code&gt;LISTEN/NOTIFY&lt;/code&gt;, so workers get pushed new tasks instead of hitting the database every second&lt;/li&gt;
&lt;li&gt;better dead-letter handling&lt;/li&gt;
&lt;li&gt;execution histograms and queue latency metrics&lt;/li&gt;
&lt;li&gt;more robust cancellation semantics&lt;/li&gt;
&lt;li&gt;richer operational dashboards&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This project will keep evolving.&lt;/p&gt;

&lt;h2&gt;
  
  
  Terms and Concepts I had to look up and learn
&lt;/h2&gt;

&lt;p&gt;Some of these I had never heard of before this project, and some I only knew by name. This is the list of terms I had to look up and properly understand while building Dhara.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dual write problem:&lt;/strong&gt; when you write to two separate systems (like a database and Kafka) and there is no single transaction covering both, so one can succeed while the other fails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transactional outbox:&lt;/strong&gt; the fix for the dual write problem. You save the message or task in the same database transaction as your business data, so both happen or neither does.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idempotency:&lt;/strong&gt; doing the same operation twice has the same result as doing it once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;At-least-once delivery:&lt;/strong&gt; a task is guaranteed to run at least one time, but it might run more than once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pessimistic locking:&lt;/strong&gt; locking a row before you work on it, so nobody else can touch it in the meantime.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;FOR UPDATE&lt;/code&gt;:&lt;/strong&gt; the Postgres clause that locks the rows you select. Other transactions have to wait for the lock to be released.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;FOR UPDATE SKIP LOCKED&lt;/code&gt;:&lt;/strong&gt; the same, but instead of waiting, it skips the locked rows. This is what lets many workers pull from one queue safely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Polling vs pushing:&lt;/strong&gt; asking "anything new?" again and again, versus being told when something new arrives.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;LISTEN/NOTIFY&lt;/code&gt;:&lt;/strong&gt; the Postgres feature that lets the database push a notification to listeners. This is my next step for replacing polling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heartbeat:&lt;/strong&gt; a small signal a worker sends regularly to say "I'm still alive". When the signals stop, something is wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reaper:&lt;/strong&gt; the background process that finds tasks stuck on dead workers and puts them back in the queue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exponential backoff and jitter:&lt;/strong&gt; waiting longer after each failed attempt, with a bit of randomness added to the wait.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thundering herd:&lt;/strong&gt; many clients all retrying at the exact same moment and overloading a system that was already struggling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dead-letter queue:&lt;/strong&gt; a place for tasks that failed too many times, so a human can look at them. In Dhara this is the &lt;code&gt;DEAD&lt;/code&gt; state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Advisory locks:&lt;/strong&gt; Postgres locks that are not tied to a row. I used them in Vow so only one instance runs migrations at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Percentile latency (p50, p95, p99):&lt;/strong&gt; the time within which 50%, 95%, or 99% of requests finish. It shows you what the slow cases look like, which an average hides.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've read this far, thank you. The code is on GitHub at &lt;a href="https://github.com/Md-Talim/dhara" rel="noopener noreferrer"&gt;Md-Talim/dhara&lt;/a&gt;. If you spot a flaw in the design, I'd genuinely love to hear about it. I'm sharing all of this as part of my learning journey, so follow along on &lt;a href="https://x.com/talimbuilds" rel="noopener noreferrer"&gt;X / @talimbuilds&lt;/a&gt; for what's next.&lt;/p&gt;

</description>
      <category>distributedsystems</category>
      <category>go</category>
      <category>postgres</category>
      <category>backend</category>
    </item>
    <item>
      <title>Building a Chain Calculator in JavaScript: Method Chaining for Clean Arithmetic</title>
      <dc:creator>Mohammad Talim</dc:creator>
      <pubDate>Mon, 09 Jun 2025 15:42:01 +0000</pubDate>
      <link>https://dev.to/md-talim/building-a-chain-calculator-in-javascript-method-chaining-for-clean-arithmetic-3m6h</link>
      <guid>https://dev.to/md-talim/building-a-chain-calculator-in-javascript-method-chaining-for-clean-arithmetic-3m6h</guid>
      <description>&lt;p&gt;In this post, we'll explore how to design a Chain Calculator—a calculator class that lets you perform a series of arithmetic operations using a fluent, chainable interface. This is a popular interview question and a great exercise to deepen your understanding of JavaScript classes and method chaining.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Method Chaining?
&lt;/h2&gt;

&lt;p&gt;Method chaining is a programming pattern that allows multiple method calls in a single statement, improving code readability and succinctness. It's used widely in libraries like jQuery and Lodash, and it's a great tool for creating intuitive APIs. By building a chainable calculator, you'll learn how to design methods that return &lt;code&gt;this&lt;/code&gt;, enabling seamless chaining of operations.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Design a Chain Calculator in JavaScript that supports method chaining for basic arithmetic operations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Addition&lt;/li&gt;
&lt;li&gt;Subtraction&lt;/li&gt;
&lt;li&gt;Multiplication&lt;/li&gt;
&lt;li&gt;Division&lt;/li&gt;
&lt;li&gt;Exponentiation (power)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The calculator should allow users to perform a sequence of operations in one line, like this:&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;calc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Calculator&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="kd"&gt;const&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;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&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="nf"&gt;subtract&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="nf"&gt;multiply&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="nf"&gt;divide&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="nf"&gt;getResult&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initial value&lt;/strong&gt;: The calculator starts with a number passed to its constructor.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chainable methods&lt;/strong&gt;: Support these methods:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;add(number)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;subtract(number)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;multiply(number)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;divide(number)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;power(number)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;All arguments&lt;/strong&gt; are valid numbers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Division by zero&lt;/strong&gt; should throw an error.&lt;/li&gt;
&lt;li&gt;The final result is retrieved with &lt;code&gt;getResult()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Approach: Using Classes and Chaining
&lt;/h2&gt;

&lt;p&gt;To achieve method chaining, each arithmetic method will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modify the calculator's internal state.&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;this&lt;/code&gt; so calls can be chained.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll use a class to encapsulate state and behavior. For error handling, division by zero will throw an exception.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Not a Functional Approach?
&lt;/h3&gt;

&lt;p&gt;You might wonder: why not just use functions and closures? While possible, the class-based approach is more idiomatic for method chaining in JavaScript and is more readable, especially in interview settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation: Step by Step
&lt;/h2&gt;

&lt;p&gt;Let's build the &lt;code&gt;Calculator&lt;/code&gt; class step by step.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Skeleton
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&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;h3&gt;
  
  
  2. Arithmetic Methods
&lt;/h3&gt;

&lt;p&gt;Each method updates the result and returns &lt;code&gt;this&lt;/code&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Division with Error Handling
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Division by zero is not allowed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Exponentiation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;power&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;**=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Retrieving the Result
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;getResult&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&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;h3&gt;
  
  
  6. The Complete Class
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;subtract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Division by zero is not allowed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;power&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;**=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getResult&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&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;
  
  
  Example Usage
&lt;/h2&gt;

&lt;p&gt;Let's see this calculator in action:&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;calc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Calculator&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&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="nf"&gt;subtract&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="nf"&gt;multiply&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="nf"&gt;divide&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="nf"&gt;getResult&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Output: 8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  More Examples
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Expression&lt;/th&gt;
&lt;th&gt;Output&lt;/th&gt;
&lt;th&gt;Explanation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;new Calculator(10).add(5).subtract(3).getResult()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;10 + 5 - 3 = 12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;new Calculator(2).multiply(5).power(2).getResult()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;td&gt;(2 × 5)² = 10² = 100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;new Calculator(20).divide(4).add(2).multiply(3).getResult()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;21&lt;/td&gt;
&lt;td&gt;20 ÷ 4 = 5, 5 + 2 = 7, 7 × 3 = 21&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;new Calculator(5).divide(0).getResult()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Error&lt;/td&gt;
&lt;td&gt;Throws error for division by zero&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;new Calculator(7).getResult()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;No operations; returns initial value&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Time and Space Complexity
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; O(1) per operation (each method does a simple calculation).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; O(1) (stores only a single number).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Always return &lt;code&gt;this&lt;/code&gt; from methods to enable chaining.&lt;/li&gt;
&lt;li&gt;Handle errors (like division by zero) gracefully.&lt;/li&gt;
&lt;li&gt;This pattern—called the &lt;a href="https://en.wikipedia.org/wiki/Fluent_interface" rel="noopener noreferrer"&gt;fluent interface&lt;/a&gt;—makes APIs easy and intuitive to use.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes" rel="noopener noreferrer"&gt;JavaScript Classes (MDN)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Fluent_interface" rel="noopener noreferrer"&gt;Fluent Interface Pattern&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>interview</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Visually Hide Elements Using CSS Clipping So That Screen Readers Can Still Read It</title>
      <dc:creator>Mohammad Talim</dc:creator>
      <pubDate>Tue, 24 Oct 2023 02:46:16 +0000</pubDate>
      <link>https://dev.to/md-talim/how-to-visually-hide-elements-using-css-clipping-so-that-screen-readers-can-still-read-it-nh2</link>
      <guid>https://dev.to/md-talim/how-to-visually-hide-elements-using-css-clipping-so-that-screen-readers-can-still-read-it-nh2</guid>
      <description>&lt;h2&gt;
  
  
  What is CSS Clipping?
&lt;/h2&gt;

&lt;p&gt;Simply put, CSS clipping allows you to hide parts of an element. You can define a rectangle that acts as a mask. Anything outside that mask get clipped or hidden.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;clip&lt;/code&gt; property lets you specify the coordinates of the clipping rectangle. The coordinates are relative to the element's top-left corner. For 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;clip&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;rect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;10&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;20&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;30&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt; &lt;span class="err"&gt;40&lt;/span&gt;&lt;span class="nt"&gt;px&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This hides everything except a rectangle with a top edge 10px down, a right edge 20px across, a bottom edge 30px down, and a left edge 40px across.&lt;br&gt;
To hide the entire element, set &lt;code&gt;clip: rect(0 0 0 0)&lt;/code&gt; This creates a zero-size rectangle that hides everything.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Hide Elements?
&lt;/h2&gt;

&lt;p&gt;There are times you may want to hide something visually but keep it for screen readers. &lt;br&gt;
For example:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provide text alternative for images, icons, or unclear buttons.&lt;/li&gt;
&lt;li&gt;Give extra instructions for screen reader users&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CSS clipping hides content from sighted users but leaves it available for assistive technologies&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
Here is HTML and CSS to visually hide an element&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;class=&lt;/span&gt;&lt;span class="s"&gt;"user-offline"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  John &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"visually-hidden"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;(offline)&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"user-online"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Kyle &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"visually-hidden"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;(online)&lt;span class="nt"&gt;&amp;lt;/span&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.visually-hidden&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;clip&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;hidden&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 &lt;code&gt;visually-hidden&lt;/code&gt; class applies the clipping and other CSS to hide the element.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;position: absolute&lt;/code&gt; removes it from document flow&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;clip: rect(0 0 0 0)&lt;/code&gt; hides the entire element&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;width: 1px; height: 1px&lt;/code&gt; makes it take up minimal space&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;overflow: hidden&lt;/code&gt; hides any overflowing content
Screen readers can still read the hidden text.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use CSS clipping to hide elements visually but keep them for screen readers&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;clip: rect(0 0 0 0)&lt;/code&gt; to hide the entire element&lt;/li&gt;
&lt;li&gt;Combine with other properties like &lt;code&gt;position&lt;/code&gt; and &lt;code&gt;overflow&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Useful for providing text alternatives or extra instructions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this simple guide helps explain CSS clipping to visually hiding an element. Let me know, if you're still confused or have any other queries.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>frontend</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Open Source 101: A Short &amp; Straight Guide for Beginner Programmers</title>
      <dc:creator>Mohammad Talim</dc:creator>
      <pubDate>Wed, 04 Oct 2023 01:41:33 +0000</pubDate>
      <link>https://dev.to/md-talim/open-source-101-a-short-straight-guide-for-beginner-programmers-58ik</link>
      <guid>https://dev.to/md-talim/open-source-101-a-short-straight-guide-for-beginner-programmers-58ik</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Diving into the world of open-source software development is pretty awesome. It's a chance to grow your tech skills and work with a global crew of folks who are just as into it as you are.&lt;/p&gt;

&lt;p&gt;Let me give you a glimpse into my open-source journey. I took on the &lt;a href="https://depths.so/events/30daysopensource" rel="noopener noreferrer"&gt;#30DaysOpenSource challenge by Depth&lt;/a&gt;. Three days in and one big lesson later, I hit a major milestone: I got my first pull request merged. It might not sound like a big deal, but it was the start of an exciting journey into the open-source universe.&lt;/p&gt;

&lt;p&gt;The pull request was just a small typo fix on a website's meta description. But this tiny change highlighted how teamwork can improve open-source projects. It was cool to see how fixing one little typo could make the project better and improve user experience.&lt;/p&gt;

&lt;p&gt;Now that I've had a taste of success, I'm excited to share my journey with other programmers who might be thinking about dipping their toes into open-source development. In this blog post, we'll go over the basics of open source, why it's important, and how you can get started.&lt;/p&gt;

&lt;p&gt;So, whether you're a newbie coder or a seasoned developer looking for a new challenge, this guide is your map to the fascinating world of open-source development. Let's jump in together and learn the key principles, tools, and strategies to become an open-source contributor.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Power of Open Source
&lt;/h2&gt;

&lt;p&gt;Open-source software is a powerful tool that promotes innovation, creativity, and collaboration. It's a platform that not only improves the quality of software but also fosters learning and skill development. As a programmer, you might be wondering why you should contribute to open source. There are many reasons for this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Contribute to Open Source?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Boost your programming skills:&lt;/strong&gt; Contributing to open source is like a continuous coding boot camp. You encounter diverse challenges, from debugging to optimizing code, which sharpen your programming skills. It's a hands-on approach to becoming a better developer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improve the software you use:&lt;/strong&gt; Imagine having the power to enhance the software tools you rely on daily. By contributing to open source, you can make improvements, fix bugs, and add features to the applications you love. It's a way to customize your digital world.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Share your expertise:&lt;/strong&gt; Everyone has unique skills and knowledge. By sharing your expertise through open source, you become part of a collective effort to create better software. Your contributions can help others learn and grow, just as you have.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build your reputation:&lt;/strong&gt; In the tech world, a strong reputation can open doors to exciting opportunities. Contributing to open source showcases your skills and commitment. Potential employers and collaborators often value this type of experience, which can boost your career.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Ways to Contribute to Open Source?
&lt;/h2&gt;

&lt;p&gt;Contrary to popular belief, you don't need to be a coding expert to contribute to open source. Even if don't know how to code, you can still contribute by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fixing typos&lt;/li&gt;
&lt;li&gt;Improving documentation&lt;/li&gt;
&lt;li&gt;Answering questions about the project&lt;/li&gt;
&lt;li&gt;Translating the project into other languages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some helpful tips for making good contributions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follow the project's style and conventions&lt;/li&gt;
&lt;li&gt;Seek feedback and learn from your mistakes&lt;/li&gt;
&lt;li&gt;Write tests for your code and make sure they pass&lt;/li&gt;
&lt;li&gt;Document your code and add comments where necessary&lt;/li&gt;
&lt;li&gt;Be respectful and courteous to everyone in the community&lt;/li&gt;
&lt;li&gt;Write clear and concise commit messages and pull descriptions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Essential Elements in Open Source Projects
&lt;/h3&gt;

&lt;p&gt;Before contributing to any open-source project, ensure they have the following elements in their project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Licence&lt;/li&gt;
&lt;li&gt;README file&lt;/li&gt;
&lt;li&gt;Code of conduct&lt;/li&gt;
&lt;li&gt;Contribution guidelines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These elements provide a framework for how the project operates and how contributors can participate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with Open Source
&lt;/h2&gt;

&lt;p&gt;Starting with open source is simple. Follow these steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Find a project that interests you:&lt;/strong&gt; The first step is finding a project that aligns with your interests and skill level. Explore GitHub, GitLab, or other platforms to discover projects that pique your curiosity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Understand project culture and guidelines:&lt;/strong&gt; Before diving in, take the time to understand the project's culture, code of conduct, and contribution guidelines. Familiarize yourself with how the community operates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fork, clone, and make changes:&lt;/strong&gt; Once you've chosen a project, fork its repository on the platform. Then, clone the forked repository to your local machine. This allows you to work on the project's code locally.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test locally and push your changes:&lt;/strong&gt; Before submitting a contribution, thoroughly test your changes locally to ensure they work as intended. Once you're confident, push your changes to your forked repository.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Create a pull request:&lt;/strong&gt; To propose your changes to the main project, create a pull request. This initiates a discussion and review process with the project maintainers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Engage with project maintainers:&lt;/strong&gt; Be open to feedback and collaborate with project maintainers to address any issues or suggestions. Effective communication is key to successful contributions.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Open source is a fantastic way to foster creativity, improve software quality, and encourage collaboration. It's a platform that allows you to enhance your skills and contribute to the software you use daily. So why wait? &lt;a href="https://finder.eddiehub.io/app" rel="noopener noreferrer"&gt;Find a project&lt;/a&gt; and Start your open-source journey today!&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>React + Tailwind CSS: The Perfect Frontend Stack for Beginners.</title>
      <dc:creator>Mohammad Talim</dc:creator>
      <pubDate>Tue, 03 Oct 2023 11:12:16 +0000</pubDate>
      <link>https://dev.to/md-talim/react-tailwind-css-the-perfect-frontend-stack-for-beginners-pbg</link>
      <guid>https://dev.to/md-talim/react-tailwind-css-the-perfect-frontend-stack-for-beginners-pbg</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Throughout the past year, I have been actively learning Frontend Development. During this time, I have come across a multitude of tutorials, tools, frameworks, and technologies. I have spent countless hours watching videos on YouTube about the "Frontend Developer Roadmap".&lt;/p&gt;

&lt;p&gt;While I initially found these resources helpful, I quickly realized that I was procrastinating by obsessing over the "perfect" stack to choose for my projects. In hindsight, I wish I had spent less time searching and more time learning and building actual projects. I hope others can learn from my experience and prioritize hands-on learning over endlessly searching for the "perfect" tools.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Best Stack
&lt;/h2&gt;

&lt;p&gt;Choosing the right front-end stack can be overwhelming for beginners. With so many frameworks and libraries to choose from, it can be hard to figure out which ones are the best fit for you.&lt;/p&gt;

&lt;p&gt;So, without further ado, here's the straightforward answer to that question: "The best frontend stack for beginners is React + TailwindCSS", which I have already mentioned in the title.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I think that for three reasons:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You don't need to be a JavaScript or CSS expert. As long as you know the basics, you can start using these frameworks.&lt;/li&gt;
&lt;li&gt;The ecosystem, tooling, and community around React are superior right now, with React Server Components, Next.js, Framer Motion, and more.&lt;/li&gt;
&lt;li&gt;And the speed that you get with Tailwindcss is just unbeatable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's explore them in some detail. If you don't like reading too much, I recommend you jump over to How to use React with TailwindCSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  React.js
&lt;/h2&gt;

&lt;p&gt;React, is a free and open-source front-end JavaScript library for building user interfaces based on components. It is maintained by Meta and a community of individual developers and companies. React can be used to develop single-page, mobile, or server-rendered applications with frameworks like Next.js.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some of the key features of React include:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Declarative programming:&lt;/strong&gt; React uses a declarative programming style, which means that you describe what you want to render on the screen, and React takes care of updating the DOM efficiently. This makes React code more readable and maintainable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Component-based architecture:&lt;/strong&gt; React applications are built up of reusable components. This makes it easy to create complex user interfaces and to share code between different parts of your application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Virtual DOM:&lt;/strong&gt; React uses a virtual DOM to efficiently update the real DOM. This makes React applications very fast and responsive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React is one of the most popular front-end development libraries in the world. It is used by companies of all sizes, including Facebook, Twitter, and Netflix.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are some of the benefits of using React:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Increased performance and scalability:&lt;/strong&gt; React applications are very fast and responsive, thanks to the use of a virtual DOM. This makes them ideal for building complex and interactive applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Improved code quality and maintainability:&lt;/strong&gt; React's declarative programming style and component-based architecture make code more readable and maintainable. This can lead to a reduction in development time and costs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large community and ecosystem:&lt;/strong&gt; React has a large and active community of developers. This means that there is a lot of support available, and there are many third-party libraries and tools that can be used with React.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Tailwind CSS
&lt;/h2&gt;

&lt;p&gt;Tailwind CSS is a utility-first CSS framework that provides a set of low-level CSS classes for building custom user interfaces. It is designed to be flexible and customizable, so you can create the exact look and feel you want for your website or application.&lt;/p&gt;

&lt;p&gt;Unlike other CSS frameworks, such as Bootstrap, Tailwind CSS does not provide any pre-built components. Instead, it provides a set of utility classes that can be used to build your components. This gives you complete control over the design and functionality of your components, and it allows you to create custom designs that are not possible with other CSS frameworks.&lt;/p&gt;

&lt;p&gt;Tailwind CSS is lightweight and efficient. It does not add unnecessary CSS to your code, which can improve the performance of your website or application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are some of the benefits of using Tailwind CSS:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Responsiveness:&lt;/strong&gt; Tailwind CSS makes it very easy to use media queries in the utility classes to make the website responsive. For example, you can use the 'sm:' directive that will apply styles only on large-screen devices rather than mobile.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility and customization:&lt;/strong&gt; Tailwind CSS is very flexible and customizable, allowing you to create exactly the look and feel you want for your website or application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Tailwind CSS is very lightweight and efficient, and it does not add any unnecessary CSS to your code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; Tailwind CSS is scalable and can be used to build websites and applications of all sizes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Community:&lt;/strong&gt; Tailwind CSS has a large and active community of developers, which means that there is a lot of support available and there are many third-party libraries and tools that can be used with Tailwind CSS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tailwind CSS is a popular choice for front-end developers of all levels and is used by companies of all sizes, including Google, Spotify, and Airbnb.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use React with Tailwind CSS
&lt;/h2&gt;

&lt;p&gt;Here is a simple code example of how to use React with Tailwind CSS:&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;// App.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./App.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="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="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&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;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* App.css */&lt;/span&gt;
&lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1200px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#ff4800&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&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 code will render a simple &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; element with the text "Hello, world!" in the center of the page. The container class is used to set the maximum width of the page and to center it horizontally. The h1 class is used to set the font size and weight of the heading.&lt;/p&gt;

&lt;p&gt;You can use Tailwind CSS to style any element on your page by adding the appropriate class names. For example, to add a blue background to the &lt;code&gt;h1&lt;/code&gt; element, you would add the &lt;code&gt;bg-blue-500&lt;/code&gt; class:&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;// App.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="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="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&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;h1&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bg-blue-500&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will render the heading with a blue background.&lt;/p&gt;

&lt;p&gt;You can also use Tailwind CSS to create responsive designs. For example, to make the heading larger on larger screens, you would add the &lt;code&gt;md:text-3xl class&lt;/code&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;// App.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="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="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&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;h1&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bg-blue-500 md:text-3xl&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will render the heading with a font size of 1.5rem on small screens and 3rem on medium and large screens.&lt;/p&gt;

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

&lt;p&gt;In conclusion, I believe that React + Tailwind CSS is the best front-end stack for beginners. It is easy to learn, fast, and flexible. If you're just starting with front-end development, I highly recommend using this stack.&lt;/p&gt;

&lt;p&gt;Of course, this is my and so many other developers' opinion. There are many other great front-end stacks out there. But if you're looking for a stack that is easy to learn, fast, and flexible, then React + Tailwind CSS is a great option.&lt;/p&gt;

&lt;p&gt;So, go ahead and give it a try! I'm sure you'll fall in love with it.&lt;/p&gt;

&lt;p&gt;P.S. If you have any questions about React or Tailwind CSS, please feel free to leave a comment below. I'm always happy to help!&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
    </item>
  </channel>
</rss>
