<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pratik Tiwari</title>
    <description>The latest articles on DEV Community by Pratik Tiwari (@pratikcodes).</description>
    <link>https://dev.to/pratikcodes</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%2F675403%2F32a84612-3581-424d-9b48-7dddbec50a33.jpg</url>
      <title>DEV Community: Pratik Tiwari</title>
      <link>https://dev.to/pratikcodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratikcodes"/>
    <language>en</language>
    <item>
      <title>Understanding Memory Management in Rust</title>
      <dc:creator>Pratik Tiwari</dc:creator>
      <pubDate>Tue, 11 Mar 2025 19:14:47 +0000</pubDate>
      <link>https://dev.to/pratikcodes/understanding-memory-management-in-rust-48pi</link>
      <guid>https://dev.to/pratikcodes/understanding-memory-management-in-rust-48pi</guid>
      <description>&lt;p&gt;Memory management is a critical aspect of software development. It ensures that applications use memory efficiently and avoid common issues like memory leaks, dangling pointers, and crashes. In this blog, we’ll explore how JavaScript, C++, and Rust handle memory management, and why it matters.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is Memory Management?
&lt;/h2&gt;

&lt;p&gt;Memory management is the process of allocating and deallocating memory in a computer system. It’s especially important when working with &lt;strong&gt;RAM&lt;/strong&gt; (Random Access Memory).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RAM is volatile memory&lt;/strong&gt;: It loses its contents when the power is turned off.&lt;/li&gt;
&lt;li&gt;All the applications you run on your computer are stored in &lt;strong&gt;RAM&lt;/strong&gt; while they’re active.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Efficient memory management ensures that systems run smoothly and resources aren’t wasted.&lt;/p&gt;




&lt;h2&gt;
  
  
  Memory Management Across Programming Languages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  JavaScript: Automatic Garbage Collection
&lt;/h3&gt;

&lt;p&gt;JavaScript handles memory management through an automatic &lt;strong&gt;garbage collector&lt;/strong&gt;. This means developers don’t need to explicitly allocate or free memory; the language takes care of it. Here’s an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;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;ul&gt;
&lt;li&gt;The variable &lt;code&gt;sum&lt;/code&gt; is stored in &lt;strong&gt;stack memory&lt;/strong&gt;, a temporary storage location in RAM.&lt;/li&gt;
&lt;li&gt;Once the function returns, the memory is &lt;strong&gt;automatically deallocated&lt;/strong&gt; by the garbage collector.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This process minimizes common memory issues like dangling pointers and memory leaks, but it can make JavaScript slower because the garbage collector periodically scans and cleans up memory.&lt;/p&gt;




&lt;h3&gt;
  
  
  C and C++: Manual Memory Management
&lt;/h3&gt;

&lt;p&gt;In C and C++, developers are responsible for manually managing memory. This involves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Allocating memory&lt;/strong&gt; using functions like &lt;code&gt;malloc&lt;/code&gt; or &lt;code&gt;new&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deallocating memory&lt;/strong&gt; using functions like &lt;code&gt;free&lt;/code&gt; or &lt;code&gt;delete&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While this approach gives you fine-grained control, it also makes the code more error-prone. Common pitfalls include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory leaks&lt;/strong&gt;: Forgetting to free allocated memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dangling pointers&lt;/strong&gt;: Referencing memory that has already been freed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The learning curve for C++ is steep, and writing safe code requires careful attention to detail.&lt;/p&gt;




&lt;h3&gt;
  
  
  Rust: Ownership and Safety
&lt;/h3&gt;

&lt;p&gt;Rust takes a unique and modern approach to memory management through its &lt;strong&gt;ownership model&lt;/strong&gt;, which ensures both safety and efficiency. Let’s break this down step by step:&lt;/p&gt;




&lt;h3&gt;
  
  
  Ownership: The Core Concept
&lt;/h3&gt;

&lt;p&gt;In Rust, &lt;strong&gt;ownership&lt;/strong&gt; means that each piece of data has a single "owner," which is typically a variable. Ownership comes with specific rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Each value has one owner&lt;/strong&gt;: For example, if a string is assigned to a variable, that variable "owns" the string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ownership can be transferred&lt;/strong&gt;: If you assign the string to another variable, the ownership moves, and the previous variable loses access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory is freed when the owner goes out of scope&lt;/strong&gt;: Rust automatically deallocates memory for the data when its owner stops being used.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s an example to illustrate 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;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;s1&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"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// s1 owns the string "hello"&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt; &lt;span class="o"&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;// Ownership moves to s2; s1 is no longer valid&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;s2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Works fine&lt;/span&gt;
    &lt;span class="c1"&gt;// println!("{}", s1); // Error: s1 is no longer valid&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By enforcing these rules, Rust prevents issues like double-free errors or invalid memory access.&lt;/p&gt;




&lt;h3&gt;
  
  
  Borrowing and References: Temporary Access
&lt;/h3&gt;

&lt;p&gt;Sometimes, you don’t want to transfer ownership but instead temporarily use the data. This is where &lt;strong&gt;borrowing&lt;/strong&gt; comes in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Borrowing allows you to create references to data without transferring ownership.&lt;/li&gt;
&lt;li&gt;References can be either &lt;strong&gt;immutable&lt;/strong&gt; (read-only) or &lt;strong&gt;mutable&lt;/strong&gt; (read-and-write).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Immutable Reference&lt;/strong&gt; Example:&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;s&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"&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;len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_length&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;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Borrowing `s` as an immutable reference&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;"The length of '{}' is {}."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// `s` is still valid&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;calculate_length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&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="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Read-only access to `s`&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;Mutable Reference&lt;/strong&gt; Example:&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="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;s&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"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;change&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&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;// Borrowing `s` as a mutable reference&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;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "hello, world"&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;change&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&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="n"&gt;s&lt;/span&gt;&lt;span class="nf"&gt;.push_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;", world"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Modifying `s`&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust ensures you don’t mix mutable and immutable references, preventing conflicts.&lt;/p&gt;




&lt;h3&gt;
  
  
  Lifetimes: Ensuring Valid References
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Lifetimes&lt;/strong&gt; in Rust define how long references are valid. Rust uses a concept called "borrow checker" to ensure references never outlive the data they point to. This prevents &lt;strong&gt;dangling references&lt;/strong&gt;, which occur when memory is accessed after being freed.&lt;/p&gt;

&lt;p&gt;Example of a dangling reference:&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;r&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Declare a reference&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// `x` is created&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// `r` borrows `x`&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// `x` goes out of scope and is dropped&lt;/span&gt;
    &lt;span class="c1"&gt;// println!("{}", r); // Error: `r` points to invalid memory&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust detects this problem at compile time, ensuring your program is safe to run.&lt;/p&gt;




&lt;h3&gt;
  
  
  No Garbage Collector, Only Guarantees
&lt;/h3&gt;

&lt;p&gt;Unlike languages like JavaScript, which rely on a &lt;strong&gt;garbage collector&lt;/strong&gt; (a process that scans and frees unused memory), Rust doesn’t need one. Instead:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rust enforces ownership rules at compile time.&lt;/li&gt;
&lt;li&gt;This makes Rust &lt;strong&gt;efficient&lt;/strong&gt; (no garbage collection overhead) and &lt;strong&gt;safe&lt;/strong&gt; (prevents memory bugs).&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Why Ownership Matters
&lt;/h3&gt;

&lt;p&gt;Rust’s ownership model:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Eliminates common bugs like memory leaks and dangling pointers.&lt;/li&gt;
&lt;li&gt;Ensures efficient memory usage without needing a garbage collector.&lt;/li&gt;
&lt;li&gt;Provides compile-time guarantees about memory safety, allowing developers to write reliable high-performance code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By mastering ownership, you unlock the full potential of Rust for building safe and efficient applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Mutability in Rust
&lt;/h2&gt;

&lt;p&gt;By default, variables in Rust are &lt;strong&gt;immutable&lt;/strong&gt;, meaning their value cannot change after being assigned. This helps prevent bugs and makes code easier to understand.&lt;/p&gt;

&lt;p&gt;To make a variable mutable, you use the &lt;code&gt;mut&lt;/code&gt; keyword:&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;let&lt;/span&gt; &lt;span class="k"&gt;mut&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;10&lt;/span&gt;&lt;span class="p"&gt;;&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;15&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;"Mutable y: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is different from JavaScript, where even a &lt;code&gt;const&lt;/code&gt; variable can reference mutable data:&lt;br&gt;
&lt;/p&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;x&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [1, 2, 3, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In JavaScript, &lt;code&gt;const&lt;/code&gt; only makes the reference immutable, not the data it points to.&lt;/p&gt;




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

&lt;p&gt;Each language approaches memory management differently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt; simplifies memory management with automatic garbage collection, making it beginner-friendly but sometimes less efficient.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C++&lt;/strong&gt; provides full control but comes with a steep learning curve and potential pitfalls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rust&lt;/strong&gt; strikes a balance between safety and performance with its innovative ownership model.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choosing the right tool for the job depends on your project’s needs and your expertise. Understanding memory management helps you write better, more efficient code.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Solidity 101: A Starter Guide</title>
      <dc:creator>Pratik Tiwari</dc:creator>
      <pubDate>Fri, 13 Oct 2023 15:32:51 +0000</pubDate>
      <link>https://dev.to/pratikcodes/solidity-101-a-starter-guide-h5m</link>
      <guid>https://dev.to/pratikcodes/solidity-101-a-starter-guide-h5m</guid>
      <description>&lt;h1&gt;
  
  
  Solidity 101: A Starter Guide
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Datatypes&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
Elementary Types

&lt;ul&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Integer&lt;/li&gt;
&lt;li&gt;Address&lt;/li&gt;
&lt;li&gt;Bytes&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Complex Types

&lt;ul&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;li&gt;Structs&lt;/li&gt;
&lt;li&gt;Enums&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Special Types

&lt;ul&gt;
&lt;li&gt;Mappings&lt;/li&gt;
&lt;li&gt;Function Types&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Storage vs Memory Datatypes vs Calldata&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Contracts&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Definition&lt;/li&gt;
&lt;li&gt;Events&lt;/li&gt;
&lt;li&gt;
Scopes

&lt;ul&gt;
&lt;li&gt;Public&lt;/li&gt;
&lt;li&gt;Internal&lt;/li&gt;
&lt;li&gt;Private&lt;/li&gt;
&lt;li&gt;External&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Inheritance&lt;/li&gt;
&lt;li&gt;
Cross contract calls

&lt;ul&gt;
&lt;li&gt;Interface calls&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Modifiers&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Gas Optimzation Techniques&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use Appropriate Data Types&lt;/li&gt;
&lt;li&gt;Use events for output data&lt;/li&gt;
&lt;li&gt;Short-Circuiting in Conditions&lt;/li&gt;
&lt;li&gt;Loops and Large Data Structures&lt;/li&gt;
&lt;li&gt;Use Libraries and Delegatecall&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Payabale&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Withdraw&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Resources&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;


&lt;h1&gt;
  
  
  Datatypes
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Elementary Types
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Boolean
&lt;/h3&gt;

&lt;p&gt;Utilised for logical operations. Two possible values: &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool isTrue = true;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Integer
&lt;/h3&gt;

&lt;p&gt;Supports both signed (int) and unsigned (uint) integers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int256 a = -10;
uint256 b = 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Address
&lt;/h3&gt;

&lt;p&gt;Used to store ethereum addresses. These addresses are link the bank account number for your bank account.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;address public owner;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Bytes
&lt;/h3&gt;

&lt;p&gt;Fixed-size byte arrays ranging from &lt;code&gt;byte1&lt;/code&gt; to &lt;code&gt;byte2&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bytes32 hash = "abc";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  Complex Types
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Arrays
&lt;/h3&gt;

&lt;p&gt;can be either fixed-size or dynamic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;uint256[] public numbers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Structs
&lt;/h3&gt;

&lt;p&gt;Structures (also called structs) are a way to group several related variables into one place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Person {
    string name;
    uint age;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Enums
&lt;/h3&gt;

&lt;p&gt;Enumeration types for creating custom types with a finite set of values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum State { Created, Locked, Inactive }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  Special Types
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mappings
&lt;/h3&gt;

&lt;p&gt;Key-value storage Structures&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mappings(addresses =&amp;gt; uint) public balances;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Types
&lt;/h3&gt;

&lt;p&gt;These are first-class citizens in solidity. allowing for higher-order functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function (uint256) external returns (bool) myFunction;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  Storage vs Memory Datatypes
&lt;/h2&gt;

&lt;p&gt;Understanding the distinction between storage and memory data types is crucial for efficient and secure smart contract development. These two data locations serve different purposes and have different cost implications in terms of gas usage.&lt;/p&gt;

&lt;h4&gt;
  
  
  Characteristics
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Persistent&lt;/strong&gt;: Data stored in storage persists between function calls and transactions. Changes to storage are very costly in terms of gas.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Blockchain State&lt;/strong&gt;: Storage variables are part of the contract's state, and thus, they are written to the Ethereum blockchain.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Usage
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;State variables are by default storage variables.&lt;/li&gt;
&lt;li&gt;Explicitly, the &lt;code&gt;storage&lt;/code&gt; keyword can be used in function arguments, although this is rarely done due to high gas costs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Memory Data Types
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; Characteristics:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-   Temporary: Memory variables only exist during the execution of a function. Once the function execution is complete, the data stored in memory is erased.
-   Lower Gas Costs: Reading from and writing to memory is generally less costly than storage operations.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; Usage:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-   Local variables within functions can be declared with the `memory` keyword.
-   Function arguments can also be explicitly set to `memory`.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; Example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

    contract MemoryExample {
        function memoryFunction() public pure returns (uint256) {
            uint256[] memory memoryArray = new uint256[](3);
            memoryArray[0] = 1;
            memoryArray[1] = 2;
            memoryArray[2] = 3;
            return memoryArray[1];  // Returns 2
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Storage Data Types
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Characteristics:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt; Persistent: Unlike memory variables, storage variables persist between function calls and transactions. The data is stored directly on the Ethereum blockchain.&lt;/li&gt;
&lt;li&gt; Higher Gas Costs: Any operation that alters the state of storage variables incurs a higher gas fee compared to operations on memory variables. This is due to the permanent nature of storage and the broader impact it has on the Ethereum network.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Usage:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt; State Variables: By default, variables declared at the contract level are storage variables. They represent the contract's state and exist for the life of the contract.&lt;/li&gt;
&lt;li&gt; Explicit Keyword: Although rarely used, the &lt;code&gt;storage&lt;/code&gt; keyword can explicitly mark certain local variables, generally in the context of referencing or aliasing state variables.&lt;/li&gt;
&lt;/ol&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract StorageExample {
    // State variable stored on the blockchain
    uint256 public storageVariable;

    // Constructor to initialize the state variable
    constructor(uint256 _initialValue) {
        storageVariable = _initialValue;
    }

    // Function to update the state variable
    function setStorageVariable(uint256 _value) public {
        storageVariable = _value;
    }

    // Function to get the state variable
    function getStorageVariable() public view returns (uint256) {
        return storageVariable;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Calldata
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; Characteristics:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-   Immutable: `Calldata` is a read-only data location.
-   External Functions: Only applicable for `external` function arguments.
-   Lowest Cost: Reading from `calldata` is cheaper than from `memory` or `storage`.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; Usage:&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-   Used in `external` function arguments to signify that the data should not be modified and will be kept in the transaction's calldata.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt; Example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

    contract CalldataExample {
        function calldataFunction(uint256[] calldata data) external pure returns (uint256) {
            return data.length;
        }
    }

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Comparative Analysis
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Storage&lt;/th&gt;
&lt;th&gt;Memory&lt;/th&gt;
&lt;th&gt;Calldata&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Lifetime&lt;/td&gt;
&lt;td&gt;Persistent&lt;/td&gt;
&lt;td&gt;Temporary&lt;/td&gt;
&lt;td&gt;Function call&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cost&lt;/td&gt;
&lt;td&gt;High (Gas)&lt;/td&gt;
&lt;td&gt;Lower (Gas)&lt;/td&gt;
&lt;td&gt;Lowest (Gas)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mutability&lt;/td&gt;
&lt;td&gt;Mutable&lt;/td&gt;
&lt;td&gt;Mutable&lt;/td&gt;
&lt;td&gt;Immutable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Location&lt;/td&gt;
&lt;td&gt;Blockchain State&lt;/td&gt;
&lt;td&gt;Function Scope&lt;/td&gt;
&lt;td&gt;Transaction&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h1&gt;
  
  
  Contracts
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;A contract in Solidity is a collection of code and data that resides at a specific address on the Ethereum blockchain. It serves as the fundamental building block for creating decentralized applications.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract SimpleContract {
    // your logic here 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it can have methods, state variables, modifiers, events etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contract ComplexContract {
    uint256 public stateVariable;

    modifier onlyOwner() {
        // Modifier code
        _;
    }

    event LogData(uint256 data);

    function doSomething() public onlyOwner {
        emit LogData(stateVariable);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Events
&lt;/h2&gt;

&lt;p&gt;In Solidity, events are a crucial feature that facilitates communication between smart contracts and their external consumers, such as decentralized applications (dApps). Events provide a logging mechanism that allows for the storage of arguments in the transaction logs—a component of the Ethereum blockchain. This feature is particularly valuable for external clients that may need to "listen" for specific occurrences within a smart contract.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;event LogData(uint indexed id, string message);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Emiting Events&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;emit LogData(1, "This is a message");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract EventExample {
    event LogChange(string oldValue, string newValue);

    string public data;

    function setData(string memory _data) public {
        emit LogChange(data, _data);
        data = _data;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Scopes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Global Scopes
&lt;/h3&gt;

&lt;p&gt;At the global scope, you can declare contracts, import other contracts or libraries, and define custom data types like structs and enums.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./AnotherContract.sol";

contract GlobalScopeExample {
    // Code and data go here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Contracts Scopes
&lt;/h3&gt;

&lt;p&gt;Within a contract, you can define state variables, functions, modifiers, and events. These elements are accessible based on their visibility modifiers (public, internal, private, external).&lt;/p&gt;

&lt;h3&gt;
  
  
  Public
&lt;/h3&gt;

&lt;p&gt;The public modifier allows the widest level of accessibility. Functions marked as public can be called both internally within the contract and externally by other contracts or external actors. For state variables, Solidity automatically generates a getter function when they are marked as public.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract PublicExample {
    uint256 public stateVariable;

    function publicFunction() public returns (uint256) {
        return stateVariable;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Internal
&lt;/h3&gt;

&lt;p&gt;The internal modifier restricts access to the current contract and derived contracts (i.e., contracts that inherit from it). Functions and state variables marked as internal cannot be accessed by external contracts or external actors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract InternalExample {
    uint256 internal stateVariable;

    function internalFunction() internal returns (uint256) {
        return stateVariable;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Private
&lt;/h3&gt;

&lt;p&gt;The private modifier imposes the most restrictive level of access. Functions and state variables marked as private can only be accessed within the contract where they are defined. Even derived contracts cannot access private members of their base contracts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract PrivateExample {
    uint256 private stateVariable;

    function privateFunction() private returns (uint256) {
        return stateVariable;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  External
&lt;/h3&gt;

&lt;p&gt;The external modifier is similar to public, but it restricts the function to only be callable from outside the contract. Functions marked as external cannot be called internally within the contract, except using this.functionName().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract ExternalExample {
    function externalFunction() external pure returns (string memory) {
        return "This function can only be called externally.";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;In Solidity, inheritance is a powerful feature that allows for the creation of new contracts based on existing ones. This concept enables code reusability, a hierarchical structure of contracts, and the development of complex decentralized applications with modular components. Understanding inheritance is essential for achieving a high level of technical competence in Solidity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract ParentContract {
    function parentFunction() public pure returns (string memory) {
        return "This is from the parent contract";
    }
}

contract ChildContract is ParentContract {
    function childFunction() public pure returns (string memory) {
        return parentFunction();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cross contracts calls
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Interface calls
&lt;/h3&gt;

&lt;p&gt;Interfaces provide a way to define the functions of the external contract, offering a more modular approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

interface IContractA {
    function foo() external pure returns (string memory);
}

contract ContractB {
    IContractA contractA;

    // why use external here? 
    // Since constructors are only executed once, any logic or state initialization placed in the constructor does not incur repeated gas costs. This is more efficient than initializing or setting these variables in separate transactions after deployment.

    constructor(address _contractA) external {
        contractA = IContractA(_contractA);
    }

    function callFoo() public view returns (string memory) {
        return contractA.foo();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  Modifiers
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Think it as a middleware in web2 world.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Solidity, modifiers are a powerful feature that allows for the modification of function behavior in a reusable and clean manner. They are often used to simplify code, enhance readability, and manage permissions or conditions that must be met before a function can execute. Understanding the role and utility of modifiers is pivotal for anyone aiming to achieve a high level of technical competence in smart contract development.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;modifier onlyOwner() {
    require(msg.sender == owner, "You are not the owner");
    _;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Usage in Functions
&lt;/h3&gt;

&lt;p&gt;Modifiers are appended to function signatures and modify the function's behavior. Multiple modifiers can be used, and they are executed in the order in which they appear.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function doSomething() public onlyOwner {
    // Function logic here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Here's a simple example demonstrating the use of a modifier for access control:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract ModifierExample {
    address public owner;

    modifier onlyOwner() {
        require(msg.sender == owner, "You are not the owner");
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    function setOwner(address _newOwner) public onlyOwner {
        owner = _newOwner;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;onlyOwner&lt;/code&gt; modifier ensures that only the contract's owner can change the owner address. The &lt;code&gt;setOwner&lt;/code&gt; function uses this modifier to restrict access.&lt;/p&gt;


&lt;h2&gt;
  
  
  Gas Optimzation Techniques
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Use Appropriate Data Types
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Use uint256 for Arithmetic: The EVM is optimised for 256-bit arithmetic, making uint256 often more gas-efficient than smaller integer types like uint8 or uint16.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use bytes1 to bytes32 for Small Data: Fixed-size byte arrays are more gas-efficient than their dynamic bytes counterpart.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Prefer uint256 for arithmetic
uint256 public counter;

// Use bytes32 for fixed-size byte arrays
bytes32 public fixedHash;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using calldata as an arguments when you know the arguments arent changing as it is stored in different read only memory which is very gas efficient. &lt;br&gt;
If the change doesnt need to be propograted use memory as it cost efficent in comparison to storage which changes are propograted to the whole contract.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optimizing on space complexity over time complexity. Suppose you have a function which iterates over a array and returns a sum. You can also store this sum whenever a value is addded and create a variable in the contract for the same. But suppose the function is just a view function you dont have much incentive to save on time complexity because any which ways the gas is not used for a view function computing and gas is used to store the variable. Hence compromising on the time compleixity here makes more sense to save on the gas.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Use Events for Output Data
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; Log Events Instead of Return: For data that doesn't need to be immediately used in transactions, consider using events. They are cheaper than storing data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;solidityCopy code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;event DataEvent(uint256 data);

function doSomething() public {
    // ... logic
    emit DataEvent(42);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Short-Circuiting in Conditionals
&lt;/h3&gt;

&lt;p&gt;Order Conditions Wisely: In if and require statements, place the most likely-to-fail conditions first. This takes advantage of short-circuiting to save gas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require(isValid &amp;amp;&amp;amp; (msg.sender == owner), "Condition failed");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Loops and Large data Structures
&lt;/h3&gt;

&lt;p&gt;Avoid Infinite Loops: Always ensure that loops have a termination condition.&lt;br&gt;
Use Pagination for Large Data Sets: When dealing with large data structures, consider implementing pagination to read/write data in chunks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Pagination example
function getElements(uint256 start, uint256 end) public view returns (uint256[] memory) {
    uint256[] memory elements = new uint256[](end - start);
    for (uint256 i = start; i &amp;lt; end; i++) {
        elements[i - start] = someLargeArray[i];
    }
    return elements;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use Libraries and Delegatecall
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;se Libraries for Reusable Logic: Code that is reused across multiple contracts can be placed in a library, reducing the deployment and runtime gas costs.&lt;br&gt;
Use delegatecall Carefully: This low-level function allows for more gas-efficient logic&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;execution but should be used cautiously due to its complexity and potential securit risks.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Using a library function
using SafeMath for uint256;

uint256 public value;

function increment(uint256 _value) public {
    value = value.add(_value);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  Payable
&lt;/h2&gt;

&lt;p&gt;In Solidity, the payable keyword is used to enable a function or a contract to receive Ether. This feature is crucial for the development of financial applications and other smart contracts that involve monetary transactions. Understanding the payable modifier is essential for anyone aiming to achieve a high level of technical competence in smart contract development.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function deposit() public payable {
    // Function logic here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract PayableExample {
    uint256 public balance;

    function deposit() public payable {
        balance += msg.value;
    }

    function getBalance() public view returns (uint256) {
        return address(this).balance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h2&gt;
  
  
  Withdraw
&lt;/h2&gt;

&lt;p&gt;In Solidity, withdraw functions are a critical component of smart contracts that handle Ether transactions. These functions facilitate the secure transfer of Ether from a contract to an external address. Understanding the design patterns and security considerations for implementing withdraw functions is essential for anyone aiming to achieve a high level of technical competence in smart contract development.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Think of it as a function to get out the eths by the contract maker to his or someone elses address&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Security&lt;/em&gt;: Withdraw functions must be carefully designed to prevent vulnerabilities such as reentrancy attacks.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Gas Efficiency&lt;/em&gt;: Efficiently coded withdraw functions can minimize gas costs, making the contract more economical to use.&lt;/p&gt;

&lt;p&gt;Access Control: Usually, withdraw functions incorporate access control mechanisms to ensure that only authorized addresses can withdraw funds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function withdraw(uint256 amount) public {
    require(amount &amp;lt;= balances[msg.sender], "Insufficient balance");
    payable(msg.sender).transfer(amount);
    balances[msg.sender] -= amount;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.8.0;

contract WithdrawExample {
    mapping(address =&amp;gt; uint256) public balances;

    function deposit() public payable {
        balances[msg.sender] += msg.value;
    }

    function withdraw(uint256 amount) public {
        require(balances[msg.sender] &amp;gt;= amount, "Insufficient balance");

        // Securely perform the transfer
        (bool success, ) = msg.sender.call{value: amount}("");
        require(success, "Withdrawal failed");

        // Update state after the transfer
        balances[msg.sender] -= amount;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the withdraw function first checks whether the caller has a sufficient balance. It then uses a low-level .call method to perform the transfer, which is currently recommended over .transfer or .send to mitigate certain types of reentrancy attacks. Finally, it updates the balance mapping to reflect the withdrawal.&lt;/p&gt;



&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.soliditylang.org/en/v0.8.6/" rel="noopener noreferrer"&gt;Solidity Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.alchemy.com/university" rel="noopener noreferrer"&gt;Alchemy University&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cryptozombies.io/" rel="noopener noreferrer"&gt;Crypto Zombies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ethernaut.openzeppelin.com/" rel="noopener noreferrer"&gt;Ethernaut&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://solidity-by-example.org/" rel="noopener noreferrer"&gt;Solidity by Example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://fravoll.github.io/solidity-patterns/" rel="noopener noreferrer"&gt;Solidity Patterns&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.openzeppelin.com/contracts/4.x/" rel="noopener noreferrer"&gt;OpenZeppelin Contracts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/OpenZeppelin/openzeppelin-contracts" rel="noopener noreferrer"&gt;OpenZeppelin Git&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>solidity</category>
      <category>web3</category>
      <category>blockchain</category>
      <category>smartcontract</category>
    </item>
    <item>
      <title>Mono Respositories in JS/TS. What? Why? and How? (with Nx)</title>
      <dc:creator>Pratik Tiwari</dc:creator>
      <pubDate>Sat, 28 Jan 2023 07:38:35 +0000</pubDate>
      <link>https://dev.to/pratikcodes/mono-respositories-in-jsts-what-why-and-how-with-nx-14he</link>
      <guid>https://dev.to/pratikcodes/mono-respositories-in-jsts-what-why-and-how-with-nx-14he</guid>
      <description>&lt;h2&gt;
  
  
  What are Mono Repositories?
&lt;/h2&gt;

&lt;p&gt;A monorepo is a type of software development strategy where all of a project's code is stored in a single repository (or "repo") instead of being split across multiple repos. This means that all of a project's code, including different components or modules, is stored in the same place. This can make it easier for developers to work on multiple parts of a project at the same time, and can also make it easier to manage and organize the code.&lt;/p&gt;

&lt;p&gt;In the context of JavaScript and TypeScript (JS/TS) applications, using a monorepo can make it easier to manage and organize the various packages and dependencies that make up a project. For example, if you're building a React or Node.js application, you might have several different components or modules that make up the application, such as a login page, a dashboard, and a settings page.&lt;/p&gt;

&lt;p&gt;With a monorepo, all of these different components and modules can be stored in the same repository, which can make it easier for developers to work on multiple parts of the project at the same time. Additionally, it can make it easier to manage and update the various dependencies that are used across the different components and modules, since they are all stored in the same place.&lt;/p&gt;

&lt;p&gt;It also can have advantages when it comes to building and testing of the application. For example, you can set up scripts that run tests across all the packages at once, or that build the entire application at once. This can save a lot of time, especially as the number of packages and modules in the application grows.&lt;/p&gt;

&lt;p&gt;Overall, a monorepo can be a powerful tool for managing and organizing the various parts of a JS/TS application, and can make it easier for developers to work together on a project.&lt;/p&gt;

&lt;p&gt;When using a monorepo, there are several things that can be shared across different parts of the project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code: All of the code for a project, including different components or modules, can be stored in the same repository. This makes it easy for developers to work on multiple parts of the project at the same time and also makes it easy to manage and organize the code.&lt;/li&gt;
&lt;li&gt;Dependencies: A monorepo can make it easier to manage and update the various dependencies that are used across different components and modules. For example, if you have a package that is used by multiple parts of the project, you can update it in one place and it will be updated everywhere it's used.&lt;/li&gt;
&lt;li&gt;Build and test scripts: A monorepo can make it easy to set up scripts that build and test the entire application at once, or that run tests across all the packages at once. This can save a lot of time, especially as the number of packages and modules in the application grows.&lt;/li&gt;
&lt;li&gt;Collaboration: With a monorepo, all developers can work on the same codebase and it's easy to see all the changes that are being made. This can help to improve collaboration and make it easier to keep track of what is being worked on.&lt;/li&gt;
&lt;li&gt;Versioning: Monorepo also allows to version all packages at once, so that if any package is updated, the version of the whole monorepo will be updated as well.&lt;/li&gt;
&lt;li&gt;Deployment: With monorepo, it becomes easy to deploy different parts of the application to different environments, since all the code is stored in the same repository.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's worth noting that while a monorepo can provide many benefits, it also has its own set of challenges, such as managing the size of the codebase and keeping track of the different dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why should you use a Monorepo setup?
&lt;/h2&gt;




&lt;p&gt;&lt;strong&gt;You should use a monorepo if it aligns with your project's specific needs and goals. Some reasons to use a monorepo include:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Improved code organization: With all of the code for a project stored in the same repository, it can make it easier to manage and organize the code, especially as the project grows in size and complexity.&lt;/li&gt;
&lt;li&gt;Better collaboration: A monorepo allows all developers to work on the same codebase, which can make it easier to keep track of what is being worked on and who is working on what.&lt;/li&gt;
&lt;li&gt;Simplified dependency management: A monorepo can make it easier to manage and update the various dependencies that are used across different components and modules.&lt;/li&gt;
&lt;li&gt;Speed: Monorepo allows to build, test and deploy all packages at once, which can save a lot of time and effort.&lt;/li&gt;
&lt;li&gt;Versioning: Monorepo also allows to version all packages at once, so that if any package is updated, the version of the whole monorepo will be updated as well.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Additionally, if you have a large and complex application, with a lot of inter-dependencies between different packages, a monorepo can make it easier to manage and maintain the codebase over time.&lt;/p&gt;

&lt;p&gt;It's important to note that while a monorepo can provide many benefits, it also has its own set of challenges and it's important to weigh the advantages and disadvantages before deciding whether or not to use a monorepo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantages of using a Monorepo Setup
&lt;/h2&gt;




&lt;p&gt;Some disadvantages of using a monorepo include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Increased complexity: Managing a monorepo can be more complex than managing multiple smaller repositories, especially as the number of packages and modules in the project grows.&lt;/li&gt;
&lt;li&gt;Large codebase: Monorepo can become very large and difficult to navigate, which can make it more challenging for developers to find the code they need.&lt;/li&gt;
&lt;li&gt;Build time: Build time can become longer as the number of packages and modules in the project increases.&lt;/li&gt;
&lt;li&gt;Limited isolation: With monorepo, it can be more difficult to isolate different parts of the application, which can make it more challenging to deploy different parts of the application to different environments.&lt;/li&gt;
&lt;li&gt;Limited scalability: Monorepo can be less scalable as the number of packages and modules in the project grows, which can make it more difficult to manage and maintain the codebase over time.&lt;/li&gt;
&lt;li&gt;Limited flexibility: Monorepo may not be suitable for certain types of projects, such as projects with multiple teams working on different parts of the codebase, and projects with large number of contributors.&lt;/li&gt;
&lt;li&gt;Limited privacy: Monorepo's usually have all the codebase in a single repository, so it may become difficult to restrict access to certain parts of the codebase.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's also worth noting that with monorepo, it becomes difficult to separate the concerns of different parts of the application, so it can become a problem when trying to reuse some parts of the codebase in other projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create Monorepo?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Manual Process
&lt;/h3&gt;




&lt;p&gt;It is also possible to create a monorepo without using a third-party tool like Lerna or NX. Here are the steps to create a monorepo manually:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new directory for your monorepo:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;my-monorepo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Change into the new directory:
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a new package.json file:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a new directory for your packages:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;packages
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Change into the packages directory:
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a new package inside the packages directory:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Repeat step 6 for each additional package you want to add to the monorepo.&lt;/li&gt;
&lt;li&gt;Add the package's names in the main package.json 'workspaces' field.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now you have a monorepo set up with multiple packages inside the packages directory. You can now develop and manage these packages together as a single repository.&lt;/p&gt;

&lt;p&gt;Note that without a tool like Nx or Lerna, you will need to manually manage dependencies, versioning and publishing of the packages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create with Nx
&lt;/h3&gt;




&lt;p&gt;Nx is a set of extensible dev tools for monorepos that helps developers create and manage large-scale, full-stack web applications.&lt;/p&gt;

&lt;p&gt;To create a monorepo using Nx and a React app inside it, you can use the following commands:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install the Nx CLI globally:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; @nrwl/cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a new Nx workspace:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-nx-workspace myworkspace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this you will be propmpted with option like Package-based Monorepo, Integrated Monorepo or Standalone. Select Package based here and continue.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vm8qgoftxbysc50fqb8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7vm8qgoftxbysc50fqb8.png" alt="Image description" width="800" height="285"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To know more about the option please visit this link from the offical documentation of Nx - &lt;a href="https://nx.dev/concepts/integrated-vs-package-based" rel="noopener noreferrer"&gt;docs&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Change into the new workspace directory:
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a new React app inside the workspace:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nx generate @nrwl/react:app myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run the app:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nx run myapp:serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example and explaination of Monorepo
&lt;/h2&gt;




&lt;p&gt;&lt;a href="https://github.com/nrwl/nx-examples" rel="noopener noreferrer"&gt;GitHub - nrwl/nx-examples: Example repo for Nx workspace&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhlarqhi436439fce8n5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhlarqhi436439fce8n5w.png" alt="Image description" width="800" height="662"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a example of how a monorepo would look like you can think &lt;code&gt;app&lt;/code&gt; folder as different apps you have and &lt;code&gt;libs&lt;/code&gt; folder consisting of common code/repositories you have like common components, utils etc etc.&lt;/p&gt;

&lt;p&gt;Another complex and good example is Nextjs - &lt;a href="https://github.com/vercel/next.js/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;




&lt;p&gt;&lt;a href="https://nx.dev/getting-started/intro" rel="noopener noreferrer"&gt;Intro to Nx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=Bw_tmWEaaIU&amp;amp;ab_channel=JackHerrington" rel="noopener noreferrer"&gt;No BS TS #32 - Monorepos with NX&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=9iU_IE6vnJ8&amp;amp;t=44s&amp;amp;ab_channel=Fireship" rel="noopener noreferrer"&gt;Monorepos - How the Pros Scale Huge Software Projects // Turborepo vs Nx&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://semaphoreci.com/blog/what-is-monorepo" rel="noopener noreferrer"&gt;What is monorepo? (and should you use it?) - Semaphore&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Understanding React's UI Rendering Process (understanding virtual dom in depth)</title>
      <dc:creator>Pratik Tiwari</dc:creator>
      <pubDate>Sat, 27 Nov 2021 16:04:38 +0000</pubDate>
      <link>https://dev.to/pratikcodes/understanding-reacts-ui-rendering-process-understanding-virtual-dom-in-depth-2842</link>
      <guid>https://dev.to/pratikcodes/understanding-reacts-ui-rendering-process-understanding-virtual-dom-in-depth-2842</guid>
      <description>&lt;p&gt;Before getting started let's discuss this basic question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does react do?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React allows you to write maintainable and performant code by using the concept of components. Components allow you to focus on describing the UI you want. rather than focusing on the details of how the UI actually gets inserted into the page.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Simple Component using JSX
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F967sd3n7r7bgo49cvgg9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F967sd3n7r7bgo49cvgg9.jpg" alt="Image description" width="428" height="211"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This component internally returns a react entity called elements which kind of looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92f3bf0npxhp2gs7powm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92f3bf0npxhp2gs7powm.jpg" alt="Image description" width="644" height="580"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's just a plain object. Let's understand the properties one after another.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;type&lt;/strong&gt;&lt;br&gt;
The type property is a string reference to the HTML tag. React internally calls it a Component Element.&lt;br&gt;
When we do import &lt;strong&gt;main&lt;/strong&gt; from "../components/main" the name of the component becomes the type that is imported.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;key&lt;/strong&gt;&lt;br&gt;
Used to uniquely identify elements among siblings.&lt;br&gt;
This is created when we are manually creating a bunch of children's i.e when we map through the array and render a bunch of components with different data. We use a key while rendering those and hence the key is substituted to this property.&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&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="k"&gt;this&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;data&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;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;i&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&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;i&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Test&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;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;ref&lt;/strong&gt;&lt;br&gt;
ref is reference to a actual DOM node. If you ever have used the create ref function or the useRef hook that's where this values end up.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;$$typeof&lt;/strong&gt;&lt;br&gt;
This is actually a safety feature. Its values are always a symbol. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol" rel="noopener noreferrer"&gt;What is a symbol?&lt;/a&gt;&lt;br&gt;
So if you have a compromised server that you are making an API call and you get back some data and you try to render it through your components. React will straight up reject that.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Props&lt;/strong&gt;&lt;br&gt;
In our case, we just had 1 child that is an h1 hence type is an h1. key and ref are null&lt;br&gt;
And its children was a text string with "look ma!" and with id title.&lt;br&gt;
Children can be an object or array of an objects.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Review
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;So writing to DOM and rendering DOM is a very expensive task. This is where react comes in.&lt;/li&gt;
&lt;li&gt;This object and its children that is known as &lt;strong&gt;virtual dom&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Is it really expensive to write to the dom but its really easy to generate these objects and they can do it super fast.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Reconciliation.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Reconciliation is &lt;strong&gt;the process through which React updates the DOM&lt;/strong&gt;. When a component's state changes, React has to calculate if it is necessary to update the DOM. It does this by creating a virtual DOM and comparing it with the current DOM. In this context, the virtual DOM will contain the new state of the component.&lt;/li&gt;
&lt;li&gt;React creates a tree of elements every time the render function is called. So to be efficient we need a way to tell what's the difference between the DOM and the virtual DOM so that we are only changing the Elements in the DOM that needs to be changed.&lt;/li&gt;
&lt;li&gt;Reconciliation houses the diffing algorithm to understand what part of the DOM needs to be replaced.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxmrtm0taf1kkthz8pck.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faxmrtm0taf1kkthz8pck.jpg" alt="Image description" width="656" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Suppose we have a list of product list and we clicked to get one individual product.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As we clicked the element product list goes away hence it's also removed from the DOM.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is a difference between the native DOM element and the component element.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Reconciliation - DOM element:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftfg63u359zki2v6y6rc6.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftfg63u359zki2v6y6rc6.jpg" alt="Image description" width="583" height="122"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Over here as the DOM element class name is changed. React find the DOM node and update the class name and nothing else and it will recurse on any children if there are any.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Reconciliation- Component element:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f40gzflcgrhvsncqx5y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f40gzflcgrhvsncqx5y.jpg" alt="Image description" width="592" height="163"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React updates the prop of the underlying component instance to match the new element. And the render method is called. The diff algorithm recurses on the old result and the new result until the end of the tree.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Reconciliation - Children&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6tlli7tvttocgf1xi061.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6tlli7tvttocgf1xi061.jpg" alt="Image description" width="560" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In this scenario reacts goes and see for the first and element and checks in the old result both are the same and hence moves to next the next again same now when it moves to next it sees third these were not present in the DOM before hence react appends it to the DOM.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What if we change the sequence?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7950w72yg9w2xjxgjzln.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7950w72yg9w2xjxgjzln.jpg" alt="Image description" width="599" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Over here we see the sequence the older items are there is just we have added a new DOM node and changed the sequence but react doesn't understand this. It goes and sees the first node is changed and remove the previous DOM node that is star wars and replace it with Spaceballs similarly for the second and sees the third has been added hence adding the third one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here we see we are rendering all of the elements again and again but we can save these things with keys.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmf1t0n3ps5sd8hgp5rgt.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmf1t0n3ps5sd8hgp5rgt.jpg" alt="Image description" width="444" height="124"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;over here it has keys with it now lets see what happens if we append 1 DOM element to the beginning of this unordered list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmtajflyfzgl1ndldbpo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmtajflyfzgl1ndldbpo.jpg" alt="Image description" width="487" height="157"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now react goes down the tree and sees key best has start wars which was already present, second best with star trek is also present but actual best with spaceballs was not present and it as been added hence we will add that to the DOM node.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Rendering
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqw5ow0kseuoplsrg7mjw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqw5ow0kseuoplsrg7mjw.jpg" alt="Image description" width="430" height="219"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;so here the render function of the React DOM is responsible for creating the react virtual DOM create the element tree which we discussed in the beginning and add that to the actual DOM.&lt;/li&gt;
&lt;li&gt;FYI react and react dom is two different library hence react work is just to do the diffing part not anything more than that. React DOM creates the virtual DOM and append to the actual DOM.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  React Fiber
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The actual render happens with react fiber.&lt;/li&gt;
&lt;li&gt;React fiber sits between the the element and the DOM node and inserts the element to the DOM node.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Conclusion:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;We write a component.&lt;/li&gt;
&lt;li&gt;We get a component instance.&lt;/li&gt;
&lt;li&gt;Its has state it takes props it computes it.&lt;/li&gt;
&lt;li&gt;Then it generates a tree of elements. That tree of element is our virtual dom.&lt;/li&gt;
&lt;li&gt;And with that the reconciliation process starts.&lt;/li&gt;
&lt;li&gt;Then its handed of to the rendering process.&lt;/li&gt;
&lt;li&gt;Then the react DOM takes it and actually generates the DOM node with React fiber.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Reference:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=i793Qm6kv3U" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=i793Qm6kv3U&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Extra Materials to learn from about the topic:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.velotio.com/engineering-blog/react-fiber-algorithm" rel="noopener noreferrer"&gt;An Introduction to React Fiber - The Algorithm Behind React&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/reactjs-reconciliation/" rel="noopener noreferrer"&gt;ReactJS Reconciliation - GeeksforGeeks&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactdom</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Why Golang ?</title>
      <dc:creator>Pratik Tiwari</dc:creator>
      <pubDate>Sat, 11 Sep 2021 12:28:39 +0000</pubDate>
      <link>https://dev.to/pratikcodes/why-go-31k4</link>
      <guid>https://dev.to/pratikcodes/why-go-31k4</guid>
      <description>&lt;p&gt;Recently I have been exposed to golang considering the radical popularity of the language and talks about how it is very efficient in building microservices. I have been learning about microservices and wanted to experience golang as the syntax is comparatively easier and very similar to javascript. &lt;/p&gt;

&lt;p&gt;But the question is understanding why go? This article answers the question. I have understood this question and have taken references from various sources to answer this question precisely.&lt;/p&gt;




&lt;h3&gt;
  
  
  Open Source and Simple
&lt;/h3&gt;

&lt;p&gt;I think it is really very important for a language to be open source in order for the dev community to be indulged actively in the growth of the language and also to interact with it and contribute.&lt;/p&gt;

&lt;p&gt;It has a clean syntax. It's a function programming-based language hence people coming from javascript have a very easy time learning it. Also as the syntax is easy it's easier for new dev and people to be onboarded on the projects and start working on them.&lt;/p&gt;

&lt;p&gt;It is easy to learn, concise, expressive, and readable.&lt;/p&gt;




&lt;h3&gt;
  
  
  Fast
&lt;/h3&gt;

&lt;p&gt;Go is compiled to machine code, it will naturally outperform languages that are interpreted or have virtual runtimes. Go programs also compile extremely fast, and the resulting binary is very small.&lt;/p&gt;

&lt;p&gt;Golang can boast speeds of close to four times quicker than its interpreted and dynamic friends. That said, very little can touch C++ (and most C languages) when it comes to speed. All of the time spent coding and compiling pays off here.&lt;/p&gt;




&lt;h3&gt;
  
  
  Concurrency
&lt;/h3&gt;

&lt;p&gt;Golang has efficient concurrency, like C, C++, Java, and at the same time concurrency in Go is done much easier thanks to &lt;a href="https://golangbot.com/goroutines/" rel="noopener noreferrer"&gt;goroutines&lt;/a&gt;, channels, and garbage collection.&lt;/p&gt;

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

&lt;p&gt;Concurrency is the capability to deal with lots of things at once. It's best explained with an example.&lt;/p&gt;

&lt;p&gt;Let's consider a person jogging. During his morning jog, let's say his shoelaces become untied. Now the person stops running, ties his shoelaces, and then starts running again. This is a classic example of concurrency. The person is capable of handling both running and tying shoelaces, that is the person is able to deal with lots of things at once :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concurrency is not parallelism&lt;/strong&gt;. In case you are confused between them go through this article in the reference to understand that in deep.&lt;/p&gt;

&lt;p&gt;Additional References:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://golangbot.com/concurrency/" rel="noopener noreferrer"&gt;Understanding Concurrency in Go (Golang) | golangbot.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://golangbot.com/goroutines/" rel="noopener noreferrer"&gt;Goroutines - Concurrency in Golang | golangbot.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=yNOe3STbtGE" rel="noopener noreferrer"&gt;Concurrency in Go is SO EASY! - Go / Golang Concurrency Tutorial&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Garbage collector
&lt;/h3&gt;

&lt;p&gt;A form of automatic memory management that has a significant influence on performance and helps to make concurrency more efficient.&lt;/p&gt;

&lt;p&gt;want to understand garbage collection in deep?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/safetycultureengineering/an-overview-of-memory-management-in-go-9a72ec7c76a8" rel="noopener noreferrer"&gt;An overview of memory management in Go&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Fewer Bugs
&lt;/h3&gt;

&lt;p&gt;Since it is a compiled language, with very strict typization, and it is statically typed, developers have to be more accurate and attentive, so the code is neater and safer.&lt;/p&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;So concluding GO is really scalable due to goroutines(concurrency).&lt;/p&gt;

&lt;p&gt;It is performance efficient. Go developers have said that Go shows the same characteristics of C and C++ in terms of speed and performance, which is really good. Also due to its amazing memory management, this also plays an important role in performance.&lt;/p&gt;

&lt;p&gt;And top of all these it has a simple syntax and GO community is growing exponentially.&lt;/p&gt;

&lt;p&gt;Hence it becomes an amazing backend technology for dev and companies who are open to incorporate the new tech.&lt;/p&gt;

</description>
      <category>go</category>
      <category>microservices</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Start/stop Upgrade/downgrade EC2 instances with lambda functions</title>
      <dc:creator>Pratik Tiwari</dc:creator>
      <pubDate>Sun, 15 Aug 2021 11:09:07 +0000</pubDate>
      <link>https://dev.to/pratikcodes/start-stop-upgrade-downgrade-ec2-instances-with-lambda-functions-5fn9</link>
      <guid>https://dev.to/pratikcodes/start-stop-upgrade-downgrade-ec2-instances-with-lambda-functions-5fn9</guid>
      <description>&lt;p&gt;Lambda functions are one of the most common services by AWS that comes really in handy. One of the very useful applications is the automation of starting/stopping and upgrading these instances.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example.
&lt;/h3&gt;

&lt;p&gt;You want to start instances for a failover you can use the start and stop instances using lambda function by triggering the function use cloud watch alarm.&lt;/p&gt;

&lt;p&gt;You want to scale up (vertically) an instance when it has a CPU utilization of more than some amount or has more traffic.&lt;br&gt;
If you want to start and stop a dev instance in the night and you want to automate this process you can use a cloud watch event with a corn job to this.&lt;/p&gt;

&lt;p&gt;It has more and endless application&lt;/p&gt;

&lt;p&gt;Enough of all talk let’s get into it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an IAM role with a policy that has full access for lambda. (IMPORTANT)&lt;/li&gt;
&lt;li&gt;After Role is created create a lambda function with python 3.8 and use the role that you created above.&lt;/li&gt;
&lt;li&gt;Use this code for the lambda function:&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Start Instances:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;import boto3&lt;br&gt;
region = 'us-west-1'&lt;br&gt;
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']&lt;br&gt;
ec2 = boto3.client('ec2', region_name=region)&lt;br&gt;
def lambda_handler(event, context):&lt;br&gt;
    ec2.stop_instances(InstanceIds=instances)&lt;br&gt;
    print('stopped your instances: ' + str(instances))&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Stop Instances:
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;import boto3&lt;br&gt;
region = 'us-west-1'&lt;br&gt;
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']&lt;br&gt;
ec2 = boto3.client('ec2', region_name=region)&lt;br&gt;
def lambda_handler(event, context):&lt;br&gt;
    ec2.start_instances(InstanceIds=instances)&lt;br&gt;
    print('started your instances: ' + str(instances))&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Upgrade/downgrade(modify)
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;import boto3&lt;br&gt;
ec2 = boto3.client('ec2')&lt;br&gt;
instance_id = 'i-05ca5f05f965b3a4b'&lt;br&gt;
ec2.stop_instances(InstanceIds=[instance_id])&lt;br&gt;
waiter=ec2.get_waiter('instance_stopped')&lt;br&gt;
waiter.wait(InstanceIds=[instance_id])&lt;br&gt;
ec2.modify_instance_attribute(InstanceId=instance_id, Attribute='instanceType', Value='t2.small')&lt;br&gt;
ec2.start_instances(InstanceIds=[instance_id])&lt;br&gt;
print("instances modified and started")&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;Official AWS documentation:&lt;br&gt;
&lt;a href="https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/" rel="noopener noreferrer"&gt;https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/&lt;/a&gt;&lt;br&gt;
Video explanation:&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=oISuE16pGFQ" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=oISuE16pGFQ&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>lambdafunctions</category>
      <category>ec2</category>
      <category>instances</category>
    </item>
    <item>
      <title>Javascript concept handbook for interviews</title>
      <dc:creator>Pratik Tiwari</dc:creator>
      <pubDate>Sat, 14 Aug 2021 17:44:07 +0000</pubDate>
      <link>https://dev.to/pratikcodes/javascript-concept-handbook-for-interviews-b0j</link>
      <guid>https://dev.to/pratikcodes/javascript-concept-handbook-for-interviews-b0j</guid>
      <description>&lt;p&gt;This series of questions and answers is all you need for all the concept and theory-based questions for your javascript interviews.&lt;/p&gt;

&lt;p&gt;Various resources have been used for the reference of the answers of the following question and are mentioned as well for further reference.&lt;/p&gt;

&lt;p&gt;Various forms/places you can get this blog.&lt;/p&gt;

&lt;p&gt;PDF: &lt;a href="https://drive.google.com/file/d/1MzoN4QZsSTOAY8naHCAQM47OTVlhe2E9/view?usp=sharing" rel="noopener noreferrer"&gt;https://drive.google.com/file/d/1MzoN4QZsSTOAY8naHCAQM47OTVlhe2E9/view?usp=sharing&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notion:&lt;br&gt;
&lt;a href="https://pratiktiwari.notion.site/Javascript-Questions-debadba90cf44b508f6553c444243945" rel="noopener noreferrer"&gt;https://pratiktiwari.notion.site/Javascript-Questions-debadba90cf44b508f6553c444243945&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
