<?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: Aman Giri</title>
    <description>The latest articles on DEV Community by Aman Giri (@aman-giri).</description>
    <link>https://dev.to/aman-giri</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%2F1250765%2F32612241-a78b-4aa0-8757-fe36c07e4798.jpeg</url>
      <title>DEV Community: Aman Giri</title>
      <link>https://dev.to/aman-giri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aman-giri"/>
    <language>en</language>
    <item>
      <title>Understanding Python Decorators: A Deep Dive</title>
      <dc:creator>Aman Giri</dc:creator>
      <pubDate>Sat, 26 Oct 2024 03:30:03 +0000</pubDate>
      <link>https://dev.to/aman-giri/understanding-python-decorators-a-deep-dive-pp0</link>
      <guid>https://dev.to/aman-giri/understanding-python-decorators-a-deep-dive-pp0</guid>
      <description>&lt;p&gt;Python decorators are powerful tools that allow us to modify or enhance the behavior of functions or methods. Common use cases include logging, authorization, and more. &lt;br&gt;
However, when asked to define a decorator, many might say, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's a wrapper for a function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While this is technically correct, there's much more happening under the hood.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dissecting a Simple Decorator&lt;/strong&gt;&lt;br&gt;
Let's explore a straightforward example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def my_decorator(func):
    def wrapper(*args, **kwargs):
        print("Before calling the function")
        result = func(*args, **kwargs)
        print("After calling the function")
        return result
    return wrapper

@my_decorator
def say_hello(name):
    print(f"Hello, {name}!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, my_decorator is a decorator for the function say_hello. When say_hello is defined, it is automatically passed to my_decorator, transforming the function call into:&lt;br&gt;
&lt;code&gt;say_hello = my_decorator(say_hello)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When Does This Transformation Happen?&lt;/strong&gt;&lt;br&gt;
This transformation occurs during the compilation of the code, specifically at function definition time—not at execution time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disassembling the Code&lt;/strong&gt;&lt;br&gt;
To understand how decorators work at a lower level, we can use the dis module to examine the bytecode of the decorated function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import dis

@my_decorator
def say_hello(name):
    print(f"Hello, {name}!")

dis.dis(say_hello)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bytecode Breakdown&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The output of dis.dis(say_hello) might look 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%2Fwiae0xypnh5pbw2ly7gs.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%2Fwiae0xypnh5pbw2ly7gs.png" alt="Bytecode Breakdown" width="800" height="305"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Explanation of the Bytecode&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Before Calling the Function&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LOAD_GLOBAL: Loads the print function.&lt;/li&gt;
&lt;li&gt;LOAD_CONST: Loads the message 'Before calling the function'.&lt;/li&gt;
&lt;li&gt;CALL_FUNCTION: Calls print.&lt;/li&gt;
&lt;li&gt;POP_TOP: Discards the return value.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Calling the Original Function&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LOAD_DEREF: Loads the original function (func) captured by the closure.&lt;/li&gt;
&lt;li&gt;LOAD_FAST: Loads the positional and keyword arguments.&lt;/li&gt;
&lt;li&gt;BUILD_MAP: Creates a new dictionary for keyword arguments.&lt;/li&gt;
&lt;li&gt;CALL_FUNCTION_EX: Calls the original function with the arguments.&lt;/li&gt;
&lt;li&gt;STORE_FAST: Stores the result in a local variable.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;After Calling the Function&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar to the first part, it calls print to output 'After calling the function'.&lt;/li&gt;
&lt;li&gt;Returning the Result&lt;/li&gt;
&lt;li&gt;Loads the result variable and returns it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Python decorators are more than just function wrappers; they enable us to modify function behavior at definition time. By understanding how they work and examining the bytecode, we can use decorators more effectively in our projects.&lt;/p&gt;

&lt;p&gt;That's it for now! If there’s anything else you’d like me to dive into, just let me know!&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>From Developer to Reviewer: A Junior Developer's Checklist for Reviewing Database Queries</title>
      <dc:creator>Aman Giri</dc:creator>
      <pubDate>Sun, 08 Sep 2024 12:19:45 +0000</pubDate>
      <link>https://dev.to/aman-giri/from-developer-to-reviewer-a-junior-developers-checklist-for-reviewing-database-queries-4kdm</link>
      <guid>https://dev.to/aman-giri/from-developer-to-reviewer-a-junior-developers-checklist-for-reviewing-database-queries-4kdm</guid>
      <description>&lt;p&gt;As a developer, it's crucial to deliver quality code that is not only functional but also optimized for performance. Over my three years in the developer domain, I transitioned from a hands-on developer to a reviewer role. One of the key areas I've focused on during reviews is database query optimization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Focus on Database Queries?&lt;/strong&gt;&lt;br&gt;
Database queries can significantly impact the performance of an application. A well-written query can fetch data efficiently, while a poorly written one can lead to excessive database hits, slowing down the entire system. As a junior developer, you might wonder how to assess the performance of these queries during code reviews. Here's my go-to checklist.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checklist for Reviewing Database Queries&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Number of Database Hits: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first step is to determine how many database hits are being made by a piece of code. Each hit to the database can add latency, so fewer hits generally mean better performance.&lt;/li&gt;
&lt;li&gt;Pro Tip: Use Django’s connection.queries and reset_queries to track the number of queries executed and the time taken for each. For example:
&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%2Fjj2i9j94swn9pb1qw2gb.png" alt="Code to determine number of hits" width="800" height="467"&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Reducing the Number of Hits: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Once you know the number of hits, the next step is to see if you can reduce them. Reducing database hits can often be achieved by optimizing the queries or combining multiple queries into one.&lt;/li&gt;
&lt;li&gt;Key Techniques:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lazy vs. Eager Loading&lt;/strong&gt;: Understand when a query is being evaluated. Lazy loading delays the query until the data is actually needed, but this can lead to the N+1 query problem. Eager loading, using select_related or prefetch_related, fetches related objects in a single query, reducing the total number of hits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Joins&lt;/strong&gt;: If you need data from related tables, consider using join queries. Django’s select_related (for foreign key relationships) and prefetch_related (for many-to-many or reverse foreign key relationships) are your friends here.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Avoiding Redundant Queries: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check for redundant queries, where the same query is executed multiple times. This can often be avoided by caching the result or restructuring the code.&lt;/li&gt;
&lt;li&gt;Example: Instead of querying for a related object inside a loop, fetch all related objects once before the loop.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Memory Considerations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;While reducing database hits is important, also consider memory usage. Loading a massive dataset into memory can cause your application to slow down or crash. Aim to only pull in the records/data you need.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Reviewing database queries for performance is a skill that develops with time and experience. As a junior developer, start by focusing on the basics—counting database hits and finding ways to reduce them. Tools like connection.queries, reset_queries, and Django's ORM features are invaluable in this process. Over time, you'll develop an intuition for spotting performance issues just by looking at the code. Until then, rely on the tools and techniques discussed here to guide you.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Understand the Execution Plan:
Dive deeper by understanding the SQL execution plan generated by your ORM queries. Tools like EXPLAIN in SQL can help you understand how your database engine is executing queries, which can provide insights into potential optimizations.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;A Tip for Fresher Developers:&lt;/strong&gt;&lt;br&gt;
To start reviewing other people's code, you don't need to know every flow of the system. Begin by reviewing small things like code quality and optimization. Start by doing the first pass, focusing on the basics, and eventually, you will become a great reviewer.&lt;br&gt;
Also, while reviewing, try to be polite and helpful in your comments rather than being arrogant. Remember, the goal is to improve the code and help your team, not just to point out mistakes.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>database</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
