<?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: Kanav Puri</title>
    <description>The latest articles on DEV Community by Kanav Puri (@kanav).</description>
    <link>https://dev.to/kanav</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%2F992431%2F9bff6681-afa8-4c99-b1f9-62f015e958ad.png</url>
      <title>DEV Community: Kanav Puri</title>
      <link>https://dev.to/kanav</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kanav"/>
    <language>en</language>
    <item>
      <title>Linting Unveiled: Elevating Code Quality and Consistency</title>
      <dc:creator>Kanav Puri</dc:creator>
      <pubDate>Fri, 13 Oct 2023 11:13:42 +0000</pubDate>
      <link>https://dev.to/kanav/linting-unveiled-elevating-code-quality-and-consistency-2ili</link>
      <guid>https://dev.to/kanav/linting-unveiled-elevating-code-quality-and-consistency-2ili</guid>
      <description>&lt;p&gt;In the ever-evolving landscape of software development, crafting robust and reliable code is not just an aspiration; it's an imperative. In this digital era where software powers our lives, even the smallest glitch can have significant consequences. Enter linting – the unsung hero that tirelessly ensures your codebase remains a bastion of quality. Linting is like a vigilant guardian that scans our code, ensuring it adheres to best practices, standards, and style guidelines. In this blog, we'll embark on a journey to demystify linting, exploring its uses, applications, and how it can transform your codebase into a masterpiece.&lt;br&gt;
 &lt;/p&gt;

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

&lt;p&gt;Linting is a process in software development that involves the use of tools called Linters to analyze source code for potential errors, bugs, stylistic inconsistencies, and adherence to coding standards. The term "lint" originally comes from a Unix utility that scanned C code for errors and potential issues.&lt;br&gt;
 &lt;br&gt;
&lt;a href="https://media.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%2F7cq8uv83k4f1bbpuwfbh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F7cq8uv83k4f1bbpuwfbh.png" alt="Coding Meme"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;br&gt;
Linters typically work by parsing the source code and looking for patterns that match known errors or style violations. For example, a linter might look for missing semicolons, unused variables, or functions that are too long.&lt;/p&gt;

&lt;p&gt;Once a linter has identified a potential problem, it will generate a report that tells the developer what the problem is and where it is located in the code. The developer can then fix the problem and run the linter again to make sure that it has been resolved.&lt;br&gt;
 &lt;/p&gt;

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

&lt;p&gt;Linting plays a pivotal role in the software development lifecycle. Let's delve deeper into why it matters:&lt;br&gt;
 &lt;br&gt;
&lt;a href="https://media.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%2F4rv0vw1xcu6ub6kglik2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F4rv0vw1xcu6ub6kglik2.png" alt="SonarLint Cognitive Complexity Error"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Error Prevention&lt;/strong&gt;&lt;br&gt;
Linters analyze code to identify syntax errors, runtime errors, and potential logical errors before the code is executed. This helps in catching and addressing issues early in the development process, reducing the likelihood of bugs and crashes in the final product.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Consistency&lt;/strong&gt;&lt;br&gt;
In a team setting, every developer may have their own coding style. Linting enforces a consistent style guide, making the codebase more readable and maintainable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Quality Improvement&lt;/strong&gt;&lt;br&gt;
Linters provide suggestions and recommendations to improve code quality. They can identify code smells, anti-patterns, and potential performance bottlenecks. This helps developers write cleaner, more efficient code.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Applications of Linting
&lt;/h2&gt;

&lt;p&gt;Leveraging the power of linting extends to various programming languages and technologies, ensuring that your code maintains a high level of quality and readability. &lt;/p&gt;

&lt;p&gt;Let's dive into the details of ESLint and SonarLint, two prominent linting tools used in the software development industry.&lt;br&gt;
 &lt;br&gt;
&lt;a href="https://media.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%2F8qrkm0sme1ke12cfuzcb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F8qrkm0sme1ke12cfuzcb.png" alt="ESLint"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;br&gt;
&lt;strong&gt;&lt;a href="https://eslint.org/" rel="noopener noreferrer"&gt;ESLint&lt;/a&gt;: Elevating JavaScript Code Quality&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ESLint is a widely used linting tool in the JavaScript ecosystem. It focuses on identifying and fixing issues in JavaScript code, making sure it adheres to coding standards and best practices.&lt;/p&gt;

&lt;p&gt;Key Features of ESLint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Highly Configurable&lt;/strong&gt;: ESLint allows developers to configure rules based on their project's specific needs. You can enable, disable, or customize rules to match your coding style.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Extensible&lt;/strong&gt; : ESLint supports the use of plugins and custom rules, enabling you to extend its functionality to suit your project requirements. Notably, there are popular ESLint plugins that align with well-established coding standards and styles. Examples include &lt;a href="https://github.com/airbnb/javascript" rel="noopener noreferrer"&gt;Airbnb JS Style Guide&lt;/a&gt; and &lt;a href="https://google.github.io/styleguide/jsguide.html" rel="noopener noreferrer"&gt;Google JS Style Guide&lt;/a&gt;.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Real-time Feedback&lt;/strong&gt;: ESLint can be integrated into your code editor (e.g., Visual Studio Code) to provide real-time feedback as you write code. This immediate feedback helps you catch issues early in the development process.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Support for ECMAScript Features&lt;/strong&gt;: ESLint is capable of understanding the latest ECMAScript (JavaScript) features, ensuring that your code remains up-to-date with the language's evolution.&lt;br&gt;
 &lt;br&gt;
&lt;a href="https://media.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%2Fsy0y3ue60fih95m7291n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fsy0y3ue60fih95m7291n.png" alt="SonarLint"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.sonarsource.com/products/sonarlint/" rel="noopener noreferrer"&gt;SonarLint&lt;/a&gt;: Code Analysis for Multiple Languages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;SonarLint is a versatile code analysis tool that supports multiple programming languages, including Java, C#, JavaScript, Python, and more. It provides comprehensive code quality checks, security analysis, and best practice recommendations.&lt;/p&gt;

&lt;p&gt;Key Features of SonarLint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multi-language Support&lt;/strong&gt;: SonarLint's ability to analyze code in various programming languages makes it valuable for projects with mixed-language codebases.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Code Smells Detection&lt;/strong&gt;: It detects code smells (poorly structured or inefficient code) and security vulnerabilities, helping developers write more robust and secure code.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Detailed Issue Reporting&lt;/strong&gt;: SonarLint generates detailed reports on code issues, categorizing them as bugs, vulnerabilities, or code smells, and provides explanations and suggested fixes.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Continuous Inspection&lt;/strong&gt;: SonarLint can be integrated with SonarQube, a continuous inspection platform, to scale code analysis across the entire development team and provide centralized reporting.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Integrating Linting into Your Workflow
&lt;/h2&gt;

&lt;p&gt;Linting can be used in a variety of ways, including:&lt;br&gt;
 &lt;br&gt;
&lt;a href="https://media.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%2Fas1ymc5ghs7skw1ys10u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fas1ymc5ghs7skw1ys10u.png" alt="ESLint error"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;During development&lt;/strong&gt;&lt;br&gt;
Linters can be integrated into the development workflow to provide real-time feedback to developers. This can help to catch errors and style violations early, before they become more difficult to fix.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pre-commit hooks&lt;/strong&gt;&lt;br&gt;
Linters can be used as pre-commit hooks to prevent developers from committing code that contains errors or style violations. This helps to ensure that the codebase remains in a good state.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Continuous integration&lt;/strong&gt;&lt;br&gt;
Linters can be integrated into continuous integration (CI) pipelines to automatically check code for errors and style violations before it is deployed to production. This helps to ensure that the code that is deployed to production is of high quality.&lt;br&gt;
 &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;Linting is an indispensable tool for every software developer. It acts as a guiding light, leading us towards clean, error-free, and maintainable code. By incorporating linting into your development workflow, you can save time, enhance collaboration, and deliver high-quality software.&lt;/p&gt;

&lt;p&gt;So, the next time you embark on a coding adventure, remember the magic of linting. It's the spell that keeps your codebase enchanting and your development journey smooth. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>productivity</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>No More Nesting: A Guide to Code Simplicity</title>
      <dc:creator>Kanav Puri</dc:creator>
      <pubDate>Tue, 12 Sep 2023 16:15:41 +0000</pubDate>
      <link>https://dev.to/kanav/no-more-nesting-a-guide-to-code-simplicity-3a77</link>
      <guid>https://dev.to/kanav/no-more-nesting-a-guide-to-code-simplicity-3a77</guid>
      <description>&lt;p&gt;Have you ever stumbled upon a tangled web of nested code, feeling like you're lost in a labyrinth of logic? Fear not, for we are about to embark on a journey to simplify your code and enhance its readability. Excessive nesting is a common pitfall in coding, leading to reduced clarity, debugging challenges, and maintenance nightmares. In this blog, we'll explore the drawbacks of code nesting and unveil two powerful techniques to untangle it. So, let's embark on a journey to make your code cleaner and more understandable. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Pitfalls of Nesting
&lt;/h3&gt;

&lt;p&gt;Nesting in code occurs when you have multiple layers of conditional statements, loops, or functions inside each other. While some level of nesting is inevitable, excessive nesting can lead to several issues:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced Readability&lt;/strong&gt;: Deeply nested code is like reading a book within a book. It becomes challenging to follow the logic and understand what's happening.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Debugging Nightmare&lt;/strong&gt;: Debugging nested code can be a daunting task. Identifying the source of a bug becomes increasingly difficult as nesting levels increase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Maintenance Headaches&lt;/strong&gt;: Making changes to deeply nested code is risky. You might unintentionally introduce new bugs, and the fear of breaking something can paralyze development.&lt;/p&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's explore two techniques that can help you conquer nesting and make your code more manageable:&lt;/p&gt;

&lt;h2&gt;
  
  
  Early Return
&lt;/h2&gt;

&lt;p&gt;Early return, also known as "guard clauses", is a coding practice where you exit a function as soon as a certain condition is met. This technique is particularly useful for reducing nesting in conditional statements. &lt;/p&gt;

&lt;p&gt;Imagine you're building a function that checks various conditions for a user registration form. Each condition may result in a different error message or validation status.&lt;/p&gt;

&lt;p&gt;Here's the initial nested approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;validateUserRegistration&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="k"&gt;if&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="k"&gt;if&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="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;6&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Username must be at least 6 characters long.&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Username is required.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&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="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Password must be at least 8 characters long.&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Password is required.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Valid registration 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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid user 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="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;br&gt;&lt;br&gt;
Now, let's apply the &lt;strong&gt;Early Return&lt;/strong&gt; technique to simplify it by using multiple return statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;validateUserRegistration&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="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;user&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid user 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="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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Username is required.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&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="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;6&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Username must be at least 6 characters long.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Password is required.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;if&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="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;8&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Password must be at least 8 characters long.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Valid registration 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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this, you eliminate unnecessary nesting, making the code easier to follow. Early return makes it clear that certain conditions must be met for the function to proceed, reducing cognitive load and improving code readability. &lt;/p&gt;




&lt;h2&gt;
  
  
  Extraction
&lt;/h2&gt;

&lt;p&gt;Extraction involves breaking down complex nested blocks of code into separate functions. This technique not only reduces nesting but also promotes code reusability and maintainability.&lt;/p&gt;

&lt;p&gt;Suppose you have a deeply nested function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;processOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&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="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Process the item&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;requiresApproval&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isUrgent&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="c1"&gt;// Approve urgently&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="c1"&gt;// Approve non-urgently&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="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;p&gt;&lt;br&gt;&lt;br&gt;
Now, let's refactor it using the &lt;strong&gt;Extraction&lt;/strong&gt; technique to reduce nesting and improve code readability:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;processApproval&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isUrgent&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Approve urgently&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Approve non-urgently&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;processItem&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&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;requiresApproval&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Process the item&lt;/span&gt;
        &lt;span class="nx"&gt;processApproval&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;processOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&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="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;processItem&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="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;p&gt;By extracting code into separate functions, you make your code more modular, enabling easier maintenance and debugging. It also enhances code comprehension as functions are named to indicate their purpose, making the code's intent clearer.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;

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

&lt;p&gt;In the world of coding, simplicity is a virtue. Excessive nesting can complicate your code and make it harder to manage. By embracing the &lt;em&gt;&lt;strong&gt;Early Return&lt;/strong&gt;&lt;/em&gt; and &lt;strong&gt;&lt;em&gt;Extraction&lt;/em&gt;&lt;/strong&gt; techniques, you can simplify your code, enhance readability, and transform debugging and maintenance into smooth processes. So, fellow coders, arm yourselves with these tools, and embark on your quest for cleaner, more understandable code. &lt;/p&gt;

&lt;p&gt;This blog was inspired by an insightful YouTube video titled &lt;a href="https://www.youtube.com/watch?v=CFRhGnuXG-4"&gt;Why You Shouldn't Nest Your Code&lt;/a&gt;. In this video, you'll find even more valuable insights and examples related to code nesting. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>cleancode</category>
      <category>programming</category>
    </item>
    <item>
      <title>Unraveling the LeetCode Two Sum Challenge</title>
      <dc:creator>Kanav Puri</dc:creator>
      <pubDate>Mon, 04 Sep 2023 18:35:14 +0000</pubDate>
      <link>https://dev.to/kanav/unraveling-the-two-sum-challenge-5ec3</link>
      <guid>https://dev.to/kanav/unraveling-the-two-sum-challenge-5ec3</guid>
      <description>&lt;p&gt;In the world of coding and problem-solving, there exist challenges that serve as fundamental building blocks of programming knowledge. One such challenge, often encountered by coding enthusiasts and frequently featured in technical interviews, is the &lt;a href="https://leetcode.com/problems/two-sum/"&gt;Two Sum&lt;/a&gt; problem. At first glance, it may appear deceptively simple, but as we delve into its intricacies, you'll discover that it holds the key to honing your problem-solving skills and unlocking the world of efficient algorithms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem Statement
&lt;/h3&gt;

&lt;p&gt;Given an array of integers &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;target&lt;/code&gt;, return indices of the two numbers such that they add up to &lt;code&gt;target&lt;/code&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Brute Force Approach
&lt;/h2&gt;

&lt;p&gt;The most straightforward way to tackle this problem is by using a brute force approach. Here's the basic idea:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Loop through the array.&lt;/li&gt;
&lt;li&gt;For each element, check if there exists another element in the array that, when added to the current element, equals the target sum.
&lt;/li&gt;
&lt;/ol&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;twoSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;i&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="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&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;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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;+&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;j&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Time Complexity:  O(N^2) &lt;/span&gt;
&lt;span class="c1"&gt;// Space Complexity: O(1)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h3&gt;
  
  
  The Downside of Brute Force
&lt;/h3&gt;

&lt;p&gt;While the Brute Force approach is conceptually simple and guarantees a solution, it's not the most efficient way to tackle the Two Sum problem. The nested loops create a time complexity of &lt;code&gt;O(n^2)&lt;/code&gt;, where 'n' represents the number of elements in the array. This means that as the size of the array increases, the time it takes to find a solution grows quadratically. In other words, it becomes slow and impractical for large datasets.&lt;/p&gt;




&lt;h2&gt;
  
  
  Optimized Hash table Approach
&lt;/h2&gt;

&lt;p&gt;To enhance our runtime efficiency, we require a more effective method for verifying the existence of the complement within the array and retrieving its corresponding index. What is the optimal approach for preserving a mapping of each element in the array to its index? The answer lies in employing a hash table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;twoSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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;comp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;nums&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comp&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;obj&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="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;comp&lt;/span&gt;&lt;span class="p"&gt;]];&lt;/span&gt;
    &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;nums&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;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Time Complexity:  O(N) &lt;/span&gt;
&lt;span class="c1"&gt;// Space Complexity: O(N)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can reduce the lookup time from &lt;code&gt;O(n)&lt;/code&gt; to &lt;code&gt;O(1)&lt;/code&gt; by trading space for speed. We achieve faster runtime at the expense of using more memory. In most cases, this tradeoff is acceptable, as modern computers typically have sufficient memory. &lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, the optimized Hash table approach is an elegant and efficient solution to the Two Sum problem. It strikes a balance between time and space complexity, providing a scalable solution for most real-world scenarios.&lt;/p&gt;

&lt;p&gt;Thank you for reading, and happy coding! 🚀&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>leetcode</category>
      <category>interview</category>
    </item>
    <item>
      <title>The Need for Speed: Next.js Performance Overhaul with Polyfills and SWC</title>
      <dc:creator>Kanav Puri</dc:creator>
      <pubDate>Mon, 28 Aug 2023 18:17:00 +0000</pubDate>
      <link>https://dev.to/kanav/the-need-for-speed-nextjs-performance-overhaul-with-polyfills-and-swc-49g4</link>
      <guid>https://dev.to/kanav/the-need-for-speed-nextjs-performance-overhaul-with-polyfills-and-swc-49g4</guid>
      <description>&lt;p&gt;In the fast-paced world of web development, staying up to date with the latest tools and technologies can make all the difference in delivering a cutting-edge user experience. As a frontend web developer, you're likely familiar with the Next.js framework, known for its ease of use and server-side rendering capabilities. However, did you know that a simple upgrade to the latest version and a couple of strategic changes can lead to a significant boost in your app's performance? In this blog, we'll delve into a real-world case study of upgrading a Next.js production app, uncovering how a version update, elimination of duplicate polyfills, and the embrace of the SWC compiler can revolutionize your app's loading speed and development workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing Duplicate Polyfills: Streamlining Browser Support
&lt;/h2&gt;

&lt;p&gt;Polyfills are essential components in web development, bridging the gap between modern functionality and older browsers that lack native support. However, not all polyfills are created equal, and redundancy can creep in, impacting your app's performance. Next.js, being a forward-thinking framework, already comes equipped with polyfills to cater to modern browsers without any additional configuration. This means you can trim down your app's payload by getting rid of duplicate polyfills.&lt;/p&gt;

&lt;p&gt;In the latest versions of Next.js, targeting specific browsers or features is a breeze using the &lt;a href="https://browsersl.ist/" rel="noopener noreferrer"&gt;Browserslist&lt;/a&gt; configuration in your package.json file. The latest version of Next.js (v13) uses the following configuration by default:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"browserslist"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"chrome 64"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"edge 79"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"firefox 67"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"opera 51"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"safari 12"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


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

&lt;/div&gt;

&lt;p&gt;By aligning your polyfills with the supported browser versions, you ensure optimal performance for your target audience while shedding unnecessary code weight.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embracing the SWC Compiler: Unleashing Rapid Compilation
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fpjbm2naq5k3uoyeyr2tn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fpjbm2naq5k3uoyeyr2tn.png" alt="SWC Logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next.js has long been a staple for server-rendered applications, but as the demand for faster development cycles grows, so does the need for speedier compilation times. Enter the SWC compiler – a Rust-based powerhouse that transforms your modern JavaScript code into a format that can run seamlessly across various environments, all while outperforming the traditional Babel transcompiler.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fz7aqdcd77j3kvild4azo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fz7aqdcd77j3kvild4azo.png" alt="SWC performance metrics"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The switch from Babel to SWC in Next.js v12 opens the doors to a realm of performance improvements. SWC leverages Rust's low-level prowess to compile and execute faster than its JavaScript-based counterparts. &lt;a href="https://swc.rs/blog/perf-swc-vs-babel" rel="noopener noreferrer"&gt;Performance benchmarks&lt;/a&gt; consistently demonstrate SWC's superiority, making it a clear winner when it comes to delivering blazing-fast compilation times. With SWC, your development workflow gets a turbo boost, with up to &lt;strong&gt;5x faster&lt;/strong&gt; Fast Refresh – enabling you to iterate and refine your code with unparalleled efficiency.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Power of Real Production Insights
&lt;/h2&gt;

&lt;p&gt;Let's dive into a real-world scenario that highlights the transformative power of these upgrades. Our Next.js app, living in the wilds of production, grappled with an initial load JavaScript size exceeding 160KB. Through a meticulous upgrade process, a judicious removal of redundant polyfills, and the introduction of the SWC compiler, a spectacular transformation unfolded. The initial load size nosedived to under 110KB – an impressive &lt;em&gt;&lt;strong&gt;30% reduction&lt;/strong&gt;&lt;/em&gt; that wasn't just about digits, but about delighting users.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ff50vkued3ea7mdz29i3h.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ff50vkued3ea7mdz29i3h.jpg" alt="High Speed Car"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The implications were profound. A leaner initial load empowered our app to race onto screens, offering lightning-fast experiences even on devices and connections that had previously posed challenges.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: Elevate Your Next.js App to New Heights
&lt;/h2&gt;

&lt;p&gt;As a frontend web developer, your journey involves constantly pushing the boundaries of what your apps can achieve. Upgrading your Next.js project, eliminating redundant polyfills, and adopting the SWC compiler represent pivotal steps in this evolution. With these changes, you're not only optimizing performance but also redefining your development workflow. Your app becomes a testament to cutting-edge technology, delivering unrivaled speed and responsiveness to your users.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F6d5c3s27bhb4exfunyx5.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F6d5c3s27bhb4exfunyx5.jpeg" alt="cutting-edge technology"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, what are you waiting for? Take the leap, embrace these enhancements, and watch your Next.js app soar to new heights. Your users and your development team will thank you for the snappy experiences and streamlined workflows that lie ahead. It's time to turbocharge your Next.js app and leave a trail of awe-inspiring performance in your wake.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>performance</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
