<?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: saurabh.v</title>
    <description>The latest articles on DEV Community by saurabh.v (@saurabhgiv).</description>
    <link>https://dev.to/saurabhgiv</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%2F8570%2F20c55fcf-eae8-4af5-af4e-d8782162c239.jpg</url>
      <title>DEV Community: saurabh.v</title>
      <link>https://dev.to/saurabhgiv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saurabhgiv"/>
    <language>en</language>
    <item>
      <title>Eventual vs Strong Consistency in Distributed Databases</title>
      <dc:creator>saurabh.v</dc:creator>
      <pubDate>Mon, 31 Jul 2017 16:28:02 +0000</pubDate>
      <link>https://dev.to/saurabhgiv/eventual-vs-strong-consistency-in-distributed-databases</link>
      <guid>https://dev.to/saurabhgiv/eventual-vs-strong-consistency-in-distributed-databases</guid>
      <description>&lt;p&gt;Explanation of this topic starts with an analogy, taking an example from real life to understand the concept better.&lt;/p&gt;

&lt;p&gt;I have the habit of writing something I call &lt;em&gt;Tech Notes&lt;/em&gt; on my laptop daily to summarize technical concepts that I learn. It helps me to recollect them in an easier way whenever I want to.&lt;/p&gt;

&lt;p&gt;But sometimes I used to worry about my laptop being stolen or what if it crashes. I will lose all these &lt;em&gt;Tech Notes&lt;/em&gt; so I started backing them up on my external Hard Disk. To further reduce the possibility of losing them, I also purchased a subscription of Dropbox.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2APiQhvAicV4TUXohXNdNMuA.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2APiQhvAicV4TUXohXNdNMuA.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every fortnight, I update my external Hard Disk with revised and newly written Tech Notes and Dropbox gets updated as soon as I connect my laptop to the internet.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here, I am using Hard Disk and Dropbox as a source of reading Tech Notes while the laptop is being used for reading as well writing them. (Master-Slave Model)&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Redundancy introduces Reliability.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now let’s get to the point.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 1: Eventual Consistency&lt;/strong&gt;&lt;br&gt;
Whenever we use multiple replicas of a database to store data and let’s say a write request comes to one of the replicas. In such a situation, Databases had to discover a strategy to make this write request at one replica reach other replicas as well so that they all could write data of the request and become consistent.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Consistency here means that a read request for an entity made to any of the nodes of the database should return the same data.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Eventual consistency makes sure that data of each node of the database gets consistent eventually. Time taken by the nodes of the database to get consistent may or may not be defined.&lt;/p&gt;

&lt;p&gt;Data getting consistent eventually means it will take time for updates to reach other replicas. So what?&lt;br&gt;
This implies that if someone reads from a replica which is not updated yet (since replicas are updated eventually) then it may return stale data.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2APFgHx8UYLhk3L5ePPmailQ.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2APFgHx8UYLhk3L5ePPmailQ.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My Hard Disk also keeps stale data for a period of 15 days as it gets updated fortnightly. Let’s assume John, my friend comes after few days of updation and asks for my Hard Disk.&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="n"&gt;John&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;want&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="n"&gt;hard&lt;/span&gt; &lt;span class="n"&gt;disk&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="n"&gt;Tech&lt;/span&gt; &lt;span class="n"&gt;Notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;why&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;But&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;been&lt;/span&gt; &lt;span class="n"&gt;updated&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="n"&gt;few&lt;/span&gt; &lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="n"&gt;John&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;fine&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Hard Disk was supplied to John immediately &lt;em&gt;(low latency)&lt;/em&gt; at the risk of having stale data in it. But I am sure about the fact that it will get updated when the next fortnight starts.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Eventual consistency offers low latency at the risk of returning stale data&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While on the other hand, we have something known as Strong Consistency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 2: Strong Consistency&lt;/strong&gt;&lt;br&gt;
It says data will get passed on to all the replicas as soon as a write request comes to one of the replicas of the database.&lt;br&gt;
But during the time these replicas are being updated with new data, response to any subsequent read/write requests by any of the replicas will get delayed as all replicas are busy in keeping each other consistent.&lt;/p&gt;

&lt;p&gt;As soon as they become consistent, they start to take care of the requests that have come at their door.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AklFtrGr8U-XmyiZ1CJx-0w.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AklFtrGr8U-XmyiZ1CJx-0w.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This time my friend Veronica comes and asks for my Tech Notes.&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="n"&gt;Veronica&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;want&lt;/span&gt; &lt;span class="n"&gt;your&lt;/span&gt; &lt;span class="n"&gt;latest&lt;/span&gt; &lt;span class="n"&gt;Tech&lt;/span&gt; &lt;span class="n"&gt;Notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="n"&gt;I&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Sure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;why&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;share&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;Dropbox&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;you&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
   &lt;span class="n"&gt;But&lt;/span&gt; &lt;span class="n"&gt;Veronica&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;access&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="n"&gt;few&lt;/span&gt; &lt;span class="n"&gt;minutes&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;have&lt;/span&gt; &lt;span class="n"&gt;written&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
   &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Tech&lt;/span&gt; &lt;span class="n"&gt;Note&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;laptop&lt;/span&gt; &lt;span class="n"&gt;which&lt;/span&gt; &lt;span class="n"&gt;will&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;synced&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;my&lt;/span&gt; 
   &lt;span class="n"&gt;Dropbox&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Veronica was able to access up-to-date Tech Notes but after few minutes of delay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strong Consistency offers up-to-date data but at the cost of high latency.&lt;/li&gt;
&lt;li&gt;While Eventual consistency offers low latency but may reply to read requests with stale data since all nodes of the database may not have the updated data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, please share it with others.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;This article was first published on Medium. You can take a look at it &lt;a href="https://medium.com/techspire/eventual-vs-strong-consistency-in-distributed-databases-282fdad37cf7" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>distributeddatabases</category>
      <category>technology</category>
      <category>database</category>
      <category>consistency</category>
    </item>
    <item>
      <title>Time of check to time of use (TOCTOU): A race condition</title>
      <dc:creator>saurabh.v</dc:creator>
      <pubDate>Fri, 02 Jun 2017 16:13:13 +0000</pubDate>
      <link>https://dev.to/saurabhgiv/time-of-check-to-time-of-use-toctou-a-race-condition</link>
      <guid>https://dev.to/saurabhgiv/time-of-check-to-time-of-use-toctou-a-race-condition</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was first published on Medium. You can take a look at it &lt;a href="https://medium.com/techspire/time-of-check-to-time-of-use-toctou-a-race-condition-99c2311bd9fc" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Explanation of this topic starts with an analogy taking an example from real life to understand the concept better.&lt;/p&gt;

&lt;p&gt;It was afternoon when John was going to the office and he told his wife Shahana that he will come by 5:00 in the evening. He told her to keep tea ready by then.&lt;/p&gt;

&lt;p&gt;Shahana cared for her husband. So the first thing that she did was to check if all ingredients required for making tea are there. Everything was fine except sugar which was in less quantity but it was enough to make tea for her husband.&lt;/p&gt;

&lt;p&gt;She felt satisfied and went to have sleep for few hours before her husband comes. While she was sleeping, their maid came and performed her regular household tasks.&lt;/p&gt;

&lt;p&gt;After completing her tasks, she felt tired and so she thought to have a cup of tea. Usually, she asks Shahana before using anything from kitchen but since Shahana was sleeping she proceeded with making a cup of tea for herself.&lt;/p&gt;

&lt;p&gt;She saw that everything required for making tea was available in the kitchen. It took few minutes for the tea to be ready. She drank and left.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Till this point, story looked bit plain but problem starts now.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Shahana woke up and since she was sure that there is enough quantity of each ingredient for making the tea, she started making it.&lt;/p&gt;

&lt;p&gt;In between the process of making, she realised that the sugar is not there.&lt;br&gt;
OH, MY GOD !!&lt;br&gt;
WHAT SHALL SHE DO NOW?&lt;/p&gt;

&lt;p&gt;This is where the concept of TOCTOU (TIME OF CHECK TO TIME OF UPDATE) kicks in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHAT IS TOCTOU?&lt;/strong&gt;&lt;br&gt;
Wikipedia states that&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;In software development, time of check to time of use (TOCTTOU or TOCTOU, pronounced “TOCK too”) is a class of software bug caused by changes in a system between the checking of a condition (such as a security credential) and the use of the results of that check. This is one example of a race condition.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;TOCTOU states that race condition can occur if the state of the system changes between the moment when some condition was checked by a process and the moment when the action was taken based on that condition by the same process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How is above-written example relevant?&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AyKoYNzf3_1AA-cvFM_g5cQ.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AyKoYNzf3_1AA-cvFM_g5cQ.jpeg" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we see the above example, shahana and maid are 2 processes which are trying to access the common resource which is sugar.&lt;/p&gt;

&lt;p&gt;Same situation can occur in concurrent programming environment when there is a single resource (object/ variable) being accessed and modified by more than one process.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Operating system can put a running process on hold and allow another process to use CPU for some time.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now it can happen that the new process is also modifying the same resource which was being accessed by the former process and hence the chances of occurrence of race condition erupt!!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, please share it with others.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>database</category>
      <category>racecondition</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>Functional Programming is Fun!! (PartÂ 1)</title>
      <dc:creator>saurabh.v</dc:creator>
      <pubDate>Tue, 16 May 2017 17:20:30 +0000</pubDate>
      <link>https://dev.to/saurabhgiv/functional-programming-is-fun-part1</link>
      <guid>https://dev.to/saurabhgiv/functional-programming-is-fun-part1</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was first published on Medium. You can take a look at it &lt;a href="https://medium.com/techspire/functional-programming-is-fun-part-1-7b3d81e1b97c" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Actually Functional programming is more than just being fun.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Functional programming concepts are important even if someone programs in imperative or object oriented languages.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It has many concepts which can be used in the program irrespective of the language a person programs in.&lt;br&gt;
Let’s take it one by one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When are functions called pure?&lt;/strong&gt;&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AX-IznQeGUHls3LlMPZFCGg.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2AX-IznQeGUHls3LlMPZFCGg.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A function is called pure if it always returns the same result for same argument values.&lt;br&gt;
Let’s understand it through an example&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;    &lt;span class="c1"&gt;# global variable
&lt;/span&gt;
&lt;span class="nf"&gt;sum_impure&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="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&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;span class="c1"&gt;# adds the value of global variable
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code Block 2&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="nf"&gt;sum_pure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above written 2 functions may look similar but they are not.&lt;br&gt;
Following example explains the difference:&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; 
&lt;span class="nf"&gt;sum_impure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# returns 8
&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;            &lt;span class="c1"&gt;# changed value of global variable to 6
&lt;/span&gt;&lt;span class="nf"&gt;sum_impure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# returns 9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sum_impure&lt;/code&gt; is an impure function because giving same input 3 as argument to it, it gives different output in each call.&lt;/p&gt;

&lt;p&gt;But this is not the case with &lt;code&gt;sum_pure&lt;/code&gt; function.&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="nf"&gt;sum_pure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;
&lt;span class="nf"&gt;sum_pure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Given 4, 9 as arguments to the &lt;code&gt;sum_pure&lt;/code&gt;, it will always return 13 no matter when and where it is being called in the program.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Do you get the difference between &lt;code&gt;sum_impure&lt;/code&gt; and &lt;code&gt;sum_pure&lt;/code&gt; function?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sum_impure&lt;/code&gt; function uses the value of global variable to calculate output but if some part of the program changes the value of global variable, the output of the &lt;code&gt;sum_impure&lt;/code&gt; function also changes for same input.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sum_pure’s&lt;/code&gt; output does not depend on the state of the program. It just acts on the input without requiring any information other than arguments to it from the program in which it is being called.&lt;/p&gt;

&lt;h3&gt;
  
  
  But why shall I make my functions pure?
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Easier Testing
&lt;/h4&gt;

&lt;p&gt;It is not easy to test &lt;code&gt;sum_impure&lt;/code&gt; function because &lt;code&gt;sum_impure&lt;/code&gt; function has to know the value of global variable x to execute. This is also known as state or context associated with a function.&lt;/p&gt;

&lt;p&gt;Secondly, you can’t compare &lt;code&gt;sum_impure&lt;/code&gt;’s result with some fixed value because it’s return value will change depending on the value of global variable x.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Now are you getting why we mock databases?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If a database read/write is there in a function definition, we can’t test that function without mocking the database.&lt;br&gt;
&lt;strong&gt;Databases are like state and the result of the function doing database read/write depends on what is there in the database during execution.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Impure functions need state to run and test.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  Predictability
&lt;/h4&gt;

&lt;p&gt;We can reason about what a pure function is doing because its results are predictable. Given same input, it always give same output.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Result of a pure function does not depend on the state of the program.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Result of &lt;code&gt;sum_impure(3)&lt;/code&gt; is unpredictable. So if &lt;code&gt;sum_impure&lt;/code&gt; is being called at 2 places in the program with same arguments, it may happen that it gives different results at these 2 places.&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="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="nf"&gt;sum_impure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# returns 7
&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="nf"&gt;sum_impure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# returns 10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many more advantages like parallelization, memoization associated with pure functions which we will learn in next article of this series.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you liked the article, please share it with others.&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Shared Mutability: Handle with care!!</title>
      <dc:creator>saurabh.v</dc:creator>
      <pubDate>Fri, 31 Mar 2017 15:28:46 +0000</pubDate>
      <link>https://dev.to/saurabhgiv/shared-mutability-handle-with-care</link>
      <guid>https://dev.to/saurabhgiv/shared-mutability-handle-with-care</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was first published on Medium. You can take a look at it &lt;a href="https://medium.com/techspire/shared-mutability-handle-with-care-351b8fa2f939" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's bifurcate these 2 words and understand them one by one.&lt;br&gt;
&lt;em&gt;Shared&lt;/em&gt; and &lt;em&gt;Mutability&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sharing&lt;/strong&gt;&lt;br&gt;
Sharing in computer science means sharing of resource like I/O, data etc between multiple processes. In case of data as a resource, sharing may mean permission to read and/or update it by multiple processes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Shared Reading&lt;/em&gt;&lt;br&gt;
Multiple processes are allowed to read a common resource but only one process can modify it at a time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Shared Modification&lt;/em&gt;&lt;br&gt;
Multiple processes can read as well as modify a particular resource at a time.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mutability&lt;/strong&gt;&lt;br&gt;
Mutability means ability to change. In computer science terms, it may mean any of the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Changing value of some variable&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;        &lt;span class="c1"&gt;# changing the value of variable
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Modifying a data structure&lt;/em&gt;&lt;br&gt;
In the example below, some nodes of binary tree are removed.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2Avu8VVmvcYJYicfJXxLxerw.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2Avu8VVmvcYJYicfJXxLxerw.png" title="Logo Title Text 1" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Modifying data kept in a data structure&lt;/em&gt;&lt;br&gt;
In the example below, value of fourth node of linked list is changed from 100 to 50.&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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ASS7zqOz_i2A2Wp042ikB7g.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%2Fcdn-images-1.medium.com%2Fmax%2F800%2F1%2ASS7zqOz_i2A2Wp042ikB7g.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Mutability is fine as long as single process is allowed to modify the data at a time. Sharing is fine as long as multiple processes do shared reading but not shared modification.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Problems come when the above mentioned 2 keywords &lt;em&gt;Sharing&lt;/em&gt; and &lt;em&gt;Mutability&lt;/em&gt; come together.&lt;br&gt;
In other words, problems arise when multiple processes are allowed to read as well as modify a common piece of data.&lt;/p&gt;

&lt;p&gt;How ????&lt;br&gt;
Let's understand it with the help of an example:&lt;br&gt;
There is a grocery store&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="n"&gt;groceries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;apple&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;banana&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strawberries&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cherries&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and as in every grocery store, there is a basket&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="n"&gt;basket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and a function to get groceries from grocery store&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;get_groceries&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;groceries&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="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;basket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;basket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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;span class="k"&gt;print&lt;/span&gt; &lt;span class="n"&gt;basket&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks like a fine piece of code !!&lt;br&gt;
Tom comes | picks up the basket | starts picking groceries from groceries array | puts it in basket.&lt;/p&gt;

&lt;p&gt;Life of tom is full of happiness !!&lt;/p&gt;

&lt;p&gt;While he was purchasing groceries, Jerry came and met tom. Tom asked Jerry to help him to get groceries. Jerry also started using the &lt;code&gt;get_groceries&lt;/code&gt; function to put groceries in &lt;strong&gt;Tom's basket&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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AFXstOcfLAr2yCDbLRgZDww.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%2Fcdn-images-1.medium.com%2Fmax%2F1000%2F1%2AFXstOcfLAr2yCDbLRgZDww.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By analysing the above example, we can see that since both Tom and Jerry were using the same basket they both saw that banana is not in the basket and hence both of them put banana in it.&lt;br&gt;
Now we have 2 bananas in the basket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basket acts as a shared resource which is being accessed by multiple processes like Tom and Jerry.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If Jerry would have taken the responsibility of only picking the basket and not putting anything in it then Tom would be only one adding groceries in it.In such a case, above mentioned problem wouldn't have occurred. &lt;strong&gt;[analogous to Shared Reading]&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Mutability by single process at a time&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not only both processes were &lt;em&gt;sharing&lt;/em&gt; the basket &lt;strong&gt;(common resource)&lt;/strong&gt; but also &lt;em&gt;mutating&lt;/em&gt; it &lt;strong&gt;(adding groceries in the basket)&lt;/strong&gt; at the same time. &lt;strong&gt;[analogous to Shared Modification]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hence the term &lt;strong&gt;Shared Mutability&lt;/strong&gt; !!&lt;/p&gt;

</description>
      <category>concurrency</category>
      <category>database</category>
      <category>datastructures</category>
      <category>softwaredesign</category>
    </item>
  </channel>
</rss>
