<?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: Syed Mubtada Ali</title>
    <description>The latest articles on DEV Community by Syed Mubtada Ali (@mubtadaali).</description>
    <link>https://dev.to/mubtadaali</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%2F536866%2F23894a04-9e85-486c-8e70-bcc834dc6b25.jpeg</url>
      <title>DEV Community: Syed Mubtada Ali</title>
      <link>https://dev.to/mubtadaali</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mubtadaali"/>
    <language>en</language>
    <item>
      <title>Tips and tricks for optimizing the performance of Django ORM</title>
      <dc:creator>Syed Mubtada Ali</dc:creator>
      <pubDate>Wed, 03 May 2023 13:59:29 +0000</pubDate>
      <link>https://dev.to/mubtadaali/tips-and-tricks-for-optimizing-the-performance-of-django-orm-8pg</link>
      <guid>https://dev.to/mubtadaali/tips-and-tricks-for-optimizing-the-performance-of-django-orm-8pg</guid>
      <description>&lt;p&gt;Django is a popular web framework for building complex, data-driven applications, and its Object-Relational Mapping (ORM) system is a crucial component for interacting with databases. However, as your application grows in complexity and scale, it’s essential to ensure that your ORM queries are optimized for performance. In this article, we’ll explore some tips and tricks for optimizing the performance of Django ORM so that your application can handle larger volumes of data and operate more efficiently. These tips will help you get the most out of your database queries and keep your application running smoothly.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://docs.djangoproject.com/en/4.2/ref/models/querysets/#only"&gt;&lt;strong&gt;Only&lt;/strong&gt;&lt;/a&gt;()
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uiJzXghP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2044/1%2AoEd2jTiinH9Bt8XjfKqyFw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uiJzXghP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2044/1%2AoEd2jTiinH9Bt8XjfKqyFw.png" alt="without only()" width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, we are only using two fields. Still, when Django ORM translates Python code into SQL, it retrieves all fields from the model as a result, even if some data is not necessary. To optimize the query and prevent unnecessary data retrieval, developers can utilize the .only() method and explicitly indicate the specific fields that are required.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zftYPbHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2984/1%2AsrEeM7pqWHQAIdh7nMEm_Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zftYPbHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2984/1%2AsrEeM7pqWHQAIdh7nMEm_Q.png" alt="with only()" width="800" height="205"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://docs.djangoproject.com/en/4.2/ref/models/querysets/#prefetch-related"&gt;&lt;strong&gt;Prefetch_related&lt;/strong&gt;&lt;/a&gt; / &lt;a href="https://docs.djangoproject.com/en/4.2/ref/models/querysets/#select-related"&gt;&lt;strong&gt;select_related&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;When Django evaluates a QuerySet, the relationship fields are not included in the query, so when you access them, Django runs the DB again to get the values. Accessing a related field in a loop results in N+1 queries. You can check the queries executed on the database using tools like &lt;a href="https://pypi.org/project/django-debug-toolbar/"&gt;django-debug-toolbar&lt;/a&gt; and &lt;a href="https://pypi.org/project/django-silk/"&gt;django-silk&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HFNDaN_Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2836/1%2AJgF57II-arxoDlIX4ODKzw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HFNDaN_Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2836/1%2AJgF57II-arxoDlIX4ODKzw.png" alt="without select related" width="800" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code, the first query is executed on the option queryset, and then for every option, three queries are executed: question, division, and concern. To reduce queries, we can add question and division in select_related and concern in prefetch_related.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jbav6GHT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2836/1%2A0kDgFKltS9t6zxFUkI0VUA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jbav6GHT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2836/1%2A0kDgFKltS9t6zxFUkI0VUA.png" alt="with select related" width="800" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code will execute only 2 queries on the database regardless of the number of options: one for options and the second for concerns. In the previous version, if there were 100 options, it would perform 301 (3N + 1) queries on the database.&lt;/p&gt;

&lt;p&gt;One important thing to understand is that after adding select_related and prefetch_related, you cannot use ORM functions such as first(), *&lt;em&gt;last(), or *order_by()&lt;/em&gt;, as these will run the queries again. If you want the first object of concern, use option.concerns.all()[0] instead of option.concerns.all(). Use &lt;em&gt;Prefetch()&lt;/em&gt; only when you want additional filtering and order_by; otherwise, simple prefetch_related(“concern”) is enough.&lt;/p&gt;

&lt;p&gt;The basic difference between select_related and prefetch_related is that select_related performs a join with each lookup and gets the results back in the same query, but it extends the “select” to include the column of all joined tables. On the other hand, prefetch_related performs a separate query for each table to be joined. It filters each of these tables with a WHERE IN clause. The select_related can be used for one-to-one and one-to-many relations if the related field exists in the table where the query is being executed. However, if the related field exists in the other table, we can use prefetch_related. It’s also used for many-to-many relations.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Queryset Evaluation and Caching&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Take a look at the following simple example to understand ORM evaluation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MzmsRDQc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2296/1%2Ao8W456K9_h6ceSyxc65ZkA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MzmsRDQc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2296/1%2Ao8W456K9_h6ceSyxc65ZkA.png" alt="Evaluation" width="800" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It might look like it performed two database queries, but it actually performed only a single query, which was executed on the last line, “&lt;em&gt;len(active_staff)&lt;/em&gt;”.&lt;/p&gt;

&lt;p&gt;Creating a queryset doesn’t perform any database activity, not even stacking the filters. A queryset result is only fetched from the database when asked by performing certain actions, like iteration, len, slicing, repr, list, etc. This is called evaluation.&lt;/p&gt;

&lt;p&gt;Caching allows you to avoid making multiple database queries when reusing the same queryset. The first time you run a QuerySet, Django saves the results in a temporary storage place called cache. Then, whenever you run that same QuerySet again, Django will use the cached results instead of running another query to the database. This makes the process of retrieving data faster and more efficient.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o-sJkcKV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2440/1%2Amua-uaQepv6KYPNmp5VmJQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o-sJkcKV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2440/1%2Amua-uaQepv6KYPNmp5VmJQ.png" alt="without caching" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example, the same query is executed twice on the database. To avoid this problem, save the QuerySet and reuse it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tZHBS0NA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2764/1%2AE0qrckAc-mboukrhwAVVqg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tZHBS0NA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2764/1%2AE0qrckAc-mboukrhwAVVqg.png" alt="with cache" width="800" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://docs.djangoproject.com/en/4.2/ref/models/querysets/#in-bulk"&gt;&lt;strong&gt;In_bulk&lt;/strong&gt;&lt;/a&gt;()
&lt;/h3&gt;

&lt;p&gt;This function will make your life easier if you need a dict map of the queryset. It allows you to retrieve a dictionary of objects from the database based on a list of primary key values or the defined key in the function argument.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3DQiMsZF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2368/1%2AbVjuwBi3c4NR2qrRYjPbIQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3DQiMsZF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2368/1%2AbVjuwBi3c4NR2qrRYjPbIQ.png" alt="without in-bulk" width="800" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SgDIb9zL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2224/1%2AemQjs_1SfdBPIRRkncP0LQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SgDIb9zL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2224/1%2AemQjs_1SfdBPIRRkncP0LQ.png" alt="with in-bulk" width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code is much cleaner and also provides a performance boost by leveraging the lazy nature of Django ORM.&lt;/p&gt;




&lt;p&gt;For those who are passionate about writing neat and organized code and haven’t had the chance to go through “Clean Code” by Robert C. Martin, this article provides a helpful summary of the book’s first half and key takeaways. It’s worth reading if you’re looking to enhance your coding skills and principles.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/mubtadaali" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rI5GG6-c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--LjrguVns--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/536866/23894a04-9e85-486c-8e70-bcc834dc6b25.jpeg" alt="mubtadaali"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/mubtadaali/lessons-from-clean-code-13e9" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Lessons from “Clean Code”&lt;/h2&gt;
      &lt;h3&gt;Syed Mubtada Ali ・ May 3 ・ 9 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#python&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#cleancode&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#productivity&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;





&lt;p&gt;I hope that you have learned something fresh and insightful. If our interests align, please consider subscribing. You can also reach out to me on &lt;a href="https://www.linkedin.com/in/mubtada-ali-naqvi-3a8660a9/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for your time and attention.&lt;/p&gt;

</description>
      <category>django</category>
      <category>productivity</category>
      <category>programming</category>
      <category>python</category>
    </item>
    <item>
      <title>Lessons from “Clean Code”</title>
      <dc:creator>Syed Mubtada Ali</dc:creator>
      <pubDate>Wed, 03 May 2023 13:46:54 +0000</pubDate>
      <link>https://dev.to/mubtadaali/lessons-from-clean-code-13e9</link>
      <guid>https://dev.to/mubtadaali/lessons-from-clean-code-13e9</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read. — &lt;strong&gt;Robert C. Martin&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Usually, there are two types of programmers clever ones and professionals. Both of them can get the job done but there are a lot of differences between the two. Professionals understand that clarity is the king. They use their powers to write clean code that others can understand and work on it by writing clean code. But what exactly is clean code? What are its characteristics and how can one write clean code? These were the questions in my mind when I started reading &lt;a href="https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882"&gt;Clean Code&lt;/a&gt; written by &lt;a href="https://en.wikipedia.org/wiki/Robert_C._Martin"&gt;Robert C. Martin&lt;/a&gt; and gladly this book didn’t disappoint me at all. This book has so many takeaways for clean code and I think every programmer should read it especially at the start of their career.&lt;/p&gt;

&lt;p&gt;In this article, we will learn&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;variable names should reveal its intent and should not include data type&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;class and function name should be a noun and verb respectively&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;functions should do only one thing and number of arguments should be minimum&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;comments should be avoided as much as possible&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;parts of code linked together should be placed next to each other&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the code should be formatted in a way that the eyeballing should be minimum while reading it&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the code in this article is written in Python so if you are unfamiliar with the language it might be a bit abstract for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Characteristics of Clean Code?
&lt;/h2&gt;

&lt;p&gt;Robert C. Martin aka Uncle Bob asked some very experienced and well-known programmers that what they thought about clean code. As per them here are a few of the characteristics of clean code.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I like my code to be elegant and efficient. Logic should be straightforward to make it hard for bugs to hide. Clean code does one thing well.&lt;/em&gt; — &lt;strong&gt;Bjarne Stroustrup&lt;/strong&gt;(inventor of C++)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Clean code can be read, and enhanced by a developer other than its original author. It has unit and acceptance tests. It has meaningful names&lt;/em&gt; — &lt;strong&gt;Dave Thomas&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Clean code is simple and direct. Clean code reads like well-written prose — *&lt;/em&gt;&lt;em&gt;Grady Booch&lt;/em&gt;* (author of Object-Oriented Analysis and Design with Applications)&lt;/p&gt;

&lt;p&gt;So, If I outline these characteristics, the clean code should be&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Elegant&lt;/strong&gt;: It should be simple and easy to read. It should make you smile while reading it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Focused&lt;/strong&gt;: Each function/module should expose a &lt;em&gt;single-minded&lt;/em&gt; attitude. It should only focus on getting one thing done.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tests&lt;/strong&gt;: If it doesn’t have tests then it’s not clean no matter how elegant/readable it is.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Writing Clean Code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Meaningful Name
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;If a name requires a comment, then the name does not reveal its intent. — &lt;strong&gt;Uncle Bob&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Names are important as they reveal the purpose. Coming up with good and meaningful names takes time but believe me it’s worth it because it saves time we have to read/overview it later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variable Name
&lt;/h3&gt;

&lt;p&gt;It should describe its nature without any comment and if it’s not, then the name should be changed. i.e.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
The data type should not be part of a variable name even if it’s allowed in the language’s coding convention. Especially in Python at the time of variable definition, we don’t set data type. It will cause problems if we change the data type of a variable at some point as we have to replace it with the new data type. Let’s take a look at the example below&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Changing the above variable into float would be very hectic as we will have to replace it wherever we have used it. However, it’s allowed in &lt;a href="https://en.wikipedia.org/wiki/Hungarian_notation"&gt;*Hungarian notation&lt;/a&gt;* but now it’s not recommended to use it and Microsoft also doesn’t recommend this notation. (&lt;a href="https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/general-naming-conventions"&gt;reference&lt;/a&gt;)&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
The variable names should be pronounceable. As Uncle Bob said, &lt;em&gt;If you can’t pronounce it, you can’t discuss it without sounding like an idiot. “Well, over here on the ‘bee cee arr three cee enn tee’ (bcr3cnt) we have a ‘pee ess zee kyew’ (pszq) int, see?”&lt;/em&gt;

&lt;p&gt;A name should also be searchable, for that, longer names are preferred over shorter ones. Also, a single character name should not be used for a large scope. It should only be used in a shorter scope like a small function/loop.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h3&gt;
  
  
  Function &amp;amp; Class Name
&lt;/h3&gt;

&lt;p&gt;A function name should be a verb and it should also reveal the action along with the value, like &lt;em&gt;extract_age, delete_user_info. *On the other hand, a class name should not be a verb. It should be a noun or a noun phrase name. i.e. *Customer, Account and WeatherRecord.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Don’t make it hard for yourself by adding unnecessary context. Don’t prefix every class/function with context unless they represent a different context. For example in an application of “Green Pizza Hut”, it will be really bad to prefix every function/class with “gph” as there will be a lot of redundant characters. Shorter names are better as far as they are clear.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cuteness
&lt;/h3&gt;

&lt;p&gt;If you are cute, great that’s good for you but please don’t make the code cute make it rather professional. I know it sounds very cute to name a variable after your pet or favorite character’s name but how on earth one can know what is “&lt;em&gt;Bubbles&lt;/em&gt;” or “&lt;em&gt;Dexter&lt;/em&gt;”.&lt;/p&gt;

&lt;p&gt;Don’t use slang words while naming your variables, functions or classes. For example, don’t use &lt;em&gt;merk()&lt;/em&gt; to mean &lt;em&gt;kill().&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  One Word per Concept
&lt;/h3&gt;

&lt;p&gt;Pick one word for a concept and stick with it. For example, we should not use *get, retrieve, extract *and *fetch as a function name *for the same concept of different classes. It will be very difficult to remember which class has which function name. In the same way, it’s also confusing to have *Manager, Controller *in the codebase as both serve the same purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Functions should do one thing. They should do it well. They should do it only.— &lt;strong&gt;Uncle Bob&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Size does matter in functions, the first rule of a function is that it should be small and then it should be smaller than that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do One Thing
&lt;/h3&gt;

&lt;p&gt;There is no advantage of writing a function if it’s doing more than one thing. As the main purpose of writing functions is to decompose a larger concept into a set of steps at the next level of abstraction. Functions should not be large enough to hold nested structures. Also, the indent level of a function should not be greater than one or two. It will make the function easier to read and understand.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
The function above, even though it’s not very long, is doing more than one thing. The block before the loop should be a separate function.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Uncle Bob mentioned a way to know that a function is doing more than “one thing” is if you can extract another function from it with a name that is not simply a rephrasing of its implementation.
&lt;h3&gt;
  
  
  Arguments
&lt;/h3&gt;

&lt;p&gt;There should be no more than three arguments for a function. An ideal number of arguments should be zero. More arguments take more conceptual power. A lesser number of arguments make easier to write test cases.&lt;/p&gt;

&lt;p&gt;Flag arguments are ugly. Passing a boolean into a function is a truly terrible practice. It immediately complicates the signature of the function, loudly proclaiming that this function does more than one thing. It does one thing if the flag is true and another if the flag is false.&lt;/p&gt;
&lt;h2&gt;
  
  
  Comments
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Nothing can be quite so helpful as a well-placed comment. Nothing can be quite so damaging as an old crufty comment that propagates lies and misinformation. — &lt;strong&gt;Uncle Bob&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Comments are always failures. We have them because we cannot always figure out how to express ourselves without them, but their use is not a cause for celebration. The purpose of a comment should not be to explain the intent. We write a module and we know it is confusing and disorganized. We know it’s a mess. So we say to ourselves, &lt;em&gt;“Ooh, I’d better comment that!”&lt;/em&gt; No! You should better clean it!&lt;/p&gt;

&lt;p&gt;There are two ways in the following code; a messy code with a comment to explain it and other is clean code explaining itself without any comment.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
So, don’t use comments when you can use a variable/function.

&lt;p&gt;Unfortunately we, the programmers, don’t realistically maintain the comments. Requirement changes and so does the code, a chunk of it moves from here and there but the comment stays there and creates the mess and misunderstanding. There should be no commented code or position markers. It’s very rare when position markers make sense to gather certain function together but in general, they should be removed.&lt;/p&gt;

&lt;p&gt;An example of position maker is given below.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*# Actions #######*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Good Comments
&lt;/h3&gt;

&lt;p&gt;Not all comments are bad, sometimes it’s necessary to add comments. For example for legal reasons, it’s good to add comments for copyrights, authorship at the start of the file. It’s also reasonable to add a warning of consequences and “to do” comments.&lt;/p&gt;

&lt;p&gt;Sometimes a comment goes beyond just useful information about the implementation and provides the reason for writing this code. For example, in the following example the developer expressing the reason for adding download delay.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Formatting
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Code formatting is important. It’s too important to ignore and it’s important to treat religiously.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The source file should be like a newspaper article. The filename should be simple but meaningful enough to tell us whether we are in the right file or not just like a newspaper article’s title. As the start of an article gives an abstract of the article and details increases as we go in. In the same way, high-level concepts should be at the top of a file and details should increase as we go down. Low-level function/details should be at the end of the file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Vertical Formatting
&lt;/h3&gt;

&lt;p&gt;Similar logical lines should be separated from other by a single blank line. The blank line is a visual cue that identifies a new and separate concept.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Just like adding a blank line separates concepts, vertical density implies close association. Lines of code that are tightly related should appear next to each other. Because it’s very frustrating if you are trying to locate a chain of inheritance for the definition of a function/variable, spending your time and energy just to find and remember where the pieces are.

&lt;p&gt;If one function calls another, they should be vertically close, and the caller should be above the callee, if at all possible. The stronger that affinity, the less vertical distance there should be between them. This gives the program a natural flow. Also, variables should be declared as close to their usage as possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Horizontal Formatting
&lt;/h3&gt;

&lt;p&gt;We use horizontal white space to associate things that are strongly related and disassociate things that are weakly related. We surround the assignment operators with white space to accentuate them. The spaces make that separation obvious. But we don’t put spaces between the function names and the opening parenthesis. This is because the function and its arguments are closely related.&lt;/p&gt;

&lt;p&gt;In python, we also wrap comparisons, mathematical and boolean operators with a single space but if there are multiple operators either in a single line then we wrap the operator with the lower priority. An example is given below.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
The following lines are not inspired by the “clean code” instead this is something that I have learned in my professional life. At the start of my career, I was often told by my team lead and seniors to split a line into multiple and vice versa while code reviewing. At that time I was confused by this splitting and merging but then my team lead explains this “*eyeballing” *effect and then it made sense to me.

&lt;p&gt;The size of a line should be near to the length of its neighboring lines. So that the eyeballing should be minimized as much as possible while reading the code. It will make reading easier a lot.&lt;/p&gt;

&lt;p&gt;For example in the code below, the size length is not near to its neighboring lines&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
The eyeballing here should be minimized, the refactored code is given below.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
So, to conclude the whole thing, here are your takeaways from this:

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Variable name should be meaningful, pronounceable, searchable and should reveal its intent. The data type should not be a part of it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Class names should be a noun and have a clear description of the things inside it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function names should be a verb and do only one thing and should not be too long.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Functions should have the minimum number of arguments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Comments can be written as an absolute last way of explaining what you are trying to accomplish and should be well thought.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parts of codes linked together should be formatted next to each other.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The code should have as less eye-balling as possible.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you have some questions or your perspective of this please do reach out to me through &lt;a href="https://www.linkedin.com/in/mubtada-ali-naqvi-3a8660a9/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy Clean Coding&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>cleancode</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
