<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mohamed RF Hasan on Medium]]></title>
        <description><![CDATA[Stories by Mohamed RF Hasan on Medium]]></description>
        <link>https://medium.com/@mohamedrfhasan?source=rss-c51ba6ba2566------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cFDlMqQwtQiBAjG9.</url>
            <title>Stories by Mohamed RF Hasan on Medium</title>
            <link>https://medium.com/@mohamedrfhasan?source=rss-c51ba6ba2566------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 06:04:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mohamedrfhasan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[3 steps to write perfect code]]></title>
            <link>https://medium.com/@mohamedrfhasan/3-steps-to-write-perfect-code-ea589684f282?source=rss-c51ba6ba2566------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea589684f282</guid>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[perfectionism]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Mohamed RF Hasan]]></dc:creator>
            <pubDate>Mon, 20 Jul 2020 13:42:02 GMT</pubDate>
            <atom:updated>2020-07-20T13:42:02.624Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Note: I have used Ruby as just an example. This would apply for any programming language</blockquote><p>3 years ago, when I joined Freshworks, I learnt a new programming language — “Ruby”. As a part of my bootcamp, I solved 3 exercises. I instantly liked the language as it was so easy to read and write than the languages I had already known — C, C++, Java.</p><p>Today, after 3 years of professional experience in developing automated tests, reviewing code, writing utility scripts, reading gems(libraries) &amp; building small web applications<strong>, </strong>I wanted to redo the same exercises again and see how I have improved in solving problems with Ruby. Have a look at the problem statements and the code diff in the links given below:</p><blockquote>Problem Statements: <a href="https://github.com/hassyyy/RubyExercises/blob/master/README.md">https://github.com/hassyyy/RubyExercises/blob/master/README.md</a><br>v2017 vs v2020:<br><a href="https://github.com/hassyyy/RubyExercises/compare/v2017...v2020">https://github.com/hassyyy/RubyExercises/compare/v2017...v2020</a></blockquote><p>I could see a major difference in the way I write code. I have learnt that there are more aspects to consider for code to be <strong>perfect</strong> than just getting the logic right.</p><p>Let’s see how I write perfect code in 3 steps for the following problem statement</p><blockquote>Find out the numbers which appears exactly twice in an array<br>For example, if the input is [5, 2, 2, 1, 3, 5, 2, 6, 3, 2], the output should be [5, 3]</blockquote><h3><strong>Step-1 : Get the logic right</strong></h3><p>Does it solve the problem? Is it working as expected?</p><p>Write code that solves the problem first. Do not worry about the style and performance yet. All it matters now is whether the logic is right</p><blockquote>Make it work and then make it better</blockquote><pre>ip = [3, 2, 2, 1, 5, 3, 2, 6, 5, 2]</pre><pre>op = []<br>ip.each do |n|<br>  op &lt;&lt; n if ip.count(n) == 2<br>end<br>p op.uniq</pre><pre>+------------+----------------------------+<br>| Array Size | Execution Time(in seconds) |<br>| 10         | 0.000027                   |<br>| 100        | 0.000096                   |<br>| 1000       | 0.008036                   |<br>| 10000      | 0.695064                   |<br>| 100000     | 67.535688                  |<br>+------------+----------------------------+</pre><blockquote>Premature optimization is the root of all evil.</blockquote><blockquote>— Donald Knuth</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IscIBvZOMiVTsVrYEQ2dHQ.jpeg" /></figure><h3><strong>Step-2 : Make it easier to understand</strong></h3><p>Is it easy to read? Can I understand the logic by just looking at the code after a year or so?</p><p>This is one of the most important aspect of writing perfect code. Some of us just stop if the code works and move on. Later when we have to make some changes in future, we would have no clue about what’s happening and it is such a pain to understand the existing logic if the code is poorly written.</p><blockquote>Programs must be written for people to read, and only incidentally for machines to execute</blockquote><blockquote>— Harold Abelson</blockquote><p>Apply all the standards and conventions of the programming language in this step. Some examples would be giving proper names for variables &amp; methods, indenting the code properly, using in-built methods etc.</p><pre>input_numbers = [3, 2, 2, 1, 5, 3, 2, 6, 5, 2]</pre><pre>output_numbers = input_numbers.select{ |number| input_numbers.count(number).eql?(2) }</pre><pre>p output_numbers.uniq</pre><pre>+------------+----------------------------+<br>| Array Size | Execution Time(in seconds) |<br>| 10         | 0.000025                   |<br>| 100        | 0.000091                   |<br>| 1000       | 0.009406                   |<br>| 10000      | 0.704435                   |<br>| 100000     | 67.162448                  |<br>+------------+----------------------------+</pre><blockquote>Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live</blockquote><blockquote>— Martin Golding</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qvgfF1IElaKulYR18f-m1A.jpeg" /><figcaption>Actual reason behind Liam Neeson’s revenge in Taken :p</figcaption></figure><h3><strong>Step-3 : Tune its performance</strong></h3><p>Is it scalable? Will it be fast enough for larger sets of data?</p><p>Now is the time to really worry about how fast and how secure your code is. You can skip this if there is no way your code is going to be used for large sets of data or you can choose to do it later.</p><pre>input_numbers = [3, 2, 2, 1, 5, 3, 2, 6, 5, 2]</pre><pre>output_numbers = input_numbers.uniq.select{ |number| input_numbers.count(number).eql?(2) }</pre><pre>p output_numbers</pre><pre>+------------+----------------------------+<br>| Array Size | Execution Time(in seconds) |<br>| 10         | 0.000012                   |<br>| 100        | 0.000054                   |<br>| 1000       | 0.001173                   |<br>| 10000      | 0.039133                   |<br>| 100000     | 0.961844                   |<br>+------------+----------------------------+</pre><h4>Tips to improve:</h4><ol><li>Refer the documentation of commonly used classes or libraries and learn about all the available methods. For example: <a href="https://ruby-doc.org/core-2.5.1/String.html">String</a>, <a href="https://ruby-doc.org/core-2.5.1/Array.html">Array</a> &amp; <a href="https://ruby-doc.org/core-2.5.1/Hash.html">Hash</a> in Ruby</li><li>Learn about the standards and conventions of the programming language. Checkout this <a href="https://github.com/rubocop-hq/ruby-style-guide">style guide</a> for Ruby</li><li>Practise a lot. Write utility scripts to automate frequently performed workflows and build small applications</li><li>Review the code written by your peers. This is the perfect way to learn about different problems and understand why code must be easy to read</li><li>Understand how popular libraries work under the hood. I recently understood how my most favourite gem(<a href="https://github.com/mperham/sidekiq">sidekiq</a>) works and learnt how to write code that is strong in logic, easy to read and robust in performance</li></ol><p>You will be able to focus on just one aspect of the code at a time. You will be more confident and satisfied with the way you write code and it would motivate you to solve more problems with code. As you grow in experience, you will be seeing very little difference between each step and eventually you will be able to do all the steps in a single iteration.</p><figure><img alt="Happy Coding!" src="https://cdn-images-1.medium.com/max/1024/0*8Q124ldu8-kHhzoz.png" /><figcaption>Source: <a href="https://soyflare.com/Happy-Coding-Logo">https://soyflare.com/Happy-Coding-Logo</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea589684f282" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Run Heroku Workers Efficiently without idling in Free Plan]]></title>
            <link>https://medium.com/@mohamedrfhasan/run-heroku-workers-without-idling-in-free-plan-906bb1ab8432?source=rss-c51ba6ba2566------2</link>
            <guid isPermaLink="false">https://medium.com/p/906bb1ab8432</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[delayed-jobs]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[workers]]></category>
            <dc:creator><![CDATA[Mohamed RF Hasan]]></dc:creator>
            <pubDate>Sat, 07 Apr 2018 09:23:35 GMT</pubDate>
            <atom:updated>2018-04-07T10:33:11.226Z</atom:updated>
            <content:encoded><![CDATA[<p>Heroku offers a free usage of <strong>1000 hours</strong> (if <em>Credit Card</em> details are given) which will be sufficient enough of <strong>1 web dyno</strong> to run 24x7 [~750 hours per month]. But when you want <em>worker</em> dynos for running scheduled jobs, you have to upgrade to <strong>hobby</strong> plan which costs around <em>7$ per dyno per month </em>if your web application is run 24x7.</p><p>In Heroku’s free plan, the dynos will sleep after <a href="https://devcenter.heroku.com/articles/free-dyno-hours#dyno-sleeping">30 minutes of inactivity</a>. This would be really helpful in saving the hours of usage so that both web and worker dynos can be run under 1000 hours of free usage. But the worker should be awake at the time of scheduled tasks for them to be processed and executed. This means, there should be web traffic for your app at least 29 minutes before the time of a scheduled task.</p><p>So the problem now is</p><p><strong><em>Keeping your web application awake for worker to be active</em></strong></p><p>You can use <strong>monitoring services</strong> like New Relic, Pingdom, Uptime Robot(!) to keep your website alive 24x7 but this is inefficient as it would consume a lot of free hours. You don’t want this if you are using <em>more than 1 app</em> per account.</p><p>So the problem now is</p><p><strong><em>750 hours of web dyno + 750 hours of worker dyno = 1500 hours</em></strong></p><p>Now let’s see how this beautiful <a href="https://github.com/lostboy/workless"><strong>workless</strong></a> gem works. It would <em>start</em> the worker dyno when a job is scheduled and <em>stop</em> it once all the jobs are processed and executed. “<em>Voila! That’s it</em>” you might think. But unfortunately, it isn’t. Although the worker dyno will be run only when a job is scheduled, you could still end up <em>exceeding</em> 1000 hours of free usage if your worker runs for more than <em>10 days. </em>Again, this would not work if you use <em>more than 1 app</em> per account.</p><p>So the status now is</p><p><strong><em>Web Dyno: 24x7</em></strong></p><p><strong>Worker Dyno:</strong> <strong>Only when required</strong></p><p>What if we could use <em>monitoring services </em>only when a worker is running so that the web dyno can sleep after 30 minutes of inactivity. That is exactly what <a href="https://github.com/hassyyy/workless"><strong>my forked version</strong></a> of workless gem does. It would <em>start</em> the monitoring service when a worker dyno is started and <em>stop</em> the service once all the worker dynos are stopped. I have used <a href="https://uptimerobot.com/"><strong>Uptime Robot</strong></a> as the monitoring service as it is free and easy to use.</p><p>This means:</p><p><strong><em>Web Dyno: Active when worker dyno is running and idle after 30 minutes of inactivity when worker is not running</em></strong></p><p><strong>Worker Dyno:</strong> <strong>Only when required</strong></p><p>Hence, your web app will be consuming less of 1000 hours and you can use more than 1 app per account. Happy Developing!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=906bb1ab8432" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>