<?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: Amartya Jha</title>
    <description>The latest articles on DEV Community by Amartya Jha (@codeantai).</description>
    <link>https://dev.to/codeantai</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%2F1274846%2Fc5c2a61b-3061-47d9-ae9e-77818bad8bea.jpeg</url>
      <title>DEV Community: Amartya Jha</title>
      <link>https://dev.to/codeantai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codeantai"/>
    <language>en</language>
    <item>
      <title>Fixing Complex Functions and Making Code Modular and Reusable</title>
      <dc:creator>Amartya Jha</dc:creator>
      <pubDate>Wed, 07 Aug 2024 20:27:22 +0000</pubDate>
      <link>https://dev.to/codeantai/fixing-complex-functions-and-making-code-modular-and-reusable-1pip</link>
      <guid>https://dev.to/codeantai/fixing-complex-functions-and-making-code-modular-and-reusable-1pip</guid>
      <description>&lt;p&gt;Learn how to fix complex functions and make your code modular and reusable with these expert tips and best practices for cleaner, more maintainable code.&lt;/p&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the world of software development, writing clean, modular, and reusable code is a hallmark of good programming practice. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Fixing Complex Functions Matters&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Complex functions are often difficult to debug, test, and maintain. They can slow down development, introduce bugs, and make it harder for new team members to understand the codebase. Simplifying these functions not only improves readability but also enhances the overall performance and scalability of the software.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Identifying Complex Functions&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Signs of Complexity&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Length:&lt;/strong&gt; Functions that are excessively long.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiple Responsibilities:&lt;/strong&gt; Functions performing multiple tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nested Loops and Conditionals:&lt;/strong&gt; Deeply nested loops and conditional statements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High Cyclomatic Complexity:&lt;/strong&gt; Numerous paths through the function.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Poor Naming Conventions:&lt;/strong&gt; Non-descriptive variable and function names.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Code Linters:&lt;/strong&gt; Tools like ESLint, Pylint, and JSHint can help identify complex code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Metrics Tools:&lt;/strong&gt; Tools such as SonarQube and CodeClimate provide metrics to measure complexity.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Steps to Fix Complex Functions&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Break Down the Function&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Single Responsibility Principle&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Each function should perform a single task. Breaking down a complex function into smaller, single-purpose functions can make the code more understandable and easier to manage.&lt;/p&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Read data from source
&lt;/span&gt;    &lt;span class="c1"&gt;# Validate data
&lt;/span&gt;    &lt;span class="c1"&gt;# Transform data
&lt;/span&gt;    &lt;span class="c1"&gt;# Save data to database
&lt;/span&gt;    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Break it down into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;read_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;validate_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;transform_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;save_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;read_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;validate_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;transformed_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;transform_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;save_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transformed_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Refactor Loops and Conditionals&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Nested loops and conditionals can often be refactored into simpler constructs or moved into separate functions to improve readability.&lt;/p&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_valid&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="nf"&gt;process_item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refactor to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;process_valid_items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;valid_items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_valid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;valid_items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;process_item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Use Descriptive Names&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Names should convey the purpose of the variable or function. Avoid abbreviations and ensure the names are meaningful.&lt;/p&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calculate_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Making Code Modular&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is Modular Code?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Modular code is structured in a way that separates functionality into independent, interchangeable modules. Each module encapsulates a specific piece of functionality, making the codebase easier to manage, test, and reuse.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Principles of Modular Code&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Encapsulation:&lt;/strong&gt; Keep related data and functions together.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separation of Concerns:&lt;/strong&gt; Divide the program into distinct features that overlap as little as possible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusability:&lt;/strong&gt; Modules should be designed to be reusable in different parts of the application or even in different projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Creating Modules&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Organize by Feature&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Group related functions and data together into modules that represent a specific feature or functionality.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# user_management.py
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;delete_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="c1"&gt;# product_management.py
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_product&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;remove_product&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;Use Interfaces&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Define clear interfaces for your modules to ensure they can interact with other parts of the codebase in a predictable way.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# user_management.py
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserManager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;delete_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Enhancing Reusability&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;DRY Principle&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The "Don't Repeat Yourself" principle emphasizes the importance of reducing duplication. Reusable code should abstract common functionality into functions or classes that can be used throughout the codebase.&lt;/p&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Email sending logic
&lt;/span&gt;    &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_notification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Notification sending logic
&lt;/span&gt;    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Abstract common logic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message_type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;message_type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Email sending logic
&lt;/span&gt;    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;message_type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;notification&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Notification sending logic
&lt;/span&gt;    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Use Libraries and Frameworks&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Leverage existing libraries and frameworks that provide reusable components to avoid reinventing the wheel.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fetch_data_from_api&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Write Generic Functions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Write functions that can handle a variety of inputs and use parameters to control behavior.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sort_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Testing and Documentation&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Unit Testing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Ensure each module and function is thoroughly tested with unit tests to guarantee they work correctly in isolation.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;unittest&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestUserManager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unittest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_create_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;user_manager&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UserManager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user_manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_user&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;John&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assertTrue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Document Your Code&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Comprehensive documentation helps other developers understand how to use your modules and functions. Include comments, docstrings, and external documentation as needed.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calculate_sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Calculate the sum of two numbers.

    :param number1: First number
    :param number2: Second number
    :return: Sum of number1 and number2
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use &lt;a href="https://www.codeant.ai/" rel="noopener noreferrer"&gt;CodeAnt AI&lt;/a&gt; to detect and suggest auto-fixing of complex function.&lt;/p&gt;

&lt;p&gt;It list complex functions in the entire repository with Cyclomatic Complexity.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw90ravrrjosmd2f23uqd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw90ravrrjosmd2f23uqd.png" alt="CodeAnt AI listing complex function" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And then suggest auto-fixing, which is kinda insightful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9pntzebc5cbjmncixvh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl9pntzebc5cbjmncixvh.png" alt="CodeAnt AI suggesting where code is complex and its refactoring" width="800" height="536"&gt;&lt;/a&gt; &lt;/p&gt;

</description>
      <category>codequality</category>
      <category>codereview</category>
    </item>
    <item>
      <title>Why we still have tech-debt?</title>
      <dc:creator>Amartya Jha</dc:creator>
      <pubDate>Sun, 11 Feb 2024 01:06:26 +0000</pubDate>
      <link>https://dev.to/codeantai/why-we-still-have-tech-debt-416l</link>
      <guid>https://dev.to/codeantai/why-we-still-have-tech-debt-416l</guid>
      <description>&lt;p&gt;When a developer creates a pull request, the reviewer checks it for business logic. However, what often gets overlooked are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data structure optimization&lt;/li&gt;
&lt;li&gt;Code security checks&lt;/li&gt;
&lt;li&gt;Function complexity and reusability&lt;/li&gt;
&lt;li&gt;Cleaning up dead and duplicate pieces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's break it down and understand all 3 stages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stage 1 - Development Editor:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We all add linters; they detect issues related to formatting and some anti-patterns. As developers, we see numerous issues, and even some paid linters stack rank the issues based on severity. However, it fails in multiple aspects because developers don't have time to manually fix these issues. Developers want these issues to be auto-fixed, or else they will silence them. Secondly, these static linters lack context about your codebase, so they may not effectively prioritize high-severity, high-impact issues for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stage 2 - Git Hook (Maybe):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some organizations have Git hooks in place to prevent certain bad practices from being pushed. The problem here lies in the depth of the rules, as most rules focus on high-level, framework-specific checks. You can think of these as extensions of your linters, with the added capability that you can't push the code if certain rules are not followed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stage 3 - Pull Requests (PRs):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, you have pushed your changes and created a PR, waiting for the reviewer to merge it. Here, the challenge is that the reviewer also has 10 other PRs to review, so they primarily check for the correctness of the business logic. Having reviewed over 1000 PRs, what I've learned is that no reviewer can ever have 100% context of their codebase. Therefore, we trust that the developer has followed all the checks, fixed anti-patterns, dead and duplicate code, ensured the use of correct data structures, etc. Additionally, awareness of security and compliance issues is not always present with most developers, so we don't review those aspects. Moreover, both the reviewer and developer are under pressure to ship the product faster, with the end goal being deployment in lower environments. If something breaks, we will fix it there.&lt;/p&gt;

&lt;p&gt;The reality is that nothing will break because the existing checks primarily focus on unit or end-to-end tests for business logic, which are already reviewed. Consequently, "bad" code gets checked in and committed.&lt;/p&gt;

&lt;p&gt;This situation results in technical debt. Why haven't we been able to solve it until now? It's due to a lack of enforcement that doesn't slow down developer velocity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What do we need then?&lt;/strong&gt;&lt;br&gt;
We need tools that have a great context of the codebase, prioritize high-severity, high-impact issues, and suggest auto-fixing of these issues without breaking any existing logic. The tool should also seamlessly integrate with the developer journey starting from IDEs to PR checkers.&lt;/p&gt;

&lt;p&gt;We are building &lt;a href="https://codeant.ai/"&gt;CodeAnt AI (YC W24)&lt;/a&gt; on the same lines, and it is live; feel free to check it out &lt;a href="https://github.com/marketplace/codeant-ai"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>codequality</category>
      <category>codereview</category>
      <category>python</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Losing events from AWS lambda?</title>
      <dc:creator>Amartya Jha</dc:creator>
      <pubDate>Fri, 09 Feb 2024 03:16:30 +0000</pubDate>
      <link>https://dev.to/codeantai/losing-events-from-aws-lambda-4g58</link>
      <guid>https://dev.to/codeantai/losing-events-from-aws-lambda-4g58</guid>
      <description>&lt;p&gt;Hello everyone, I have been building venture-backed startups for over 4 years now, and security has always been a challenge. Trust me, identifying impactful bugs and dedicating the team's time to fixing them is another struggle - one that I wanted to fix.&lt;/p&gt;

&lt;p&gt;Currently, I am building &lt;a href="https://codeant.ai/"&gt;CodeAnt AI&lt;/a&gt;, backed by YC. We have made it a priority to develop this as a tool that tackles real pain points for developers.&lt;/p&gt;

&lt;p&gt;Here is an example: We deployed our tool in a company, scanned their codebase, and discovered a missing Deadletter queue in AWS Lambda.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0w7848izu2e3lvlcdea.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc0w7848izu2e3lvlcdea.png" alt="Transaction events failing" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's understand the impact of this bug. When AWS Lambda sends events to an SQS queue and encounters failures, it could be due to network partitions or sending too much data for SQS to handle. In such cases, AWS retries the failed events a certain number of times (around 3). If all retries fail, the event is discarded without storage. This means losing the entire event data, with potential consequences, especially if it's a transaction event.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9mleycmaknx316cyuyxc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9mleycmaknx316cyuyxc.png" alt="Added a Deadletter queue" width="800" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CodeAnt fixed this by suggesting Deadletter queue. AWS Lambda now sends all failed events to this queue. When SQS is back up, the Deadletter queue resends these failed events, ensuring no loss of transaction data in our case.&lt;/p&gt;

&lt;p&gt;Would love it if you guys would try CodeAnt AI, and give me some feedback - &lt;a href="https://github.com/marketplace/codeant-ai"&gt;https://github.com/marketplace/codeant-ai&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We detect and auto-fix bad code, like security vulnerabilities, code antipatterns, complex functions, dead &amp;amp; duplicate code etc.&lt;/p&gt;

</description>
      <category>python</category>
      <category>javascript</category>
      <category>devops</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
