<?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>Forem: Pratik Raghuvanshi</title>
    <description>The latest articles on Forem by Pratik Raghuvanshi (@prxtikk).</description>
    <link>https://forem.com/prxtikk</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%2F593265%2F65353f68-2b6a-47f4-b499-f1f10637adc5.jpg</url>
      <title>Forem: Pratik Raghuvanshi</title>
      <link>https://forem.com/prxtikk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/prxtikk"/>
    <language>en</language>
    <item>
      <title>How to write Clean and Modular Code 🤩👨‍💻</title>
      <dc:creator>Pratik Raghuvanshi</dc:creator>
      <pubDate>Mon, 06 Sep 2021 12:57:46 +0000</pubDate>
      <link>https://forem.com/prxtikk/how-to-write-clean-and-modular-code-1d87</link>
      <guid>https://forem.com/prxtikk/how-to-write-clean-and-modular-code-1d87</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I had not said these lines, so no &lt;strong&gt;offense&lt;/strong&gt;. 😶 &lt;/p&gt;

&lt;h2&gt;
  
  
  Terminologies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clean code&lt;/strong&gt; : Code that is readable, simple, and concise. Clean production-quality code is crucial for collaboration and maintainability in software development.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modular code&lt;/strong&gt; : Code that is logically broken up into functions and modules. Modular production-quality code that makes our code more organized, efficient, and reusable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Module&lt;/strong&gt; : A file. Modules allow code to be reused by encapsulating them into files that can be imported into other files.
&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%2F1i9yfofdjt6glw87e6s5.jpg" alt="Modular Code"&gt; 
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Writing clean code
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use Meaningful Names.
&lt;/h3&gt;

&lt;p&gt;• &lt;strong&gt;Be descriptive and imply type&lt;/strong&gt; : For booleans, we can prefix with &lt;code&gt;is_&lt;/code&gt; or &lt;code&gt;has_&lt;/code&gt; to make it clear it is a condition. We can also use parts of speech to imply types, like using verbs for functions and nouns for variables.&lt;br&gt;
• &lt;strong&gt;Be consistent but clearly differentiate&lt;/strong&gt; : &lt;code&gt;age_list&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; is easier to differentiate than &lt;code&gt;ages&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;.&lt;br&gt;
• &lt;strong&gt;Avoid abbreviations and single letters&lt;/strong&gt; : We can determine when to make these exceptions based on the audience of our code and if it is counters and common math variables.&lt;br&gt;
• &lt;strong&gt;Long names aren't the same as descriptive names&lt;/strong&gt; : We should be descriptive, but only with relevant information. For example, good function names describe what they do well without including details about implementation or highly specific uses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Whitespace Properly.
&lt;/h3&gt;

&lt;p&gt;• &lt;strong&gt;Organize your code with consistent indentation&lt;/strong&gt; : the standard is to use four spaces for each indent. We can make this a default in our text editor.&lt;br&gt;
• Separate sections with blank lines to keep our code well organized and readable.&lt;br&gt;
• If you are coding in &lt;strong&gt;python&lt;/strong&gt;, try to limit your lines to around 79 characters, which is the guideline given in the PEP 8 style guide. In many good text editors, there is a setting to display a subtle line that indicates where the 79 character limit is.&lt;/p&gt;

&lt;h3&gt;
  
  
  For Example :
&lt;/h3&gt;

&lt;p&gt;Imagine we are writing a program that executes a number of tasks and categorizes each task based on its execution time. Below is a small snippet of this program.&lt;/p&gt;

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

t = end_time - start  # compute execution time
c = category(t)  # get category of task
print('Task Duration: {} seconds, Category: {}'.format(t, c)


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;How we can make this code look cleaner?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following naming changes could make this code cleaner.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rename the variable &lt;code&gt;start&lt;/code&gt; to &lt;code&gt;start_time&lt;/code&gt; to make it consistent with &lt;code&gt;end_time&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Rename the variable &lt;code&gt;t&lt;/code&gt; to &lt;code&gt;execution_time&lt;/code&gt; to make it more descriptive.&lt;/li&gt;
&lt;li&gt;Rename the function &lt;code&gt;category&lt;/code&gt; to &lt;code&gt;categorize_task&lt;/code&gt; to match the part of speech.&lt;/li&gt;
&lt;li&gt;Rename the variable &lt;code&gt;c&lt;/code&gt; to &lt;code&gt;category&lt;/code&gt; to make it more descriptive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the changes the code will look like this :&lt;/p&gt;

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

execution_time = end_time - start_time
category = categorize_task(execution_time)
print('Task Duration: {} seconds, Category: {}'.format(execution_time, category)


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

&lt;/div&gt;



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

&lt;p&gt;Follow the tips below to write modular code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tip : DRY (Don't Repeat Yourself)&lt;/strong&gt;&lt;br&gt;
Don't repeat yourself! Modularization allows us to reuse parts of our code. Generalize and consolidate repeated code in functions or loops. &lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tip : Abstract out logic to improve readability&lt;/strong&gt;&lt;br&gt;
Abstracting out code into a function not only makes it less repetitive, but also improves readability with descriptive function names. Although our code can become more readable when we abstract out logic into functions, it is possible to over-engineer this and have way too many modules, so we can use our own judgement. &lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tip : Minimize the number of entities (functions, classes, modules, etc.)&lt;/strong&gt;&lt;br&gt;
There are trade-offs to having function calls instead of inline logic. If we have broken up your code into an unnecessary amount of functions and modules, we will have to jump around everywhere if we want to view the implementation details for something that may be too small to be worth it. Creating more modules doesn't necessarily result in effective modularization. &lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tip : Functions should do one thing&lt;/strong&gt;&lt;br&gt;
Each function we write should be focused on doing one thing. If a function is doing multiple things, it becomes more difficult to generalize and reuse. Generally, if there's an "and" in your function name, consider refactoring. &lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tip : Arbitrary variable names can be more effective in certain functions&lt;/strong&gt;&lt;br&gt;
Arbitrary variable names in general functions can actually make the code more readable. &lt;br&gt;&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Tip : Try to use fewer than three arguments per function&lt;/strong&gt;&lt;br&gt;
Try to use no more than three arguments when possible. This is not a hard rule and there are times when it is more appropriate to use many parameters. But in many cases, it's more effective to use fewer arguments. Remember we are modularizing to simplify our code and make it more efficient. If our function has a lot of parameters, we may want to rethink how we are splitting this up.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  For Example :
&lt;/h3&gt;

&lt;p&gt;We have a list of &lt;strong&gt;Test Scores&lt;/strong&gt; that we curve in three different ways. Assume that we are an educator who gave out a test that was too difficult or gave a question that was a little unfair (like our college examiners do sometimes). So we decide to figure out a way to boost out student’s scores. &lt;/p&gt;

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

s = [88, 92, 79, 93, 85]
print(sum(s)/len(s))

s1 = []
for x in s:
    s1.append(x + 5)

print(sum(s1)/len(s1))

s2 = []
for x in s:
    s2.append(x + 10)

print(sum(s2)/len(s2))

s3 = []
for x in s:
    s3.append(x ** 0.5 * 10)

print(sum(s3)/len(s3))

# difficult to understand and pretty repetitive


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

&lt;/div&gt;

&lt;p&gt;For the first two methods, we add a flat curve of 5 points to each scores and 10 points to each score respectively. In third method we applied a square root curve, where we find the square root of each score and multiply it by 10.&lt;/p&gt;

&lt;p&gt;Right now, its &lt;strong&gt;difficult to understand&lt;/strong&gt; what this code is for and looks pretty &lt;strong&gt;repetitive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now by following the tips we can modify the code as&lt;/p&gt;

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

import math
import numpy as np

def flat_curve(arr, n):
    return [i + n for i in arr]

def square_root_curve(arr):
    return [math.sqrt(i) * 10 for i in arr]

test_scores = [88, 92, 79, 93, 85]
curved_5 = flat_curve(test_scores, 5)
curved_10 = flat_curve(test_scores, 10)
curved_sqrt = square_root_curve(test_scores)

for score_list in test_scores, curved_5, curved_10, curved_sqrt:
    print(np.mean(score_list))

# clean and modular


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

&lt;/div&gt;

&lt;p&gt;This is an example of &lt;strong&gt;clean&lt;/strong&gt; and &lt;strong&gt;modular code&lt;/strong&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%2Felds6rv3g3q3ff4j40kj.gif" 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%2Felds6rv3g3q3ff4j40kj.gif" alt="gif"&gt;&lt;/a&gt; &lt;/p&gt;

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

&lt;p&gt;Follow these tips to write clean and modular code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Meaningful Names&lt;/li&gt;
&lt;li&gt;Use Whitespace Properly&lt;/li&gt;
&lt;li&gt;DRY (Don't Repeat Yourself)&lt;/li&gt;
&lt;li&gt;Abstract out logic to improve readability&lt;/li&gt;
&lt;li&gt;Minimize the number of entities (functions, classes, modules, etc.)&lt;/li&gt;
&lt;li&gt;Functions should do one thing&lt;/li&gt;
&lt;li&gt;Arbitrary variable names can be more effective in certain functions&lt;/li&gt;
&lt;li&gt;Try to use fewer than three arguments per function&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Thanks for reading!
&lt;/h2&gt;

&lt;p&gt;Feel free to give any suggestions. :-)&lt;/p&gt;

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

&lt;p&gt;AWS Machine Learning Foundation Course on Udacity&lt;/p&gt;

&lt;h2&gt;
  
  
  You can connect with me on
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/Prxtikk" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.linkedin.com/in/prxtikk/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>codequality</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
