<?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>Forem: Prashant Sharma</title>
    <description>The latest articles on Forem by Prashant Sharma (@sharmaprash).</description>
    <link>https://forem.com/sharmaprash</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1186196%2F35eaa801-59b9-4bfd-bbd2-4e7e9cdc2f7c.png</url>
      <title>Forem: Prashant Sharma</title>
      <link>https://forem.com/sharmaprash</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sharmaprash"/>
    <language>en</language>
    <item>
      <title>Designing High Throughput Go Services for Continuous Database Change Streams</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Thu, 11 Dec 2025 21:24:47 +0000</pubDate>
      <link>https://forem.com/sharmaprash/golang-optimizations-for-high-volume-services-dij</link>
      <guid>https://forem.com/sharmaprash/golang-optimizations-for-high-volume-services-dij</guid>
      <description>&lt;p&gt;Modern backend systems these days are basically on a permanent caffeine drip, constantly streaming real time data, reshaping it mid-flight, and shoving it into search engines or analytics stores like it’s speed running ETL. And because the universe likes to keep engineers humble, the stream isn’t just continuous; it’s potentially infinite. Yes, infinite. Like those “quick fixes” that turn into 4 hour debugging sessions.&lt;/p&gt;

&lt;p&gt;So your service architecture?&lt;br&gt;
Yeah, it needs to stay calm while the traffic graph looks like it’s trying to escape orbit.&lt;/p&gt;

&lt;p&gt;This article walks through how to build memory efficient, low latency Go services that slurp up database change events (like from a logical replication feed), transform them without crying, and ship them off to something like Elastic search. All of this comes from real systems that run 24/7 without the luxury of “just restart it, maybe it’ll fix itself.”&lt;/p&gt;

&lt;p&gt;If you’re building something that needs to stay fast, predictable, and not throw a GC tantrum under load then you’re in the right place.&lt;/p&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;1. The Challenge: Infinite Input, Finite Resources&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;The hardest part of streaming pipelines is that the source never stops. As long as writes occur in the primary database, new events keep arriving. If your consumer slows down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;upstream storage (e.g., WAL segments) may accumulate&lt;/li&gt;
&lt;li&gt;downstream indexing may exert backpressure&lt;/li&gt;
&lt;li&gt;your Go service may buffer too much data, growing the heap&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The engineering goal is to &lt;strong&gt;turn an unbounded stream into a bounded, measurable flow.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This requires:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;limiting concurrency&lt;/li&gt;
&lt;li&gt;keeping memory usage predictable&lt;/li&gt;
&lt;li&gt;ensuring transformations are allocation efficient&lt;/li&gt;
&lt;li&gt;making GC behavior stable&lt;/li&gt;
&lt;li&gt;avoiding burst triggered latency spikes&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;2. Reducing JSON Overhead: Faster Encoders &amp;amp; Allocation Awareness&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Streaming databases into search engines almost always involves heavy JSON serialization. Go’s standard &lt;code&gt;encoding/json&lt;/code&gt; package is reliable but not optimised for high frequency encoding of similar structs.&lt;/p&gt;

&lt;p&gt;Many high throughput services use alternative JSON engines like &lt;code&gt;jsoniter&lt;/code&gt;, primarily because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it performs less reflection&lt;/li&gt;
&lt;li&gt;it caches metadata&lt;/li&gt;
&lt;li&gt;it produces fewer allocations&lt;/li&gt;
&lt;li&gt;it is optimized for repetitive encoding patterns (common in bulk indexing)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;When using alternative encoders, be aware of:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;differences in how null and empty fields are handled&lt;/li&gt;
&lt;li&gt;incompatibilities with unusual JSON tags&lt;/li&gt;
&lt;li&gt;behavior with libraries that implement custom marshaling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good rule of thumb: &lt;strong&gt;avoid exotic JSON tags&lt;/strong&gt; and stick to &lt;code&gt;omitempty&lt;/code&gt; when aiming for consistent, allocator friendly serialization.&lt;/p&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;3. Reducing Allocations with &lt;code&gt;sync.Pool&lt;/code&gt;&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Even with a fast JSON encoder, memory churn can overwhelm the GC when processing millions of events per hour. Many objects in a pipeline are short lived:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;structs representing change events&lt;/li&gt;
&lt;li&gt;scratch buffers for JSON encoding&lt;/li&gt;
&lt;li&gt;temporary slices created during transformations&lt;/li&gt;
&lt;li&gt;objects used to assemble bulk indexing payloads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;sync.Pool&lt;/code&gt; helps reduce heap pressure by reusing these frequently allocated objects.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Good candidates for pooling:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;bytes.Buffer&lt;/code&gt; instances&lt;/li&gt;
&lt;li&gt;small structs reused per event&lt;/li&gt;
&lt;li&gt;reusable &lt;code&gt;[]byte&lt;/code&gt; scratch buffers&lt;/li&gt;
&lt;li&gt;transformation helpers that are stateless and easy to reset&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Guidelines for safe pooling:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;always reset objects before putting them back&lt;/li&gt;
&lt;li&gt;avoid pooling anything that holds references to external resources&lt;/li&gt;
&lt;li&gt;don’t pool objects with complex, stateful lifecycles&lt;/li&gt;
&lt;li&gt;be mindful that pooled objects may be garbage collected when idle&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Used responsibly, pooling cuts down on heap traffic and dramatically smooths GC behavior.&lt;/p&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;4. Designing a Stable Pipeline with Bounded Concurrency&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Unlimited parallelism is not your friend. A robust ingestion pipeline needs &lt;strong&gt;explicit limits&lt;/strong&gt; on concurrency and buffering to maintain control over memory and CPU.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Key architectural limits:&lt;/strong&gt;
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;A. Controlled goroutine counts&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Limit reads, transformations, and indexing workers. More workers do not always mean higher throughput especially when downstream systems throttle.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;B. A bounded internal queue&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;A well sized channel helps absorb brief bursts while preventing runaway memory usage.&lt;/p&gt;

&lt;p&gt;When the internal buffer fills, backpressure should propagate upward so ingestion slows gracefully, not catastrophically.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;C. Measured batching for downstream systems&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;For something like Elasticsearch bulk indexing, optimal batches are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;large enough to amortize overhead&lt;/li&gt;
&lt;li&gt;small enough to avoid memory spikes&lt;/li&gt;
&lt;li&gt;frequent enough to keep indexing pipelines busy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can tune:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;max batch size in bytes&lt;/li&gt;
&lt;li&gt;max number of actions&lt;/li&gt;
&lt;li&gt;concurrency of bulk workers&lt;/li&gt;
&lt;li&gt;flush interval&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finding the sweet spot is essential for predictable latency.&lt;/p&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;5. Managing Garbage Collection Behavior&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Even after cutting allocations, GC behavior plays a major role in real time service performance.&lt;/p&gt;

&lt;p&gt;Modern Go releases continuously improve GC, and newer experimental garbage collectors (like GreenTea GC) aim to make GC cycles more incremental and less bursty.&lt;/p&gt;

&lt;p&gt;These experimental modes often:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;smooth out pause times&lt;/li&gt;
&lt;li&gt;reduce tail latency&lt;/li&gt;
&lt;li&gt;trade slightly higher memory usage for better throughput stability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But GC tuning should happen only &lt;strong&gt;after&lt;/strong&gt; you’ve reduced unnecessary allocations. No amount of tuning can fix an allocator heavy design.&lt;/p&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;6. Additional Practical Optimization Techniques&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Beyond major architectural decisions, several smaller improvements add up in high volume systems:&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;A. Preallocate slices and buffers&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Avoid repeated reallocation during batch building:&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;buf&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;B. Prefer strongly typed structs over &lt;code&gt;map[string]interface{}&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Maps and interfaces generate both heap churn and serialization overhead.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;C. Avoid unnecessary conversions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;String/byte conversions and intermediate allocations add up significantly over millions of events.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;D. Instrument everything&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Monitoring should include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;queue depth&lt;/li&gt;
&lt;li&gt;event processing duration&lt;/li&gt;
&lt;li&gt;GC cycles and heap size&lt;/li&gt;
&lt;li&gt;JSON serialization latency&lt;/li&gt;
&lt;li&gt;bulk indexer success/failure rates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without visibility, performance tuning is guesswork.&lt;/p&gt;




&lt;h1&gt;
  
  
  &lt;strong&gt;7. What a Stable End-to-End Pipeline Looks Like&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;A well-designed ingestion pipeline is characterized by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A fixed number of ingestion goroutines&lt;/strong&gt;, ensuring predictable load.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A bounded queue&lt;/strong&gt; to prevent heap explosion during bursts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficient, low-allocation transformations and JSON encoding&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reuse of buffers and structs&lt;/strong&gt; via &lt;code&gt;sync.Pool&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tuned batching and concurrency settings&lt;/strong&gt; for downstream systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GC behavior that no longer creates latency spikes&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The outcome is a system that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;keeps up with continuous database updates&lt;/li&gt;
&lt;li&gt;avoids backpressure cascades&lt;/li&gt;
&lt;li&gt;delivers consistent throughput&lt;/li&gt;
&lt;li&gt;maintains predictable memory usage&lt;/li&gt;
&lt;li&gt;remains stable even during write-intensive traffic surges&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Building high-throughput Go services for continuous change streams requires more than fast code, it requires discipline in memory management, concurrency boundaries, and predictable downstream interaction. With careful JSON optimization, strategic use of &lt;code&gt;sync.Pool&lt;/code&gt;, disciplined pipeline architecture, and targeted GC tuning, you can build systems that stay fast and stable no matter how aggressively upstream data grows.&lt;/p&gt;

</description>
      <category>performance</category>
      <category>backend</category>
      <category>go</category>
      <category>architecture</category>
    </item>
    <item>
      <title>DuckDB: how to fine tune parameters?</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Thu, 11 Dec 2025 18:18:25 +0000</pubDate>
      <link>https://forem.com/sharmaprash/duckdb-how-to-fine-tune-parameters-39h8</link>
      <guid>https://forem.com/sharmaprash/duckdb-how-to-fine-tune-parameters-39h8</guid>
      <description>&lt;p&gt;DuckDB has rapidly gained popularity as an in-process, analytical database that delivers blazing-fast SQL queries without requiring a separate server. While its out-of-the-box performance is impressive, fine-tuning its parameters can unlock even greater efficiency—especially for complex workloads.&lt;/p&gt;

&lt;p&gt;In this guide, we’ll explore how to optimize DuckDB’s configuration based on insights from &lt;a href="https://stackoverflow.com/questions/798444106/duckdb-how-to-fine-tune-parameters" rel="noopener noreferrer"&gt;this StackOverflow discussion&lt;/a&gt; and best practices. Whether you're processing large datasets or running embedded analytics, these tweaks can make a noticeable difference.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why Fine-Tune DuckDB?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;DuckDB is designed to work well with default settings, but customizing its behavior can help in scenarios like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory constraints&lt;/strong&gt; (e.g., avoiding OOM errors)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query performance&lt;/strong&gt; (e.g., faster joins, aggregations)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency &amp;amp; parallelism&lt;/strong&gt; (e.g., multi-threaded workloads)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s dive into key parameters and how to adjust them.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Key DuckDB Parameters to Optimize&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Memory Management&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;DuckDB is memory-efficient, but large queries may require adjustments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;memory_limit&lt;/code&gt;&lt;/strong&gt;
Sets the maximum memory DuckDB can use (default: ~80% of system RAM).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;memory_limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'4GB'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Adjust based on your workload&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Takeaway:&lt;/em&gt; Reduce this if running in constrained environments (e.g., containers).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;temp_directory&lt;/code&gt;&lt;/strong&gt;
Offloads temporary files to disk if memory is tight.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;temp_directory&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'/path/to/temp'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;2. Parallelism &amp;amp; Threading&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;DuckDB leverages multi-threading, but you can control it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;threads&lt;/code&gt;&lt;/strong&gt;
Limits the number of threads (default: auto-detected).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Useful for CPU-bound workloads&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Takeaway:&lt;/em&gt; Reduce threads if running alongside other CPU-heavy tasks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;enable_progress_bar&lt;/code&gt;&lt;/strong&gt;
Disabling this can slightly improve performance in scripts.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;enable_progress_bar&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;3. Query Execution Tweaks&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Optimize how DuckDB processes data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;enable_external_access&lt;/code&gt;&lt;/strong&gt;
Controls file system access (disable for security).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;enable_external_access&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;enable_object_cache&lt;/code&gt;&lt;/strong&gt;
Caches parsed queries (useful for repeated executions).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;enable_object_cache&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;enable_verify_parallelism&lt;/code&gt;&lt;/strong&gt;
Validates parallel query plans (disable for minor speed gains).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;enable_verify_parallelism&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;4. Storage &amp;amp; I/O&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Improve read/write performance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;checkpoint_threshold&lt;/code&gt;&lt;/strong&gt;
Adjusts how often WAL (Write-Ahead Log) checkpoints occur.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;checkpoint_threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'100MB'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Default: 16MB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;file_compression&lt;/code&gt;&lt;/strong&gt;
Compresses data files (trade-off: CPU vs. disk usage).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;  &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;file_compression&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'zstd'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- Options: 'zstd', 'lz4', 'none'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Best Practices for Fine-Tuning&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Profile First&lt;/strong&gt;
Use &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; to identify bottlenecks before tweaking.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;ANALYZE&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;large_table&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;complex_condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Start Conservative&lt;/strong&gt;&lt;br&gt;
Adjust one parameter at a time and measure impact.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Environment-Specific Tuning&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cloud/Containers:&lt;/strong&gt; Lower &lt;code&gt;memory_limit&lt;/code&gt;, disable &lt;code&gt;enable_external_access&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High-Performance Workloads:&lt;/strong&gt; Increase &lt;code&gt;threads&lt;/code&gt;, enable &lt;code&gt;object_cache&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Persistent Configs&lt;/strong&gt;&lt;br&gt;
Save settings in a &lt;code&gt;.duckdbrc&lt;/code&gt; file for reuse:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;   &lt;span class="c1"&gt;-- .duckdbrc&lt;/span&gt;
   &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;memory_limit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'2GB'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;DuckDB’s flexibility makes it a powerful tool for embedded analytics, but fine-tuning its parameters can significantly boost performance. Start with memory and threading, then experiment with query execution and storage settings based on your workload.&lt;/p&gt;

&lt;p&gt;For more details, check the &lt;a href="https://duckdb.org/docs/configuration" rel="noopener noreferrer"&gt;official DuckDB docs&lt;/a&gt; and the &lt;a href="https://stackoverflow.com/questions/798444106/duckdb-how-to-fine-tune-parameters" rel="noopener noreferrer"&gt;StackOverflow discussion&lt;/a&gt; that inspired this guide.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy querying!&lt;/strong&gt; 🚀&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>tutorial</category>
      <category>sql</category>
    </item>
    <item>
      <title>Is GCC right when it accepts a C++ template struct having a member with a wrong default initializer?</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Thu, 11 Dec 2025 18:15:28 +0000</pubDate>
      <link>https://forem.com/sharmaprash/is-gcc-right-when-it-accepts-a-c-template-struct-having-a-member-with-a-wrong-default-initializer-4067</link>
      <guid>https://forem.com/sharmaprash/is-gcc-right-when-it-accepts-a-c-template-struct-having-a-member-with-a-wrong-default-initializer-4067</guid>
      <description>&lt;p&gt;Have you ever written C++ template code that compiled fine with GCC but failed with Clang or MSVC? You're not alone. A recent &lt;a href="https://stackoverflow.com/questions/79844065/is-gcc-right-when-it-accepts-a-c-template-struct-having-a-member-with-a-wrong" rel="noopener noreferrer"&gt;StackOverflow question&lt;/a&gt; highlights a subtle but important compiler discrepancy that every C++ developer should understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem: When GCC Accepts Invalid Code
&lt;/h2&gt;

&lt;p&gt;Consider this template struct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Default initializer&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, this looks reasonable. But what if &lt;code&gt;T&lt;/code&gt; is a type that can't be initialized with &lt;code&gt;1&lt;/code&gt;? For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;S&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Should this compile?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GCC says yes. Clang and MSVC say no. Who's right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Standard
&lt;/h2&gt;

&lt;p&gt;The C++ standard ([dcl.init]/11.6) states that default member initializers must be valid for the type. When &lt;code&gt;T&lt;/code&gt; is &lt;code&gt;std::string&lt;/code&gt;, &lt;code&gt;T x = 1&lt;/code&gt; is clearly invalid - you can't initialize a string with an integer.&lt;/p&gt;

&lt;p&gt;However, GCC implements a controversial extension: it only checks the validity of default initializers when they're actually used, not when the template is instantiated. This is why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;S&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// OK - int can be initialized with 1&lt;/span&gt;
&lt;span class="n"&gt;S&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// GCC accepts this, but shouldn't&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Portability Issues&lt;/strong&gt;: Code that compiles with GCC may fail with other compilers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Silent Bugs&lt;/strong&gt;: Invalid initializers might go unnoticed until they're actually used&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standard Compliance&lt;/strong&gt;: GCC's behavior violates the C++ standard&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Developer Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Be Explicit&lt;/strong&gt;: Always ensure your default initializers are valid for all possible template arguments&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Static Assertions&lt;/strong&gt;: Add &lt;code&gt;static_assert&lt;/code&gt; checks to validate template arguments&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test Multiple Compilers&lt;/strong&gt;: Don't rely on GCC's permissive behavior&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consider &lt;code&gt;-pedantic&lt;/code&gt;&lt;/strong&gt;: Use GCC's strict mode to catch these issues&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typename&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;S&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Better: explicit construction&lt;/span&gt;
    &lt;span class="c1"&gt;// Or:&lt;/span&gt;
    &lt;span class="k"&gt;static_assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;is_constructible_v&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                 &lt;span class="s"&gt;"T must be constructible from int"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;T&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;While GCC's behavior might seem convenient, it can lead to subtle bugs and portability issues. As C++ developers, we should strive for standard-compliant code that works across all major compilers. Understanding these compiler quirks helps us write more robust template code.&lt;/p&gt;

&lt;p&gt;Have you encountered similar compiler discrepancies? Share your experiences in the comments!&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>cpp</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Is there a way to determine if a shared object ( .so ) file was generated from a c or c++ code?</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Thu, 11 Dec 2025 18:10:26 +0000</pubDate>
      <link>https://forem.com/sharmaprash/is-there-a-way-to-determine-if-a-shared-object-so-file-was-generated-from-a-c-or-c-code-4a2h</link>
      <guid>https://forem.com/sharmaprash/is-there-a-way-to-determine-if-a-shared-object-so-file-was-generated-from-a-c-or-c-code-4a2h</guid>
      <description>&lt;p&gt;Shared object files (&lt;code&gt;.so&lt;/code&gt;) are the backbone of dynamic linking in Linux and Unix systems. They allow programs to share code libraries efficiently, reducing memory usage and enabling modular development. But have you ever wondered whether a &lt;code&gt;.so&lt;/code&gt; file was compiled from C or C++ source code?&lt;/p&gt;

&lt;p&gt;This question, &lt;a href="https://stackoverflow.com/questions/79835703/is-there-a-way-to-determine-if-a-shared-object-so-file-was-generated-from-a" rel="noopener noreferrer"&gt;asked on StackOverflow&lt;/a&gt;, is more nuanced than it seems. While there’s no foolproof method, several techniques can help you make an educated guess. Let’s explore them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Does It Matter?
&lt;/h2&gt;

&lt;p&gt;Before diving into solutions, let’s understand why this distinction is useful:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Debugging &amp;amp; Reverse Engineering&lt;/strong&gt; – Knowing the original language helps when analyzing binaries or debugging crashes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dependency Management&lt;/strong&gt; – Some tools or scripts may behave differently based on the language.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security Audits&lt;/strong&gt; – C++ binaries might use different runtime features (e.g., exceptions, RTTI) than C.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compatibility Checks&lt;/strong&gt; – Ensuring ABI (Application Binary Interface) compatibility between libraries.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Methods to Identify the Source Language
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Check for C++-Specific Symbols&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;C++ introduces name mangling to support function overloading, namespaces, and classes. Tools like &lt;code&gt;nm&lt;/code&gt; or &lt;code&gt;objdump&lt;/code&gt; can reveal these mangled names.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using &lt;code&gt;nm&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;bash&lt;br&gt;
nm -D /path/to/library.so | grep -E '(&lt;em&gt;Z|&lt;/em&gt;_cxa)'&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;_Z&lt;/code&gt; prefixes indicate mangled C++ symbols (e.g., &lt;code&gt;_Z3fooi&lt;/code&gt; for &lt;code&gt;foo(int)&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__cxa&lt;/code&gt; symbols (e.g., &lt;code&gt;__cxa_throw&lt;/code&gt;) are part of the C++ ABI for exceptions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Using &lt;code&gt;objdump&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;bash&lt;br&gt;
objdump -tT /path/to/library.so | grep -E '(&lt;em&gt;Z|&lt;/em&gt;_cxa)'&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway&lt;/strong&gt;: If you see mangled names or C++ ABI symbols, the &lt;code&gt;.so&lt;/code&gt; was likely compiled from C++.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Inspect the ELF Header and Sections&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;ELF (Executable and Linkable Format) files contain metadata that can hint at the language.&lt;/p&gt;

&lt;h4&gt;
  
  
  Using &lt;code&gt;readelf&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;bash&lt;br&gt;
readelf -h /path/to/library.so&lt;/p&gt;

&lt;p&gt;Look for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.comment&lt;/code&gt; section&lt;/strong&gt;: May contain compiler info (e.g., &lt;code&gt;GCC: (GNU) 11.3.0&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;.gnu.debuglink&lt;/code&gt;&lt;/strong&gt;: Debug symbols might reveal source files (&lt;code&gt;.cpp&lt;/code&gt; vs &lt;code&gt;.c&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Using &lt;code&gt;file&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;bash&lt;br&gt;
file /path/to/library.so&lt;/p&gt;

&lt;p&gt;Output like &lt;code&gt;ELF 64-bit LSB shared object, x86-64&lt;/code&gt; won’t directly reveal the language, but it’s a starting point.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway&lt;/strong&gt;: Compiler metadata can indirectly suggest the language, but it’s not definitive.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. &lt;strong&gt;Analyze Runtime Dependencies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;C++ binaries often link against &lt;code&gt;libstdc++&lt;/code&gt; (GCC) or &lt;code&gt;libc++&lt;/code&gt; (LLVM).&lt;/p&gt;

&lt;h4&gt;
  
  
  Using &lt;code&gt;ldd&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;bash&lt;br&gt;
ldd /path/to/library.so | grep -E '(libstdc++|libc++)'&lt;/p&gt;

&lt;p&gt;If these libraries appear, the &lt;code&gt;.so&lt;/code&gt; was likely compiled from C++.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway&lt;/strong&gt;: Dynamic linker dependencies are a strong indicator of C++.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. &lt;strong&gt;Check for C++-Specific Features&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;C++ introduces features absent in C, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Exceptions&lt;/strong&gt;: Look for &lt;code&gt;__cxa_throw&lt;/code&gt; or &lt;code&gt;__cxa_begin_catch&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RTTI (Run-Time Type Information)&lt;/strong&gt;: Symbols like &lt;code&gt;_ZTI&lt;/code&gt; (typeinfo) or &lt;code&gt;_ZTV&lt;/code&gt; (vtable).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Standard Library Usage&lt;/strong&gt;: Strings (&lt;code&gt;std::string&lt;/code&gt;), containers (&lt;code&gt;std::vector&lt;/code&gt;), etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Using &lt;code&gt;strings&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;bash&lt;br&gt;
strings /path/to/library.so | grep -E '(std::|_ZTI|_ZTV)'&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway&lt;/strong&gt;: These features are almost exclusively C++.&lt;/p&gt;




&lt;h3&gt;
  
  
  5. &lt;strong&gt;Use Binary Analysis Tools&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Advanced tools like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ghidra&lt;/strong&gt; (NSA’s reverse engineering tool)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IDA Pro&lt;/strong&gt; (commercial disassembler)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;radare2&lt;/strong&gt; (open-source framework)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These can decompile sections of the &lt;code&gt;.so&lt;/code&gt; and reveal language-specific patterns (e.g., C++ class layouts, virtual tables).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Takeaway&lt;/strong&gt;: For deep analysis, these tools are invaluable but require expertise.&lt;/p&gt;




&lt;h2&gt;
  
  
  Limitations and Caveats
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Mixed-Language Binaries&lt;/strong&gt;: A &lt;code&gt;.so&lt;/code&gt; might contain both C and C++ code (e.g., a C++ library with C-compatible APIs).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compiler Optimizations&lt;/strong&gt;: Aggressive optimizations can obfuscate symbols.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stripped Binaries&lt;/strong&gt;: Debug symbols might be removed, hiding clues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Extern "C" Blocks&lt;/strong&gt;: C++ code wrapped in &lt;code&gt;extern "C"&lt;/code&gt; avoids name mangling.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Pro Tip&lt;/strong&gt;: Combine multiple methods for higher confidence.&lt;/p&gt;




&lt;h2&gt;
  
  
  Developer Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;For Debugging&lt;/strong&gt;: Start with &lt;code&gt;nm&lt;/code&gt; and &lt;code&gt;ldd&lt;/code&gt; for quick checks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For Security&lt;/strong&gt;: Use &lt;code&gt;readelf&lt;/code&gt; and &lt;code&gt;strings&lt;/code&gt; to audit dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For Reverse Engineering&lt;/strong&gt;: Leverage Ghidra or IDA Pro for deep dives.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;For Automation&lt;/strong&gt;: Script these checks (e.g., &lt;code&gt;grep&lt;/code&gt; for &lt;code&gt;_Z&lt;/code&gt; in &lt;code&gt;nm&lt;/code&gt; output).&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;While there’s no 100% reliable way to determine if a &lt;code&gt;.so&lt;/code&gt; file was generated from C or C++, combining symbol analysis, ELF metadata, and runtime dependencies can give you a strong indication. C++ leaves more distinctive fingerprints (mangled names, RTTI, exceptions), whereas C binaries tend to be simpler.&lt;/p&gt;

&lt;p&gt;Happy hacking! 🚀&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>linux</category>
      <category>programming</category>
    </item>
    <item>
      <title>Top 10 Productivity Hacks Every Developer Should Know 🚀</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Thu, 11 Dec 2025 18:03:41 +0000</pubDate>
      <link>https://forem.com/sharmaprash/top-10-productivity-hacks-every-developer-should-know-5ek1</link>
      <guid>https://forem.com/sharmaprash/top-10-productivity-hacks-every-developer-should-know-5ek1</guid>
      <description>&lt;p&gt;As developers, we're constantly juggling deadlines, complex problems, and ever-evolving technologies. The difference between a productive day and a frustrating one often comes down to small, intentional habits. In this article, we'll explore &lt;strong&gt;10 battle-tested productivity hacks&lt;/strong&gt; that can transform your workflow, reduce burnout, and help you write better code—faster.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Master Your IDE Shortcuts ⌨️
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; IDEs like VS Code, IntelliJ, or Sublime Text are packed with shortcuts that can save hours of repetitive work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key shortcuts to learn:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multi-cursor editing&lt;/strong&gt; (Ctrl+D / Cmd+D)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quick file navigation&lt;/strong&gt; (Ctrl+P / Cmd+P)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminal integration&lt;/strong&gt; (Ctrl+&lt;code&gt;/ Cmd+&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Developer takeaway:&lt;/strong&gt; Spend 10 minutes daily practicing shortcuts—it’s an investment that pays off exponentially.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Automate Repetitive Tasks 🤖
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; Manual tasks like testing, deployments, or file organization drain mental energy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tools to automate:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Git hooks&lt;/strong&gt; for pre-commit checks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CI/CD pipelines&lt;/strong&gt; (GitHub Actions, GitLab CI)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scripts&lt;/strong&gt; (Bash, Python) for routine operations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Trend:&lt;/strong&gt; AI-powered tools like &lt;strong&gt;GitHub Copilot&lt;/strong&gt; are now automating even code generation.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. The Pomodoro Technique for Deep Work ⏳
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why it works:&lt;/strong&gt; 25-minute focused sprints + 5-minute breaks align with the brain’s natural attention span.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro tip:&lt;/strong&gt; Use apps like &lt;a href="https://www.focustodo.cn/" rel="noopener noreferrer"&gt;Focus To-Do&lt;/a&gt; or &lt;a href="https://pomotodo.com/" rel="noopener noreferrer"&gt;Pomotodo&lt;/a&gt; to track sessions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Developer insight:&lt;/strong&gt; Pair Pomodoros with &lt;strong&gt;time-blocking&lt;/strong&gt; for maximum efficiency.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Optimize Your Workspace for Flow 🪑
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Ergonomics matter:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dual monitors&lt;/strong&gt; reduce context-switching&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mechanical keyboards&lt;/strong&gt; (e.g., Keychron) improve typing speed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blue light filters&lt;/strong&gt; (f.lux) reduce eye strain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Remote work hack:&lt;/strong&gt; Use &lt;strong&gt;virtual desktops&lt;/strong&gt; to separate projects.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Learn to Debug Like a Pro 🐛
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Time-savers:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Console.log() alternatives:&lt;/strong&gt; &lt;code&gt;debugger;&lt;/code&gt; statements, Chrome DevTools&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error tracking tools:&lt;/strong&gt; Sentry, Rollbar&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AI-assisted debugging:&lt;/strong&gt; Tools like &lt;strong&gt;Cursor&lt;/strong&gt; or &lt;strong&gt;Amazon CodeWhisperer&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Takeaway:&lt;/strong&gt; Debugging is 80% of development—optimize it.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. The Two-Minute Rule for Tasks ✅
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Rule:&lt;/strong&gt; If a task takes &amp;lt;2 minutes, do it immediately.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Fixing a typo in docs&lt;/li&gt;
&lt;li&gt;Replying to a quick Slack message&lt;/li&gt;
&lt;li&gt;Merging a trivial PR&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why it works:&lt;/strong&gt; Prevents small tasks from piling up into mental clutter.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Use a Task Manager (But Keep It Simple) 📝
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Top picks:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Linear&lt;/strong&gt; (for dev teams)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Todoist&lt;/strong&gt; (for solo devs)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Notion&lt;/strong&gt; (for docs + tasks)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Anti-pattern:&lt;/strong&gt; Over-engineering your system. Stick to &lt;strong&gt;one tool&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Batch Similar Tasks Together 📦
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Group by context:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code reviews&lt;/strong&gt; (1-hour block)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Meetings&lt;/strong&gt; (back-to-back)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learning&lt;/strong&gt; (dedicated time for tutorials)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Science-backed:&lt;/strong&gt; Reduces &lt;strong&gt;context-switching overhead&lt;/strong&gt; by 40%.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Document as You Code 📖
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Why it’s a hack:&lt;/strong&gt; Writing docs &lt;em&gt;during&lt;/em&gt; development forces clarity.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Markdown&lt;/strong&gt; (for READMEs)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Swagger/OpenAPI&lt;/strong&gt; (for APIs)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Architecture Decision Records (ADRs)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Bonus:&lt;/strong&gt; Future-you will thank you.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Protect Your Mental Energy 🧠
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Non-negotiables:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No meetings before 10 AM&lt;/strong&gt; (deep work hours)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Email batching&lt;/strong&gt; (check 2x/day)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Digital detox&lt;/strong&gt; (e.g., no phone after 8 PM)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Trend:&lt;/strong&gt; More devs are adopting &lt;strong&gt;"async-first"&lt;/strong&gt; communication.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion: Small Hacks, Big Impact
&lt;/h2&gt;

&lt;p&gt;Productivity isn’t about working harder—it’s about working &lt;em&gt;smarter&lt;/em&gt;. Start with &lt;strong&gt;one or two hacks&lt;/strong&gt; from this list, measure their impact, and iterate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your turn:&lt;/strong&gt; Which hack will you try first? Share your favorites in the comments! 👇&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Happy coding! 🚀&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How I Cut My Debugging Time in Half as a Front-End Developer (A Practical Guide)</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Thu, 11 Dec 2025 17:41:11 +0000</pubDate>
      <link>https://forem.com/sharmaprash/weekly-developer-trends-2025-12-11-55c6</link>
      <guid>https://forem.com/sharmaprash/weekly-developer-trends-2025-12-11-55c6</guid>
      <description>&lt;p&gt;Debugging is an inevitable part of front-end development, but it doesn’t have to be a time-consuming nightmare. After years of trial and error, I’ve refined my debugging workflow to be &lt;strong&gt;twice as fast&lt;/strong&gt;—without sacrificing accuracy. In this guide, I’ll share the tools, techniques, and mindset shifts that helped me slash debugging time while improving code quality.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Debugging Feels Like a Black Hole (And How to Escape It)
&lt;/h2&gt;

&lt;p&gt;Before diving into solutions, let’s address why debugging often feels endless:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Lack of Structure&lt;/strong&gt; – Jumping between console logs, breakpoints, and Stack Overflow without a plan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over-Reliance on Guesswork&lt;/strong&gt; – Making changes blindly instead of isolating the root cause.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tool Underutilization&lt;/strong&gt; – Not leveraging modern dev tools to their full potential.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reactive Debugging&lt;/strong&gt; – Waiting for bugs to appear instead of preventing them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The key? &lt;strong&gt;Systematic debugging&lt;/strong&gt;—a repeatable process that minimizes guesswork.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Reproduce the Bug Reliably
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;"If you can’t reproduce it, you can’t fix it."&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Techniques to Reproduce Bugs Faster&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Isolate the Component&lt;/strong&gt; – Use tools like &lt;a href="https://storybook.js.org/" rel="noopener noreferrer"&gt;Storybook&lt;/a&gt; or &lt;a href="https://codesandbox.io/" rel="noopener noreferrer"&gt;CodeSandbox&lt;/a&gt; to test components in isolation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Record User Flows&lt;/strong&gt; – Tools like &lt;a href="https://sentry.io/" rel="noopener noreferrer"&gt;Sentry&lt;/a&gt; or &lt;a href="https://logrocket.com/" rel="noopener noreferrer"&gt;LogRocket&lt;/a&gt; capture real user sessions to replay bugs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automate Reproduction&lt;/strong&gt; – Write a minimal test case (e.g., with &lt;a href="https://www.cypress.io/" rel="noopener noreferrer"&gt;Cypress&lt;/a&gt; or &lt;a href="https://playwright.dev/" rel="noopener noreferrer"&gt;Playwright&lt;/a&gt;) to trigger the bug consistently.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pro Tip:&lt;/strong&gt; If the bug is intermittent, check for race conditions (e.g., async state updates, network delays).&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Narrow Down the Scope with Binary Debugging
&lt;/h2&gt;

&lt;p&gt;Instead of scanning every line of code, use &lt;strong&gt;divide-and-conquer&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Comment Out Half the Code&lt;/strong&gt; – If the bug disappears, the issue is in the commented section. Repeat until you find the culprit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;debugger&lt;/code&gt; Strategically&lt;/strong&gt; – Place &lt;code&gt;debugger&lt;/code&gt; statements in key areas (e.g., event handlers, state updates) to pause execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leverage Source Maps&lt;/strong&gt; – Ensure your bundler (Webpack, Vite, etc.) generates source maps for accurate breakpoints in production-like builds.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Before: Random console logs everywhere&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;State:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;state&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Props:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// After: Targeted debugging&lt;/span&gt;
&lt;span class="k"&gt;debugger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Pauses only when state.user is null&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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;User not loaded!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&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;
  
  
  Step 3: Master Your Browser DevTools
&lt;/h2&gt;

&lt;p&gt;Most developers only scratch the surface of DevTools. Here’s how to go deeper:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Chrome DevTools Power Moves&lt;/strong&gt;
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;th&gt;How It Saves Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Event Listener Breakpoints&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Debugging event-driven bugs (e.g., clicks, scrolls)&lt;/td&gt;
&lt;td&gt;Pause when a specific event fires.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Network Throttling&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Testing slow network conditions&lt;/td&gt;
&lt;td&gt;Reproduce race conditions easily.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance Tab&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Identifying render bottlenecks&lt;/td&gt;
&lt;td&gt;Spot forced re-renders or layout shifts.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Overlay Rulers&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;CSS/alignment issues&lt;/td&gt;
&lt;td&gt;Visualize padding, margins, and flexbox gaps.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Hidden Gem:&lt;/strong&gt; Use &lt;code&gt;monitorEvents($0)&lt;/code&gt; in the console to log all events on a selected DOM element.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4: Automate Error Detection
&lt;/h2&gt;

&lt;p&gt;Prevent bugs before they reach debugging:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Static Analysis Tools&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ESLint + Prettier&lt;/strong&gt; – Catch syntax errors and enforce consistency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TypeScript&lt;/strong&gt; – Eliminate entire classes of runtime errors (e.g., &lt;code&gt;undefined&lt;/code&gt; props).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React Strict Mode&lt;/strong&gt; – Highlight unsafe lifecycle methods and side effects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Runtime Checks&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;React Error Boundaries&lt;/strong&gt; – Gracefully handle UI crashes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Hooks for Validation&lt;/strong&gt; – Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;useDebugValue&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="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;useDebugValue&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="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 5: Debug Like a Detective, Not a Gambler
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The 5 Whys Technique&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Ask "why" five times to uncover the root cause:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Why did the button not work?&lt;/strong&gt; → The &lt;code&gt;onClick&lt;/code&gt; handler wasn’t called.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why wasn’t it called?&lt;/strong&gt; → The event listener was overwritten.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why was it overwritten?&lt;/strong&gt; → A third-party script modified the DOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Why did the script run?&lt;/strong&gt; → It was loaded without &lt;code&gt;defer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Root Cause:&lt;/strong&gt; Missing &lt;code&gt;defer&lt;/code&gt; attribute in the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Rubber Duck Debugging&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Explain the problem out loud (or to a rubber duck). Often, the solution reveals itself mid-explanation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 6: Build a Debugging Toolkit
&lt;/h2&gt;

&lt;p&gt;Curate a set of go-to tools for different scenarios:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Tool/Technique&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;State Management Bugs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Redux DevTools, React Query DevTools&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;CSS Issues&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;a href="https://getcssscan.com/" rel="noopener noreferrer"&gt;CSS Scanner&lt;/a&gt;, Browser’s "Elements" tab&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;API Failures&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Postman, &lt;code&gt;fetch&lt;/code&gt; interceptors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Memory Leaks&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Chrome’s "Memory" tab, &lt;code&gt;weakMap&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Cross-Browser Bugs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;BrowserStack, LambdaTest&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Step 7: Prevent Future Bugs with Defensive Coding
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Practices to Reduce Debugging Time Long-Term&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Write Atomic Components&lt;/strong&gt; – Small, single-responsibility components are easier to debug.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use PropTypes/TypeScript&lt;/strong&gt; – Validate props early.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add Assertions&lt;/strong&gt; – Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fetchUser: id is required&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Document Edge Cases&lt;/strong&gt; – Add JSDoc comments for non-obvious behavior.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion: Debugging Doesn’t Have to Be Painful
&lt;/h2&gt;

&lt;p&gt;By adopting a &lt;strong&gt;structured approach&lt;/strong&gt;—reproducing bugs reliably, narrowing scope, leveraging tools, and automating checks—you can &lt;strong&gt;cut debugging time in half&lt;/strong&gt; while writing more resilient code.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Your Action Plan&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pick one technique&lt;/strong&gt; from this guide to implement this week (e.g., binary debugging or Event Listener Breakpoints).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automate one error-prone workflow&lt;/strong&gt; (e.g., add TypeScript to a component or set up ESLint).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Share your wins&lt;/strong&gt; – What debugging hack saved you the most time? Drop a comment below!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Happy debugging—may your &lt;code&gt;console.log&lt;/code&gt;s be ever in your favor.&lt;/strong&gt; 🚀&lt;/p&gt;




&lt;p&gt;&lt;em&gt;What’s your biggest debugging pain point? Let’s discuss in the comments!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>productivity</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What's Server-Side Rendering (SSR) with Next.js, and Why Use It?</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Fri, 28 Nov 2025 18:18:11 +0000</pubDate>
      <link>https://forem.com/sharmaprash/whats-server-side-rendering-ssr-with-nextjs-and-why-use-it-1dd0</link>
      <guid>https://forem.com/sharmaprash/whats-server-side-rendering-ssr-with-nextjs-and-why-use-it-1dd0</guid>
      <description>&lt;h2&gt;
  
  
  🧒 The Toddler Explanation
&lt;/h2&gt;

&lt;p&gt;Imagine you're drawing a picture of a house. There are two ways to show your friend this picture:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Way 1 (SSR - Server-Side Rendering):&lt;/strong&gt; You draw the entire house with all the colors, windows, and doors BEFORE showing it to your friend. When they see it - BAM! - the whole house is already there, complete and ready to look at.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Way 2 (Client-Side Rendering):&lt;/strong&gt; You give your friend a blank paper and crayons, and they have to draw the house themselves. It takes them time to draw it, so they see a blank page first, then slowly the house appears.&lt;/p&gt;

&lt;p&gt;SSR is like Way 1 - the website is already drawn and ready when you open it. It's faster to see and makes everyone happy! 🎨✨&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 Why Developers Use SSR
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Speed &amp;amp; Performance
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Faster First Paint&lt;/strong&gt;: Users see content immediately instead of staring at a blank screen or loading spinner&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better SEO&lt;/strong&gt;: Search engines like Google can read your fully-rendered HTML right away&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Performance on Slow Devices&lt;/strong&gt;: The server does the heavy lifting, not the user's phone&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Real-World Benefits
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Traditional CSR: Blank → Spinner → Content (3-5 seconds)
Next.js SSR: Instant Content → Interactive (1-2 seconds)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧑‍💻 The Deep Dive: How SSR Actually Works
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The SSR Lifecycle
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;User Requests Page&lt;/strong&gt; → Browser asks server for &lt;code&gt;/products&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server Renders React&lt;/strong&gt; → Server executes your React components and generates HTML&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTML Sent to Browser&lt;/strong&gt; → Fully rendered HTML travels to the client&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Browser Displays HTML&lt;/strong&gt; → User sees content immediately (non-interactive)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript Downloads&lt;/strong&gt; → React bundle loads in the background&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hydration&lt;/strong&gt; → React "attaches" to the existing HTML, making it interactive&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// pages/products.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getServerSideProps&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This runs on the SERVER for every request&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/products&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// Passed to component as props&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Products&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This runs TWICE:&lt;/span&gt;
  &lt;span class="c1"&gt;// 1. On server (generates HTML)&lt;/span&gt;
  &lt;span class="c1"&gt;// 2. On client (hydrates the HTML)&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Our Products&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⚠️ The Hydration Mismatch Problem
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Is Hydration?
&lt;/h3&gt;

&lt;p&gt;Hydration is when React takes the server-rendered HTML and "wakes it up" by attaching event listeners and state. React expects the HTML it generates on the client to match EXACTLY what the server sent.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Mismatch Horror Story
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ This WILL cause hydration errors&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BadComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Current time: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why it breaks:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Server renders at 10:30:15 → HTML shows "10:30:15"&lt;/li&gt;
&lt;li&gt;Client hydrates at 10:30:17 → React expects "10:30:17"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MISMATCH!&lt;/strong&gt; → Console error and potential UI bugs&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Common Causes of Hydration Mismatches
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Random/Time-Based Values&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;   &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt; &lt;span class="c1"&gt;// Different every render&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt; &lt;span class="c1"&gt;// Different server vs client&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Browser-Only APIs&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;   &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// window doesn't exist on server&lt;/span&gt;
   &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;theme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt; &lt;span class="c1"&gt;// localStorage is undefined on server&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Conditional Rendering Based on Client State&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;   &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isClient&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Server sees nothing, client sees something&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Third-Party Libraries&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;   &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SomeLibrary&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// Library might access window or document&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ✅ Solutions to Hydration Mismatches
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Solution 1: useEffect for Client-Only Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;SafeComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Runs ONLY on client after hydration&lt;/span&gt;
    &lt;span class="nf"&gt;setTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Current time: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Loading...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution 2: Suppress Hydration Warning (Use Sparingly)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;suppressHydrationWarning&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution 3: Dynamic Imports with No SSR
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;dynamic&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;next/dynamic&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;DynamicComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dynamic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../components/ClientOnly&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="na"&gt;ssr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Skips SSR for this component&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;DynamicComponent&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Solution 4: Check Environment
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isBrowser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;isBrowser&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Width: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🎯 Best Practices
&lt;/h2&gt;

&lt;h3&gt;
  
  
  DO ✅
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fetch data in &lt;code&gt;getServerSideProps&lt;/code&gt; or &lt;code&gt;getStaticProps&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;useEffect&lt;/code&gt; for browser-only code&lt;/li&gt;
&lt;li&gt;Keep server and client renders identical initially&lt;/li&gt;
&lt;li&gt;Use dynamic imports for third-party libraries that need &lt;code&gt;window&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  DON'T ❌
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Access &lt;code&gt;window&lt;/code&gt;, &lt;code&gt;document&lt;/code&gt;, or &lt;code&gt;localStorage&lt;/code&gt; during render&lt;/li&gt;
&lt;li&gt;Generate random values or timestamps during render&lt;/li&gt;
&lt;li&gt;Use different logic for server vs client renders&lt;/li&gt;
&lt;li&gt;Forget that your component runs twice (server + client)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔍 Debugging Hydration Issues
&lt;/h2&gt;

&lt;p&gt;When you see: &lt;code&gt;Warning: Text content did not match...&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Check the DOM&lt;/strong&gt;: Use React DevTools to compare server HTML vs client render&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Console Log&lt;/strong&gt;: Add &lt;code&gt;console.log('SERVER')&lt;/code&gt; vs &lt;code&gt;console.log('CLIENT')&lt;/code&gt; to track execution&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolate&lt;/strong&gt;: Comment out components until the error disappears&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search for&lt;/strong&gt;: Time, random numbers, browser APIs, or conditional rendering&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;SSR with Next.js gives you the best of both worlds: fast initial page loads with the interactivity of React. The hydration process is mostly magical, but understanding when server and client diverge will save you hours of debugging.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember&lt;/strong&gt;: Server and client must start identical. Diverge only after hydration with &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Found this helpful? Follow me for more Next.js tips and tricks!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>serverless</category>
      <category>react</category>
    </item>
    <item>
      <title>How Do I Return the Response from an Asynchronous Call?</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Fri, 28 Nov 2025 18:10:05 +0000</pubDate>
      <link>https://forem.com/sharmaprash/how-do-i-return-the-response-from-an-asynchronous-call-nl2</link>
      <guid>https://forem.com/sharmaprash/how-do-i-return-the-response-from-an-asynchronous-call-nl2</guid>
      <description>&lt;p&gt;Async calls (e.g., &lt;code&gt;fetch()&lt;/code&gt;) return Promises, not immediate values. Direct &lt;code&gt;return&lt;/code&gt; yields &lt;code&gt;undefined&lt;/code&gt;. Focus: JavaScript (adapt for other langs).&lt;/p&gt;

&lt;h2&gt;
  
  
  Problem Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/data&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Return Promise (Chain .then())
&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;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/data&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pros&lt;/strong&gt;: Non-blocking. &lt;strong&gt;Cons&lt;/strong&gt;: Nesting issues.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Async/Await (Recommended)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchData&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;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pros&lt;/strong&gt;: Readable, try/catch errors. &lt;strong&gt;Cons&lt;/strong&gt;: Requires async callers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Callbacks (Legacy)
&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;function&lt;/span&gt; &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com/data&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;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;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pros&lt;/strong&gt;: Simple. &lt;strong&gt;Cons&lt;/strong&gt;: Control inversion.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Errors&lt;/strong&gt;: Always &lt;code&gt;.catch()&lt;/code&gt; or try/catch.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parallel&lt;/strong&gt;: &lt;code&gt;Promise.all([async1(), async2()])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Timeouts&lt;/strong&gt;: Use &lt;code&gt;AbortController&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing&lt;/strong&gt;: Mock &lt;code&gt;fetch&lt;/code&gt; in Jest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Node&lt;/strong&gt;: &lt;code&gt;node-fetch&lt;/code&gt; for &amp;lt; v18.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Real Example: User Posts API
&lt;/h2&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;fetch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node-fetch&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://jsonplaceholder.typicode.com/posts?userId=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Returns array&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;p&gt;Use async/await for clean returns. Reduces bugs in 90% of cases.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>async</category>
      <category>promises</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is a NullPointerException, and How Do I Fix It?</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Fri, 28 Nov 2025 18:06:47 +0000</pubDate>
      <link>https://forem.com/sharmaprash/what-is-a-nullpointerexception-and-how-do-i-fix-it-7bc</link>
      <guid>https://forem.com/sharmaprash/what-is-a-nullpointerexception-and-how-do-i-fix-it-7bc</guid>
      <description>&lt;p&gt;NullPointerException (NPE) is a Java runtime exception thrown when code accesses a null reference (e.g., calling a method on null).&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Concept
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Trigger&lt;/strong&gt;: Attempting to use a null object reference (e.g., &lt;code&gt;null.length()&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type&lt;/strong&gt;: Unchecked exception; occurs at runtime, not compile time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;  &lt;span class="c1"&gt;// Throws NPE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stack Trace&lt;/strong&gt;: Points to the line; trace upward for root cause.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common Causes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Uninitialized References&lt;/strong&gt;: Variables default to null if not set.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getUser&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Returns null if not found&lt;/span&gt;
  &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// NPE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Null Parameters&lt;/strong&gt;: Methods receive unexpected null inputs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collection Access&lt;/strong&gt;: Null lists/maps or missing keys.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="nc"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map&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;HashMap&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
  &lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// NPE if key absent&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Method Chains&lt;/strong&gt;: One null in chain fails all.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;  &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProfile&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getAddress&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getCity&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Fails if profile null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency&lt;/strong&gt;: Race conditions set references to null mid-use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;External Data&lt;/strong&gt;: JSON/DB results with null fields.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to Fix
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Add Null Checks&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;if (obj != null)&lt;/code&gt; before access.&lt;/li&gt;
&lt;li&gt;Or &lt;code&gt;Objects.requireNonNull(obj, "Message");&lt;/code&gt; for early failure.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Optional (Java 8+)&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;   &lt;span class="nc"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ofNullable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
       &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;User:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
       &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;orElse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Default"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Safe Collection Methods&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Maps: &lt;code&gt;map.getOrDefault(key, default)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Lists: Return &lt;code&gt;Collections.emptyList()&lt;/code&gt; if null.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Annotations&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@NotNull&lt;/code&gt; (Lombok/JSR-305) for IDE warnings.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debug Steps&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Read stack trace bottom-up.&lt;/li&gt;
&lt;li&gt;Add logs: &lt;code&gt;log.debug("Obj: {}", obj);&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Test with null injections.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Prevention Strategies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fail Fast&lt;/strong&gt;: Validate in constructors; throw on null.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Immutable Design&lt;/strong&gt;: Use &lt;code&gt;final&lt;/code&gt; fields; libraries like Immutables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Null Objects&lt;/strong&gt;: Return empty/default objects, not null.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools&lt;/strong&gt;: Static analysis (SonarQube, IntelliJ inspections).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Docs&lt;/strong&gt;: Use &lt;code&gt;@Nullable&lt;/code&gt; in Javadocs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Reviews&lt;/strong&gt;: Checklist for null handling.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Strategy Comparison&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Null Checks&lt;/strong&gt;: Pros - Simple; Cons - Verbose; Best for - Legacy code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optional&lt;/strong&gt;: Pros - Safe chaining; Cons - Overhead; Best for - New code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Annotations&lt;/strong&gt;: Pros - Proactive; Cons - Tool-dependent; Best for - Teams.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Null Objects&lt;/strong&gt;: Pros - Clean APIs; Cons - Design effort; Best for - Models.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;p&gt;Spot nulls early, check explicitly, use Optional for fluency. Reduces NPEs by 80%+ in practice.&lt;/p&gt;

</description>
      <category>java</category>
      <category>debugging</category>
      <category>exceptions</category>
    </item>
    <item>
      <title>Why can't I store a value and a reference to that value in the same struct?</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Thu, 05 Dec 2024 08:42:54 +0000</pubDate>
      <link>https://forem.com/sharmaprash/why-cant-i-store-a-value-and-a-reference-to-that-value-in-the-same-struct-3ook</link>
      <guid>https://forem.com/sharmaprash/why-cant-i-store-a-value-and-a-reference-to-that-value-in-the-same-struct-3ook</guid>
      <description>&lt;p&gt;If you've been exploring Rust's powerful type system and ownership model, you might have stumbled upon an odd limitation: &lt;strong&gt;you cannot store a value and a reference to that value in the same struct&lt;/strong&gt;. This constraint often trips up new Rustaceans, especially those coming from other languages where circular references or "self-referencing" structures are more straightforward.&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore &lt;strong&gt;why this limitation exists&lt;/strong&gt;, some common pitfalls, and how you can work around it in idiomatic Rust.&lt;/p&gt;




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

&lt;p&gt;Let's start with a basic example. Imagine you want a struct that stores a &lt;code&gt;String&lt;/code&gt; and a reference to that &lt;code&gt;String&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, Rust!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;my_struct&lt;/span&gt;&lt;span class="py"&gt;.value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// This doesn't work!&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;At first glance, this seems reasonable. But the Rust compiler immediately shuts it down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error[E0505]: cannot borrow `my_struct.value` as immutable because it is also borrowed as mutable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s going on here?&lt;/p&gt;




&lt;h2&gt;
  
  
  Why This Doesn't Work
&lt;/h2&gt;

&lt;p&gt;The core issue lies in &lt;strong&gt;Rust's ownership and borrowing rules&lt;/strong&gt;. To understand the problem, let’s break it down:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory Layout&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A struct in Rust lays out its fields in memory contiguously. If a struct contains both a value and a reference to that value, Rust cannot determine a safe memory layout for the reference because it points to a part of the struct itself. This creates a &lt;strong&gt;self-referential structure&lt;/strong&gt;, which Rust doesn't allow because of the potential for undefined behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Borrowing Rules&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Rust enforces strict borrowing rules to ensure memory safety. When you try to create &lt;code&gt;MyStruct&lt;/code&gt;, the &lt;code&gt;value&lt;/code&gt; field is owned by the struct. To create a reference to it (&lt;code&gt;&amp;amp;my_struct.value&lt;/code&gt;), you’re essentially borrowing from &lt;code&gt;my_struct&lt;/code&gt; while still initializing it—a circular dependency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lifetimes and Safety&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Lifetimes in Rust are used to guarantee that references are valid as long as they're needed. If you store a reference to &lt;code&gt;value&lt;/code&gt; in the same struct, Rust cannot ensure the reference remains valid because the struct owns &lt;code&gt;value&lt;/code&gt;. If the struct moves or gets dropped, the reference becomes invalid.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  How to Work Around It
&lt;/h2&gt;

&lt;p&gt;Although Rust doesn't allow storing a value and a reference to that value directly in the same struct, there are several ways to achieve similar functionality:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Use &lt;code&gt;Option&lt;/code&gt; and Late Initialization
&lt;/h3&gt;

&lt;p&gt;You can use an &lt;code&gt;Option&lt;/code&gt; to delay setting the reference until after the struct is created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, Rust!"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Initially, there's no reference&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;my_struct&lt;/span&gt;&lt;span class="py"&gt;.reference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;my_struct&lt;/span&gt;&lt;span class="py"&gt;.value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Set the reference later&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt;&lt;span class="py"&gt;.reference&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&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 works because the reference is set after the struct is fully initialized.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Use &lt;code&gt;Rc&lt;/code&gt; or &lt;code&gt;Arc&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;Rc&lt;/code&gt; (Reference Counted) or &lt;code&gt;Arc&lt;/code&gt; (Atomic Reference Counted) to create shared ownership:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;rc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Rc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Rc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, Rust!"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Rc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;Rc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt;&lt;span class="py"&gt;.reference&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;Rc&lt;/code&gt;, both fields share ownership of the same &lt;code&gt;String&lt;/code&gt;. However, this approach doesn't preserve the semantics of "a reference to a field" but achieves a similar result.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Use an Index or Key
&lt;/h3&gt;

&lt;p&gt;Another idiomatic approach is to separate the value storage and reference using an index or key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;collections&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;HashMap&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;HashMap&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="nf"&gt;.insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello, Rust!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Owned Value"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="nf"&gt;.get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"key"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt;&lt;span class="py"&gt;.reference&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;Here, the reference is managed externally, avoiding self-referential issues.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Use &lt;code&gt;Cell&lt;/code&gt; or &lt;code&gt;RefCell&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;For interior mutability, you can use &lt;code&gt;Cell&lt;/code&gt; or &lt;code&gt;RefCell&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;RefCell&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;RefCell&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyStruct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;RefCell&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, Rust!"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;reference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_struct&lt;/span&gt;&lt;span class="py"&gt;.value&lt;/span&gt;&lt;span class="nf"&gt;.borrow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reference&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 approach allows runtime borrow checking, but you must ensure correctness yourself.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Rust Says "No"
&lt;/h2&gt;

&lt;p&gt;Rust's safety guarantees rely on compile-time checks. Allowing self-referential structs could lead to dangling references, use-after-free errors, or memory corruption. Instead, Rust encourages developers to rethink their designs and use idiomatic patterns that achieve the same goals while maintaining safety.&lt;/p&gt;




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

&lt;p&gt;While Rust's restriction on storing a value and a reference to that value in the same struct can be frustrating, it’s there for good reason. The alternatives—like &lt;code&gt;Option&lt;/code&gt;, &lt;code&gt;Rc&lt;/code&gt;, or external references—may require rethinking your design but ensure your program is robust and memory-safe. Embracing these patterns helps you write better Rust code while gaining a deeper understanding of its ownership model.&lt;/p&gt;

&lt;p&gt;Have you encountered this limitation in your Rust journey? Share your thoughts and experiences in the comments below!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why is it Discouraged to Accept `&amp;String`, `&amp;Vec`, or `&amp;Box` as Function Arguments in Rust?</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Fri, 29 Nov 2024 09:08:40 +0000</pubDate>
      <link>https://forem.com/sharmaprash/why-is-it-discouraged-to-accept-string-vec-or-box-as-function-arguments-in-rust-3g72</link>
      <guid>https://forem.com/sharmaprash/why-is-it-discouraged-to-accept-string-vec-or-box-as-function-arguments-in-rust-3g72</guid>
      <description>&lt;p&gt;In Rust, when designing functions, it's common to pass arguments by reference to avoid unnecessary cloning or ownership transfer. However, you may have come across advice to avoid accepting types like &lt;code&gt;&amp;amp;String&lt;/code&gt;, &lt;code&gt;&amp;amp;Vec&lt;/code&gt;, or &lt;code&gt;&amp;amp;Box&lt;/code&gt; as function arguments. Instead, you’re encouraged to use more general types like &lt;code&gt;&amp;amp;str&lt;/code&gt;, &lt;code&gt;&amp;amp;[T]&lt;/code&gt;, or directly dereference the boxed type. Why is that?&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore why this advice exists and how following it can make your Rust code more idiomatic, flexible, and ergonomic.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Root of the Problem: Redundancy and Limiting Flexibility
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Redundancy in Indirection&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Types like &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt;, or &lt;code&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt; are already heap-allocated and provide indirection. When you take &lt;code&gt;&amp;amp;String&lt;/code&gt;, &lt;code&gt;&amp;amp;Vec&lt;/code&gt;, or &lt;code&gt;&amp;amp;Box&lt;/code&gt;, you're adding another layer of indirection unnecessarily.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string_ref&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Length: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string_ref&lt;/span&gt;&lt;span class="nf"&gt;.len&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;Here, &lt;code&gt;&amp;amp;String&lt;/code&gt; means you're passing a reference to a heap-allocated &lt;code&gt;String&lt;/code&gt;. But &lt;code&gt;String&lt;/code&gt; already has a concept of a borrowed view—&lt;code&gt;&amp;amp;str&lt;/code&gt;. A more idiomatic way is to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string_slice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Length: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string_slice&lt;/span&gt;&lt;span class="nf"&gt;.len&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;Why is this better? We’ll get to that shortly.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Loss of Generality&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Accepting &lt;code&gt;&amp;amp;String&lt;/code&gt; or &lt;code&gt;&amp;amp;Vec&amp;lt;T&amp;gt;&lt;/code&gt; restricts your function to only accept references to these specific types. This limits its usability with other types that can be borrowed in a similar way.&lt;/p&gt;

&lt;p&gt;Consider &lt;code&gt;&amp;amp;str&lt;/code&gt; versus &lt;code&gt;&amp;amp;String&lt;/code&gt;. While &lt;code&gt;&amp;amp;String&lt;/code&gt; works only for references to &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;&amp;amp;str&lt;/code&gt; can also be used with string literals (&lt;code&gt;&amp;amp;'static str&lt;/code&gt;), substrings, or any type that implements &lt;code&gt;Deref&lt;/code&gt; to &lt;code&gt;str&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;owned_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;owned_string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Works&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;literal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;// print_message(literal); // ERROR: mismatched types&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we refactor the function to accept &lt;code&gt;&amp;amp;str&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;owned_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;owned_string&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Works&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;literal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;print_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;literal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Works!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes the function more versatile and idiomatic.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. &lt;strong&gt;The Power of Deref Coercion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Rust’s &lt;strong&gt;deref coercion&lt;/strong&gt; automatically converts a reference to a type (like &lt;code&gt;&amp;amp;String&lt;/code&gt; or &lt;code&gt;&amp;amp;Vec&amp;lt;T&amp;gt;&lt;/code&gt;) into a reference to its "inner" type (&lt;code&gt;&amp;amp;str&lt;/code&gt; or &lt;code&gt;&amp;amp;[T]&lt;/code&gt;). This is why you don’t need to explicitly write functions that accept &lt;code&gt;&amp;amp;String&lt;/code&gt; or &lt;code&gt;&amp;amp;Vec&lt;/code&gt;—the compiler will handle the conversion for you.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;print_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;vec&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="nf"&gt;print_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// `&amp;amp;Vec&amp;lt;i32&amp;gt;` automatically coerced to `&amp;amp;[i32]`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You benefit from this powerful feature by accepting the most general form (&lt;code&gt;&amp;amp;str&lt;/code&gt;, &lt;code&gt;&amp;amp;[T]&lt;/code&gt;) as function arguments.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practices for Function Arguments
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Use Slices (&lt;code&gt;&amp;amp;[T]&lt;/code&gt;) Instead of &lt;code&gt;&amp;amp;Vec&amp;lt;T&amp;gt;&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;A slice (&lt;code&gt;&amp;amp;[T]&lt;/code&gt;) represents a view into a contiguous sequence of elements, which is exactly what &lt;code&gt;Vec&lt;/code&gt; provides. By accepting slices, your function can work with any type that can be dereferenced into a slice, not just &lt;code&gt;Vec&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&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;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;vec&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.sum&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;Write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;i32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;slice&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.sum&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 works with arrays, vectors, and other slice-like types.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Use &lt;code&gt;&amp;amp;str&lt;/code&gt; Instead of &lt;code&gt;&amp;amp;String&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;&amp;amp;str&lt;/code&gt; is a string slice, which can represent string data in various forms—string literals, parts of a string, or an entire &lt;code&gt;String&lt;/code&gt;. By using &lt;code&gt;&amp;amp;str&lt;/code&gt;, your function becomes more generic and flexible.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, {}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&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;Write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, {}!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, your function works with both &lt;code&gt;String&lt;/code&gt; and string literals.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Avoid &lt;code&gt;&amp;amp;Box&amp;lt;T&amp;gt;&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Boxed types (&lt;code&gt;Box&amp;lt;T&amp;gt;&lt;/code&gt;) are heap-allocated, but taking &lt;code&gt;&amp;amp;Box&amp;lt;T&amp;gt;&lt;/code&gt; introduces redundant indirection. You can simply take &lt;code&gt;&amp;amp;T&lt;/code&gt; instead.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&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;Write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&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;
  
  
  When Should You Use &lt;code&gt;&amp;amp;String&lt;/code&gt;, &lt;code&gt;&amp;amp;Vec&lt;/code&gt;, or &lt;code&gt;&amp;amp;Box&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;While it's discouraged in most cases, there are rare situations where you might need to accept &lt;code&gt;&amp;amp;String&lt;/code&gt;, &lt;code&gt;&amp;amp;Vec&lt;/code&gt;, or &lt;code&gt;&amp;amp;Box&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;You're working with APIs that explicitly require these types.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;You're debugging or refactoring existing code and can’t change upstream designs.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Even then, consider refactoring the API if possible.&lt;/p&gt;




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

&lt;p&gt;Avoiding &lt;code&gt;&amp;amp;String&lt;/code&gt;, &lt;code&gt;&amp;amp;Vec&amp;lt;T&amp;gt;&lt;/code&gt;, or &lt;code&gt;&amp;amp;Box&amp;lt;T&amp;gt;&lt;/code&gt; as function arguments is more than just stylistic advice—it’s about writing idiomatic, flexible, and ergonomic Rust code. By leveraging slices (&lt;code&gt;&amp;amp;[T]&lt;/code&gt;), string slices (&lt;code&gt;&amp;amp;str&lt;/code&gt;), and deref coercion, your functions can support a wider range of input types, leading to cleaner and more reusable code.&lt;/p&gt;

&lt;p&gt;Next time you’re writing a Rust function, think about its most general form. You’ll be amazed at how much more versatile and future-proof your code becomes!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>devops</category>
      <category>web3</category>
    </item>
    <item>
      <title>How to Iterate Over a String in Rust</title>
      <dc:creator>Prashant Sharma</dc:creator>
      <pubDate>Sun, 24 Nov 2024 17:28:49 +0000</pubDate>
      <link>https://forem.com/sharmaprash/how-to-iterate-over-a-string-in-rust-1alk</link>
      <guid>https://forem.com/sharmaprash/how-to-iterate-over-a-string-in-rust-1alk</guid>
      <description>&lt;p&gt;In Rust, strings (&lt;code&gt;String&lt;/code&gt; and &lt;code&gt;&amp;amp;str&lt;/code&gt;) are UTF-8 encoded, which means characters can be multiple bytes long. This makes direct indexing impossible, but Rust provides safe and efficient ways to iterate over strings. Here’s how you can do it:&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Iterate Over Characters
&lt;/h2&gt;

&lt;p&gt;To iterate through each character (&lt;code&gt;char&lt;/code&gt;) in a string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello, Rust!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="nf"&gt;.chars&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&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;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;H
e
l
l
o
,

R
u
s
t
!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. Iterate Over Bytes
&lt;/h2&gt;

&lt;p&gt;To work with the raw bytes of a string, use the &lt;code&gt;bytes&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello, Rust!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="nf"&gt;.bytes&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;72
101
108
108
111
44
32
82
117
115
116
33
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Iterate Over Grapheme Clusters
&lt;/h2&gt;

&lt;p&gt;To handle user-perceived characters (like emojis or accented characters), use the &lt;code&gt;unicode-segmentation&lt;/code&gt; crate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[dependencies]&lt;/span&gt;
&lt;span class="py"&gt;unicode-segmentation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"1.10"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;unicode_segmentation&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;UnicodeSegmentation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"नमस्ते"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Example with multi-byte characters&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;grapheme&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="nf"&gt;.graphemes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;grapheme&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;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;न
म
स
्
ते
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  4. Iterate with Indices
&lt;/h2&gt;

&lt;p&gt;To get both the index and the character:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello, Rust!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_string&lt;/span&gt;&lt;span class="nf"&gt;.char_indices&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Index: {}, Character: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&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;
  
  
  Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index: 0, Character: H
Index: 1, Character: e
Index: 2, Character: l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;chars&lt;/code&gt;&lt;/strong&gt;: Iterate over individual characters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;bytes&lt;/code&gt;&lt;/strong&gt;: Iterate over raw bytes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;graphemes&lt;/code&gt;&lt;/strong&gt;: Work with user-perceived characters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;char_indices&lt;/code&gt;&lt;/strong&gt;: Iterate with indices.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rust</category>
      <category>cargo</category>
      <category>devchallenge</category>
      <category>web3</category>
    </item>
  </channel>
</rss>
