<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://kumaranimesh.me/feed.xml" rel="self" type="application/atom+xml" /><link href="https://kumaranimesh.me/" rel="alternate" type="text/html" /><updated>2024-08-09T15:15:07+00:00</updated><id>https://kumaranimesh.me/feed.xml</id><title type="html">My Life: Journey and beyond …</title><subtitle>This website is a personal blog of Animesh Kumar where he shares his life experiences and anything which he discovers in life and thinks to be worth sharing with the World.
</subtitle><entry><title type="html">Why Test Driven Development(TDD)?</title><link href="https://kumaranimesh.me/blog/post/2018/12/16/why-test-driven-development.html" rel="alternate" type="text/html" title="Why Test Driven Development(TDD)?" /><published>2018-12-16T11:40:00+00:00</published><updated>2018-12-16T11:40:00+00:00</updated><id>https://kumaranimesh.me/blog/post/2018/12/16/why-test-driven-development</id><content type="html" xml:base="https://kumaranimesh.me/blog/post/2018/12/16/why-test-driven-development.html"><![CDATA[<p>This blog starts with a brief definition of Test Driven Development.
Then we recall the basic goals of programming and understand how <abbr title="Test Driven Development">TDD</abbr> 
is essential for achieving these goals. Then we’ll see a very basic hands-on example of <abbr title="Test Driven Development">TDD</abbr>
in <a href="https://en.wikipedia.org/wiki/Python_(programming_language)" target="_blank">Python</a>.</p>

<h2>Definition</h2>

<p>
Test driven development is a style of development where you write the test cases beforehand and then run them.
What happens next? You see all your test cases failing as you've not implemented anything. Now you start
writing code to make the test cases pass and as all of the test cases pass, you're done. You've
come up with a correct version of code at the first place. Now you may refactor the code to optimize
it further and make sure that it still works correctly by running the test cases again. Fore more
information on <abbr title="Test Driven Development">TDD</abbr>, click
<a href="https://en.wikipedia.org/wiki/Test-driven_development" target="_blank">here</a>.
</p>

<h2>3 Goals of Programming</h2>

<p>Let’s get back to the basics and recall our basic goals while we’re writing code:</p>
<ol>
  <li>The code should be correct, today and in the unknown future</li>
  <li>The code should be readable for other programmers as well as future <em>You</em></li>
  <li>The code should be extensible i.e. ready to incorporate new changes without a re-write</li>
</ol>

<h4> Goal #1</h4>

<p>
We achieve this with TDD as we write the correct version of the code in the first place instead of debugging it later.
</p>

<h4> Goal #2</h4>
<p>Usually we break the test cases into different functions with each of them explaining the aspect of
specification they are testing. This makes reading the code easier as you know what the code is expected to do
rather than reading the standalone code and guessing what it is supposed to do.</p>
<p>
</p>

<h4> Goal #3</h4>

<p>
When you have a test suite with a decent coverage. You can extend the functionality and test it regressively
to ensure that the existing functionality is not breaking. Then add your test cases to
test the new functionality as well.
</p>

<h2> TDD Example </h2>

<p>
Most of us start to learn coding with a simple <code>print("Hello, World!")</code> program. But how do we get to know if we have
written it correctly or not? Well, it's simple, we run the program and see the result.
</p>
<p>
Then, on a later stage, if we are asked to write a program to find if <b>N</b> is a prime number or not.
We build our logic and implement it on code. To check if it's correct or not, we try it on some random inputs.
If the output is correct, we assume the program to be correct. And that's where we go wrong. We develop a habit of writing code
and then test it manually for some random inputs or some edge cases. As we get experienced with programming, we become
so confident that we omit the testing part at all. Below is one such program:
</p>

<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="s">"""
    :param n: int, greater than or equal to 1
    :return: bool, True if n is prime else False
    """</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
    <span class="k">return</span> <span class="bp">True</span>

<span class="k">print</span><span class="p">(</span><span class="n">is_prime</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="c1">#=&gt; prints 'True'
</span><span class="k">print</span><span class="p">(</span><span class="n">is_prime</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
<span class="c1">#=&gt; prints 'False'
</span><span class="k">print</span><span class="p">(</span><span class="n">is_prime</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="c1">#=&gt; prints 'True'
</span><span class="k">print</span><span class="p">(</span><span class="n">is_prime</span><span class="p">(</span><span class="mi">557</span><span class="p">))</span>
<span class="c1">#=&gt; prints 'True'</span></code></pre></figure>

<p>
I can assume it to be correct on the basis of my testing. But when I give 1 as input, it fails as shown below:
</p>

<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">print</span><span class="p">(</span><span class="n">is_prime</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
<span class="c1">#=&gt; prints 'True'</span></code></pre></figure>

<p>
Had we written test cases at the first place, we could have avoided this bug. Below is the 
<abbr title="Test Driven Development">TDD</abbr> approach for the same program. For simplicity,
I've not used Python's <a href="https://docs.python.org/3.7/library/unittest.html"><i>unittest</i></a>
module and instead wrote a simple function <code>test_is_prime</code> to test the <code>is_prime</code>
function.
<br />
Notice how we have kept the definition of <code>is_prime</code> empty on purpose.
</p>

<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="s">"""
    :param n: int, greater than or equal to 1
    :return: bool, True if n is prime else False
    """</span>
    <span class="k">pass</span>


<span class="k">def</span> <span class="nf">test_is_prime</span><span class="p">():</span>
    <span class="s">"""
    Tests the function is_prime for some set of inputs -&gt; {1, 2, 3, 4, 5, 100, 101, 557}
    """</span>
    <span class="n">prime_nums</span> <span class="o">=</span> <span class="p">{</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">101</span><span class="p">,</span> <span class="mi">557</span><span class="p">}</span>
    <span class="n">non_prime_nums</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">553</span><span class="p">}</span>

    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">prime_nums</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
        <span class="k">assert</span> <span class="n">result</span> <span class="ow">is</span> <span class="bp">True</span><span class="p">,</span> <span class="s">"{} is a prime number while `is_prime` returned `False`"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">non_prime_nums</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
        <span class="k">assert</span> <span class="n">result</span> <span class="ow">is</span> <span class="bp">False</span><span class="p">,</span> <span class="s">"{} is not a prime number while `is_prime` returned `True`"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Tested `is_prime` on input set -&gt; {}"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">prime_nums</span><span class="p">.</span><span class="n">union</span><span class="p">(</span><span class="n">non_prime_nums</span><span class="p">)))</span>
    <span class="k">print</span><span class="p">(</span><span class="s">"Ok"</span><span class="p">)</span>

<span class="n">test_is_prime</span><span class="p">()</span>

<span class="c1">#=&gt; Output:
</span><span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s">"is_prime.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">34</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
    <span class="n">test_is_prime</span><span class="p">()</span>
  <span class="n">File</span> <span class="s">"is_prime.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">24</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_is_prime</span>
    <span class="k">assert</span> <span class="n">result</span> <span class="ow">is</span> <span class="bp">True</span><span class="p">,</span> <span class="s">"{} is a prime number while `is_prime` returned `False`"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="nb">AssertionError</span><span class="p">:</span> <span class="mi">2</span> <span class="ow">is</span> <span class="n">a</span> <span class="n">prime</span> <span class="n">number</span> <span class="k">while</span> <span class="sb">`is_prime`</span> <span class="n">returned</span> <span class="sb">`False`</span></code></pre></figure>

<p>
Let's change <code>is_prime</code> definition and run the tests again:
</p>

<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="s">"""
    :param n: int, greater than or equal to 1
    :return: bool, True if n is prime else False
    """</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
    <span class="k">return</span> <span class="bp">True</span>

<span class="n">test_is_prime</span><span class="p">()</span>
<span class="c1">#=&gt; Output:
</span><span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s">"is_prime.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">33</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
    <span class="n">test_is_prime</span><span class="p">()</span>
  <span class="n">File</span> <span class="s">"is_prime.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">27</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_is_prime</span>
    <span class="k">assert</span> <span class="n">result</span> <span class="ow">is</span> <span class="bp">False</span><span class="p">,</span> <span class="s">"{} is not a prime number while `is_prime` returned `True`"</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="nb">AssertionError</span><span class="p">:</span> <span class="mi">1</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">a</span> <span class="n">prime</span> <span class="n">number</span> <span class="k">while</span> <span class="sb">`is_prime`</span> <span class="n">returned</span> <span class="sb">`True`</span></code></pre></figure>

<p>
Now fixing this 1 as input problem:
</p>

<figure class="highlight"><pre><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="s">"""
    :param n: int, greater than or equal to 1
    :return: bool, True if n is prime else False
    """</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">False</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">n</span> <span class="o">%</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">False</span>
    <span class="k">return</span> <span class="bp">True</span>

<span class="n">test_is_prime</span><span class="p">()</span>
<span class="c1">#=&gt; Output:
</span><span class="n">Tested</span> <span class="sb">`is_prime`</span> <span class="n">on</span> <span class="nb">input</span> <span class="nb">set</span> <span class="o">-&gt;</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">101</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">553</span><span class="p">,</span> <span class="mi">557</span><span class="p">}</span>
<span class="n">Ok</span></code></pre></figure>

<p>
Now we can be confident that our program is working for the mentioned input set. If we ever need to 
test the funtion for some more inputs, we just need to add them to <code>test_is_prime</code>
definition. This makes testing the program an automated process which can be repeated n number of
times without any manual intervention. Also, by writing program in such a manner, we avoid the 
pain of debugging the program later as we've written the program correct in the first place.
That's why we say:
</p>

<p><em>Testing rocks, debugging sucks.</em></p>

<p>
The source code of the final version of the program that we wrote above is available below. Download and try it yourself:
</p>

<p><a href="/assets/scripts/is_prime.py">source code</a></p>

<p>You can email me any questions/suggestions regarding this blog on my email address or ping me on the networking links provided below
in the footer.</p>]]></content><author><name></name></author><category term="blog" /><category term="post" /><category term="TDD" /><category term="Test Driven Development" /><summary type="html"><![CDATA[This blog starts with a brief definition of Test Driven Development. Then we recall the basic goals of programming and understand how TDD is essential for achieving these goals. Then we’ll see a very basic hands-on example of TDD in Python.]]></summary></entry><entry><title type="html">In Fond Memory of My Loving Pet, Picksy!</title><link href="https://kumaranimesh.me/blog/post/2018/12/02/my-loving-pet-picksy.html" rel="alternate" type="text/html" title="In Fond Memory of My Loving Pet, Picksy!" /><published>2018-12-02T11:00:00+00:00</published><updated>2018-12-02T11:00:00+00:00</updated><id>https://kumaranimesh.me/blog/post/2018/12/02/my-loving-pet-picksy</id><content type="html" xml:base="https://kumaranimesh.me/blog/post/2018/12/02/my-loving-pet-picksy.html"><![CDATA[<p>Meet Picksy:<br /><br /></p>

<p><img src="/assets/images/picksy_002.jpeg" alt="Picksy's photo" /></p>

<p>She had been with my family since 1<sup>st</sup> May 2005 and she was the most loved family member.
She left her body on 27<sup>th</sup> November 2018.
We were so much accustomed to her that it’s been very difficult to accept that she has left.
<br /></p>

<p>
Everyone in my family was involved with her in one way or the other. My mother used to give her food.
My sister and I used to take her for walk and take care of her whenever she would be ill. My father 
used to bring pedigree and other things she liked for her from the market.
</p>

<p>But the way she left her body was the least painful one possible. Also, she could drink 
<a href="https://www.speakingtree.in/blog/significance-behind-consuming-gangajal" target="_blank">Holy Gangajal</a> from my
mother’s hands in her last moments which ensures that one attains Nirvana after life.</p>

<p>Considering these things, we shouldn’t be sad at all for her as she has gone to her heavenly abode,
away from the shackles of this mortal world.</p>

<p>What we can learn from her demise:</p>
<ol>
  <li>Death is certain for everyone who has taken birth</li>
  <li>Loss of life is the biggest loss possible</li>
  <li>One should take care of those whom they are associated with and live the life in the present as 
we never know what is going to happen in the next moment</li>
  <li>One should always focus their mind on God, who is the eternal reason for this existence, and perform
their duties with full devotion</li>
</ol>

<p>Picksy will always be remembered for her loyalty, innocence and courage.</p>]]></content><author><name></name></author><category term="blog" /><category term="post" /><category term="pet" /><category term="picksy" /><summary type="html"><![CDATA[Meet Picksy:]]></summary></entry><entry><title type="html">My First Blog Post!</title><link href="https://kumaranimesh.me/blog/post/2017/11/11/my-first-blog-post.html" rel="alternate" type="text/html" title="My First Blog Post!" /><published>2017-11-11T08:30:00+00:00</published><updated>2017-11-11T08:30:00+00:00</updated><id>https://kumaranimesh.me/blog/post/2017/11/11/my-first-blog-post</id><content type="html" xml:base="https://kumaranimesh.me/blog/post/2017/11/11/my-first-blog-post.html"><![CDATA[<p>This is my first post on my blog called “My Life: Journey and beyond…”. Here I’ll share my thoughts and
anything which I discover in my life and I think it to be worth sharing with the World.
I’ll be currently posting one technical and one non-technical post alternately. Starting with an introduction to
myself.</p>

<p>I was born and brought up in Lucknow, Uttar Pradesh, India. I had been interested in science and technology since childhood.
But alongside I had also been interested in knowing the secret of life, death and how the existence came to be.
I liked science because it could give rational answers to few of my questions like “why is the color of sky blue?”,
“how can I see my duplicate in the mirror?” etc.</p>

<p>But more subtle questions were yet unanswered, like “Who am I?”, “What is my purpose and the purpose of the whole
existence at large?” etc. On one side, I continued to learn science and mathematics, and on the other, I was pursuing
my questions regarding myself and my purpose.</p>

<p>One day, when I was in 9th standard and biology class was going on,
my biology teacher told in the class that you all should, at your home, sit quietly for 5 minutes without allowing any
thoughts in the mind. He challenged that it would be difficult for us to do it, even for 5 minutes. He mentioned it’s
called “meditation”. I don’t remember if he told anything further about it or not. But I gave it a try and it was really
difficult to keep off the thoughts. When you’re thinking of not thinking, then also you’re thinking, so I was tricked.</p>

<p>Then I read more about <em>meditation</em> and found that it connects people to their real selves. This was exactly
what I had been seeking. So, I bought a book on <em>meditation</em> and tried to practice after reading from the book.
I could not continue it for a long but it gave me a direction.</p>

<p>Meanwhile, I graduated in Electronics &amp; Communication Engineering from Ajay Kumar Garg Engineering College,
Ghaziabad, India. I had developed an interest in <em>Software Engineering</em> in my college days only and hence, started
working as a software professional after graduating.</p>

<p>On my spiritual journey, I had found an authentic source and a companion to guide me through the life and that was
<em>Bhagwad Gita</em>.</p>

<p>Since then, I’ve been simply following the path as described in <em>Bhagwad Gita</em>. This path is about performing our
duties selflessly, but with full consciousness and enthusiasm. It gives you an opportunity to live your life to
the fullest, not as a servant of your mind or senses, but a master.</p>]]></content><author><name></name></author><category term="blog" /><category term="post" /><summary type="html"><![CDATA[This is my first post on my blog called “My Life: Journey and beyond…”. Here I’ll share my thoughts and anything which I discover in my life and I think it to be worth sharing with the World. I’ll be currently posting one technical and one non-technical post alternately. Starting with an introduction to myself.]]></summary></entry></feed>