<?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: Varun Barad</title>
    <description>The latest articles on DEV Community by Varun Barad (@varunbarad).</description>
    <link>https://dev.to/varunbarad</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%2F149239%2F626adfbf-d1ed-43ee-84a2-c61b271333e3.png</url>
      <title>DEV Community: Varun Barad</title>
      <link>https://dev.to/varunbarad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/varunbarad"/>
    <language>en</language>
    <item>
      <title>Dart Extension Methods</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Tue, 04 Feb 2020 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/dart-extension-methods-4c7a</link>
      <guid>https://dev.to/varunbarad/dart-extension-methods-4c7a</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CKlFMU7o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://varunbarad.com/assets/images/posts/headers/dart-extension-methods.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CKlFMU7o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://varunbarad.com/assets/images/posts/headers/dart-extension-methods.png" alt="Article Header - Dart Extension Methods"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Welcome devs, have you tried your hands at extensions methods? Dart recently announced support for extension methods (from Dart 2.7) and extension properties.&lt;/p&gt;

&lt;p&gt;Extension methods are a way to add functionality to existing libraries. When you are using someone else’s library it’s often impractical/impossible to change the API. But you can &lt;em&gt;extend&lt;/em&gt; it using extension methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are extension methods?
&lt;/h2&gt;

&lt;p&gt;Consider the following code that parses a string into an integer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'42'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Won’t it look cleaner/better if this functionality was available on &lt;code&gt;String&lt;/code&gt; class like below?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="s"&gt;'42'&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Since the language (where class &lt;code&gt;String&lt;/code&gt; comes from) doesn’t provide such a method we can’t parse a string to an int like that. Extension methods allow us to specify such custom functionalities not provided by the library (Dart language in this case). Let’s see how to do that in next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing a basic extension method
&lt;/h2&gt;

&lt;p&gt;So we want to have a method &lt;code&gt;parseInt&lt;/code&gt; &lt;strong&gt;on&lt;/strong&gt; &lt;code&gt;String&lt;/code&gt; class which would parse the given string to integer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="s"&gt;'42'&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For that we will define an extension method on &lt;code&gt;String&lt;/code&gt; like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="c1"&gt;// File: extension_methods.dart&lt;/span&gt;
&lt;span class="n"&gt;extension&lt;/span&gt; &lt;span class="n"&gt;StringExtras&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// We can also define getters as extensions&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="n"&gt;doubleLength&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we have named this set of extension methods as &lt;code&gt;StringExtras&lt;/code&gt;, you can name them anything you like.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; As you might have noticed above, we can define multiple extension methods/getters under a single extension block.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the extension method
&lt;/h2&gt;

&lt;p&gt;Extension methods (once imported) can be used just like any member method on that class’s object. Our example would go like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'extension_methods.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'42'&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Would print 50&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Similarly we can use the getters too&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'extension_methods.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'42'&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doubleLength&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Would print 4&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Important Notes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Extension methods &lt;em&gt;don’t&lt;/em&gt; work with the type &lt;code&gt;dynamic&lt;/code&gt;. However they &lt;em&gt;do&lt;/em&gt; work with Dart’s type inference.&lt;/li&gt;
&lt;li&gt;Since extension methods are resolved statically, they’re as fast as calling a static function.&lt;/li&gt;
&lt;li&gt;Extension methods can’t access private members of the class.&lt;/li&gt;
&lt;li&gt;If an extension member has a name conflict then you should refer &lt;a href="https://dart.dev/guides/language/extension-methods#api-conflicts"&gt;here&lt;/a&gt; for your options.&lt;/li&gt;
&lt;li&gt;You can define extension methods on classes which take generic type parameters (for example &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;). Refer &lt;a href="https://dart.dev/guides/language/extension-methods#implementing-generic-extensions"&gt;this section&lt;/a&gt; for further details.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;&lt;a href="https://dart.dev/guides/language/extension-methods"&gt;Official release documentation - https://dart.dev/guides/language/extension-methods&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Want to discuss this or any other interesting thing, hit me up on Twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dart</category>
      <category>flutter</category>
      <category>programming</category>
    </item>
    <item>
      <title>P vs NP Algorithm Problem Types</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Sun, 02 Feb 2020 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/p-vs-np-algorithm-problem-types-gn1</link>
      <guid>https://dev.to/varunbarad/p-vs-np-algorithm-problem-types-gn1</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YQsYMeOy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://varunbarad.com/assets/images/posts/p-np-venn-diagram.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YQsYMeOy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://varunbarad.com/assets/images/posts/p-np-venn-diagram.png" alt="Venn Diagram - Problem Types"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above image represents something completely un-understandable to me during my college years. I couldn’t understand what the professor tried to explain during my algorithms class when the topic of P vs NP vs NP-Complete problems came.&lt;/p&gt;

&lt;p&gt;Finally I understood it from &lt;a href="https://www.youtube.com/watch?v=EHp4FPyajKQ"&gt;a video by “Up and Atom”&lt;/a&gt;. The problems are grouped according to these 2 criteria:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Can a solution to the problem be found in polynomial time?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can a given solution for the problem be verified in polynomial time?&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  NP Problems
&lt;/h2&gt;

&lt;p&gt;These are the problems for which condition 2 holds true. If given a solution to such problem, the correctness of that solution can be verified in polynomial time. Condition 1 being true or false for such problems doesn’t affect whether they can fall under NP category or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  NP-Complete Problems
&lt;/h2&gt;

&lt;p&gt;These are the problems for which condition 2 holds true but condition 1 is false. A valid solution to the problem can’t be verified in polynomial time, but if given a solution to the problem the correctness of that solution can be verified in polynomial time.&lt;/p&gt;

&lt;h2&gt;
  
  
  P Problems
&lt;/h2&gt;

&lt;p&gt;For these problems, both conditions 1 &amp;amp; 2 hold true. There exist methods by which a valid solution to any of these problem can be found under polynomial time. And also, any given solution for such problem can be verified in polynomial time.&lt;/p&gt;

&lt;p&gt;Want to discuss this or any other interesting thing, hit me up on Twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Record Linux terminal session</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Sat, 26 Oct 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/record-linux-terminal-session-422g</link>
      <guid>https://dev.to/varunbarad/record-linux-terminal-session-422g</guid>
      <description>&lt;p&gt;I was recently setting up my computer with Ubuntu on it and this time I was decided that I would document every step of the process so that I can get it done faster the next time. That thought and process worked easily for the programs whose installations were just a simple &lt;code&gt;sudo apt install ...&lt;/code&gt; but when I was installing something new (like VS Code) there were many steps involved and it wasn’t an easy task to document them as I went on installing those things.&lt;/p&gt;

&lt;p&gt;What I decided to then do was to just record my terminal sessions for each such long program and review those recordings later to write a script for everything that I needed to do.&lt;/p&gt;

&lt;p&gt;Linux has a handy utility called &lt;code&gt;script&lt;/code&gt; which does just that. If it isn’t pre-installed then you can easily install it with &lt;code&gt;sudo apt install script&lt;/code&gt; as it is generally available in the default PPA.&lt;/p&gt;

&lt;p&gt;Using it is straight-forward too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;script &amp;lt;file_name_to_write_record_to&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I also find these options to be of significance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-a, --append&lt;/code&gt;: This appends the recording if the file is already existing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-c, --command &amp;lt;command_which_is_to_be_recorded&amp;gt;&lt;/code&gt;: If you want to record the interaction of a program with terminal then you can use this.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I found this command to be quite helpful in my recent endeavour of documenting the setup process and think it might others too. Want to discuss this or any other interesting thing, hit me up on Twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>linux</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Sync git fork with upstream</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Fri, 25 Oct 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/sync-git-fork-with-upstream-13oa</link>
      <guid>https://dev.to/varunbarad/sync-git-fork-with-upstream-13oa</guid>
      <description>&lt;p&gt;It is a common situation when we fork a project on Github and after some time we want to update our fork with the changes that have been integrated into the source project from where we forked. This is a simple guide on how to do that.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conditions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have the &lt;code&gt;remote&lt;/code&gt; of source project added to our local git repo.&lt;/li&gt;
&lt;li&gt;The name of the source project remote is &lt;code&gt;upstream&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We are syncing changes from &lt;code&gt;upstream/master&lt;/code&gt; to our local &lt;code&gt;master&lt;/code&gt; branch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below are the steps I found on Github’s guide.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Commit/Stash all your local changes.&lt;/li&gt;
&lt;li&gt;Checkout your local &lt;code&gt;master&lt;/code&gt; branch if not already there. &lt;code&gt;git checkout master&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Fetch (not pull) changes from &lt;code&gt;upstream&lt;/code&gt;. &lt;code&gt;git fetch upstream&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Merge changes from &lt;code&gt;upstream/master&lt;/code&gt; to local &lt;code&gt;master&lt;/code&gt;. &lt;code&gt;git merge upstream/master&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Resolve merge-conflicts, if any.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And you are done. Your local &lt;code&gt;master&lt;/code&gt; branch is now updated with changes from &lt;code&gt;upstream/master&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork"&gt;Github guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Syncing like this will only update your local &lt;code&gt;master&lt;/code&gt;. You will still need to push those synced changes to your Github repo if you want to update that too.&lt;/p&gt;

&lt;p&gt;This was just a quick tip that I needed quite a lot recently and every time I had to go and search for that guide. So I wrote this to solidify the concepts in my mind. If you have any more such tips I would love to hear about them at this &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>git</category>
      <category>github</category>
    </item>
    <item>
      <title>How to add “latest” post redirect to Jekyll site</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Tue, 08 Oct 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/how-to-add-latest-post-redirect-to-jekyll-site-4fbe</link>
      <guid>https://dev.to/varunbarad/how-to-add-latest-post-redirect-to-jekyll-site-4fbe</guid>
      <description>&lt;p&gt;I wanted to have a URL which I can give to someone and it would always take them to the latest post published on my blog. I ended up using (&lt;a href="https://varunbarad.com/blog/latest)%5Bvb-blog-latest%5D(https://varunbarad.com/blog/latest"&gt;https://varunbarad.com/blog/latest)[vb-blog-latest](https://varunbarad.com/blog/latest&lt;/a&gt;) as the URL.&lt;/p&gt;

&lt;p&gt;Since my website is statically generated and hosted, it wasn’t an option for me to specify a server-side &lt;code&gt;30x&lt;/code&gt; redirect for that particular URL to my latest post.&lt;/p&gt;

&lt;p&gt;So what I ended up doing is generate a page on each build which would redirect user to the latest post using JS. To get the information about latest post, I made use of Jekyll’s Liquid templating &lt;em&gt;syntax&lt;/em&gt; (not sure what else to call it).&lt;/p&gt;

&lt;p&gt;So the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tag of that page ended up looking like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
{%- assign latest_post = site.posts[0] -%}
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Redirecting you to {{ latest_post.title }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{{ latest_post.url }}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;

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



&lt;p&gt;Here I get the details of latest post by taking the first post in the list &lt;code&gt;site.posts&lt;/code&gt;. AFAIK &lt;code&gt;site.posts&lt;/code&gt; is sorted in descending order by publishing date, so this approach fetches me the last published post.&lt;/p&gt;

&lt;p&gt;Then it is a simple matter of setting the &lt;code&gt;window.location.href&lt;/code&gt; to the final url of the latest post.&lt;/p&gt;

&lt;p&gt;The final task remaining was to make sure that this page was compiled and generated exactly at &lt;code&gt;https://varunbarad.com/blog/latest&lt;/code&gt;. For that I specified a &lt;code&gt;permalink: "/blog/latest/"&lt;/code&gt; in the front-matter for that page. That way Jekyll knows where to put the final generated HTML file for that page.&lt;/p&gt;

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

&lt;p&gt;That was all, a simple way to add a URL to your Jekyll site which will always take the visitor to your latest blog post. You can take a look at the full source code of my website over at &lt;a href="https://github.com/VarunBarad/varunbarad.github.io/tree/development"&gt;GitHub&lt;/a&gt; or you can also find the full code of this “latest post redirect” page at &lt;a href="https://github.com/VarunBarad/varunbarad.github.io/blob/development/latest-post-redirect.html"&gt;this link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to talk with me about anything related to this feel free to reach out to me on twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>web</category>
      <category>jekyll</category>
      <category>ruby</category>
    </item>
    <item>
      <title>JavaScript 30 - Array Cardio 1</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Thu, 03 Oct 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/javascript-30-array-cardio-1-1h43</link>
      <guid>https://dev.to/varunbarad/javascript-30-array-cardio-1-1h43</guid>
      <description>&lt;p&gt;Day 4 of &lt;a href="https://javascript30.com"&gt;JS30 challenge&lt;/a&gt;, today I worked with various methods on array (I knew most of them beforehand) but I learned two cool things today.&lt;/p&gt;

&lt;p&gt;The key concepts I learned from today’s challenge were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Converting iterables to array in JS.&lt;/li&gt;
&lt;li&gt;Displaying array of objects nicely formatted in console.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Converting iterables to array in JS
&lt;/h2&gt;

&lt;p&gt;As you might have observed that &lt;code&gt;document.querySelectorAll&lt;/code&gt; doesn’t return us an actual &lt;code&gt;Array&lt;/code&gt; but instead it returns us what is called as a &lt;code&gt;NodeList&lt;/code&gt;. Consequently &lt;code&gt;NodeList&lt;/code&gt; doesn’t have all the methods that an &lt;code&gt;Array&lt;/code&gt; does (like &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt; etc).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NodeList&lt;/code&gt; is an example of what we call as “iterable” in JS. So whenever working with iterables we tend to prefer to convert them to an actual &lt;code&gt;Array&lt;/code&gt; so that we have more options to work with.&lt;/p&gt;

&lt;p&gt;I already knew of one way to convert them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;anyIterable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;convertedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;anyIterable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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



&lt;p&gt;Here we used the &lt;code&gt;Array.from&lt;/code&gt; method to convert the iterable to an &lt;code&gt;Array&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The other way I learned today was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;anyIterable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;convertedArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;anyIterable&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

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



&lt;p&gt;Here we made use of a combination of 2 things:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. ES6 spread operator &lt;code&gt;...&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;When we prepend an iterable with &lt;code&gt;...&lt;/code&gt; (called the ES6 spread operator) what it does is it extracts all values from that iterable and replaces the &lt;code&gt;...anyIterable&lt;/code&gt; part of the expression with those values.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Array literal &lt;code&gt;[]&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Then when we enclose the “spreaded” values with &lt;code&gt;[&lt;/code&gt; and &lt;code&gt;]&lt;/code&gt; it converts the whole thing into an array containing all the values from that iterable.&lt;/p&gt;

&lt;p&gt;Both produce the same result. I don’t know if one performs better than the other under large data-sets. But even though the &lt;code&gt;[...anyIterable]&lt;/code&gt; way is more concise, to me the &lt;code&gt;Array.from(anyIterable)&lt;/code&gt; usage seems more readable.&lt;/p&gt;

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

&lt;p&gt;That’s all folks, that was it for today. If you have anything unclear in this article or want to discuss anything else, hit me up on twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>web</category>
      <category>javascript</category>
      <category>javascript30</category>
    </item>
    <item>
      <title>JavaScript 30 - CSS Variables</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Wed, 02 Oct 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/javascript-30-css-variables-44c5</link>
      <guid>https://dev.to/varunbarad/javascript-30-css-variables-44c5</guid>
      <description>&lt;p&gt;On my day 3 of &lt;a href="https://javascript30.com"&gt;JS30 challenge&lt;/a&gt; I got introduced primarily to CSS variables but also to many other things.&lt;/p&gt;

&lt;p&gt;The key concepts I learned from today’s challenge were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CSS variables&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dataset&lt;/code&gt; property on DOM elements&lt;/li&gt;
&lt;li&gt;Image blur&lt;/li&gt;
&lt;li&gt;Color input&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  CSS variables
&lt;/h2&gt;

&lt;p&gt;CSS variables work very similar to how we use variables in JS or any other programming language. They let us define their value at one place and then just use that same value anywhere we refer to that variable.&lt;/p&gt;

&lt;p&gt;For example, we can define at one place that we want text on our website to be &lt;code&gt;white&lt;/code&gt; then we can define it like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;--text-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;p&gt;Here we define this &lt;code&gt;text-color&lt;/code&gt; variable at the root element. Now any place that we need to use this value we need to do something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--text-color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;p&gt;CSS variables can be used for more than just colors. In today’s project we used these variables for storing the border thickness and color of an image and also for storing the blur amount for that image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; One fun idea I have with CSS variables is that I can define the color palette of my website at one place and then provide the user with a night-mode switch. I can change colors across the whole website by changing them at just one place.&lt;/p&gt;

&lt;p&gt;To modify their value via JS a simple method call does the trick.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;dom&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;--text-color&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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



&lt;p&gt;The above snippet will change the value associated with &lt;code&gt;--text-color&lt;/code&gt; at any point in the &lt;code&gt;dom-element&lt;/code&gt; and any of &lt;code&gt;dom-element&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;dataset&lt;/code&gt; property on DOM elements
&lt;/h2&gt;

&lt;p&gt;In HTML whenever we want to write a custom-property on an element we make use of what are called &lt;code&gt;data&lt;/code&gt; attributes. A sample can be like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"blur"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"range"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"blur"&lt;/span&gt; &lt;span class="na"&gt;min=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt; &lt;span class="na"&gt;max=&lt;/span&gt;&lt;span class="s"&gt;"25"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"5"&lt;/span&gt; &lt;span class="na"&gt;data-sizing=&lt;/span&gt;&lt;span class="s"&gt;"px"&lt;/span&gt; &lt;span class="na"&gt;data-purpose=&lt;/span&gt;&lt;span class="s"&gt;"A slider input to select blur amount"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

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



&lt;p&gt;Here we have defined 2 data attributes, namely &lt;code&gt;data-sizing&lt;/code&gt; and &lt;code&gt;data-purpose&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now when we need to use these in JS we can simply do&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sizing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Used to access the data-sizing attribute.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;purpose&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Used to access the data-purpose attribute.&lt;/span&gt;

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



&lt;p&gt;&lt;code&gt;dataset&lt;/code&gt; is a very simple property on DOM elements which lists all the &lt;code&gt;data&lt;/code&gt; attributes defined on that element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Image blur
&lt;/h2&gt;

&lt;p&gt;Today I learnt a nifty simple trick to blur an image using only CSS.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;img&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;blur&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;p&gt;We can just specify a &lt;code&gt;blur&lt;/code&gt; CSS filter and our work is done. There are lots of other functions available which can be used as &lt;code&gt;filter&lt;/code&gt;, do make sure to take a look at that list.&lt;/p&gt;

&lt;h2&gt;
  
  
  Color input
&lt;/h2&gt;

&lt;p&gt;Standardisation of HTML and CSS has made it easy to create a simple and functional color-picker. The only thing that needs to be done is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"base"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"color"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"base"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"#43bc46"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

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



&lt;p&gt;Here we have just declared an input tag which then takes care of displaying a color picker and also shows the user with a tiny sample of the color they have picked.&lt;/p&gt;

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

&lt;p&gt;That’s all folks, that was it for today. If you have anything unclear in this article or want to discuss anything else, hit me up on twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>web</category>
      <category>javascript</category>
      <category>javascript30</category>
    </item>
    <item>
      <title>JavaScript 30 - CSS Analog Clock</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Tue, 01 Oct 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/javascript-30-css-analog-clock-2n1a</link>
      <guid>https://dev.to/varunbarad/javascript-30-css-analog-clock-2n1a</guid>
      <description>&lt;p&gt;Today I am continuing my work on &lt;a href="https://javascript30.com"&gt;JS30 challenge&lt;/a&gt; and building a simple analog clock using CSS shapes and then animate it with CSS transforms timed using JS.&lt;/p&gt;

&lt;p&gt;The key concepts I learned from today’s challenge were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Changing rotation center of an object in CSS&lt;/li&gt;
&lt;li&gt;CSS Transition timing function&lt;/li&gt;
&lt;li&gt;A weird reverse-jump in animation whenever any clock hand crosses 12 o’clock mark.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Changing rotation center of an object in CSS
&lt;/h2&gt;

&lt;p&gt;By default an object in CSS rotates around its center. But in our clock we need to rotate them around one of their corners. Therefore we need to change their CSS origins&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.hand&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transform-origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;p&gt;The above snippet moves the origin point for all CSS transforms to the right end of that element.&lt;/p&gt;

&lt;h2&gt;
  
  
  CSS Transition timing function
&lt;/h2&gt;

&lt;p&gt;You can provide a timing function for your CSS transitions so that they follow it for their rate of change during the duration of your transitions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.hand&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transition-timing-function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ease-in-out&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;p&gt;There are many built-in transition timing functions but I generally liked the &lt;code&gt;ease-in-out&lt;/code&gt; for how its acceleration and deceleration feel much like natural movements. You can check out the rest of them if you like.&lt;/p&gt;

&lt;p&gt;My favorite ability in this was that we can provide our custom timing functions using what is known as a bezier curve specification. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.hand&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transition-timing-function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cubic-bezier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0.42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0.58&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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



&lt;p&gt;In the above code-sample I have written the &lt;code&gt;cubic-bezier&lt;/code&gt; format of the &lt;code&gt;ease-in-out&lt;/code&gt; timing function but you can customize the bezier curve to your heart’s content using those 2 anchor points.&lt;/p&gt;

&lt;h1&gt;
  
  
  A weird reverse-jump in animation whenever any clock hand crosses 12 o’clock mark
&lt;/h1&gt;

&lt;p&gt;The hands of my clock were moving nicely from 12 to 12 but when they were just going to reach 12 they would do a weird jump and instead of moving 1 tick forward from right before 12 to 12, they would perform a full reverse circle and reach 12 from that side.&lt;/p&gt;

&lt;p&gt;This problem was occurring because of how we calculated the angles of rotation. We used the below equation for counting number of seconds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;secondsDegrees&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;360&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Here 90 degrees is the initial offset to make clock-hands start at 12&lt;/span&gt;

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



&lt;p&gt;With this what would happen is as below:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Seconds&lt;/th&gt;
&lt;th&gt;Degrees&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;180&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;270&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;45&lt;/td&gt;
&lt;td&gt;360&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;60 (or 0)&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;So the main issue is that when reaching the 60 second mark it rotates counter-clockwise because the final value of clock-hand handle (90) is less than its current value (360). So instead of the transition being a smooth forward transition, it jumps counter-clockwise and that’s where we see that queer problem.&lt;/p&gt;

&lt;p&gt;A very quick way to deal with this is to keep the degrees calculation so that they are always increasing. The result with that would be something like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Seconds&lt;/th&gt;
&lt;th&gt;Degrees&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;td&gt;180&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;270&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;45&lt;/td&gt;
&lt;td&gt;360&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;60 (or 0)&lt;/td&gt;
&lt;td&gt;450&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;If you want to discuss anything that was unclear in today’s article then hit me up on twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>web</category>
      <category>javascript</category>
      <category>javascript30</category>
    </item>
    <item>
      <title>JavaScript 30 - Building a Drum Kit</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Tue, 01 Oct 2019 04:00:36 +0000</pubDate>
      <link>https://dev.to/varunbarad/javascript-30-building-a-drum-kit-14a6</link>
      <guid>https://dev.to/varunbarad/javascript-30-building-a-drum-kit-14a6</guid>
      <description>&lt;p&gt;&lt;a href="https://wesbos.com"&gt;Wes Bos&lt;/a&gt; has created a &lt;a href="https://javascript30.com"&gt;course&lt;/a&gt; to get you confident in your JS skills in 30 days. In it you work on a new project daily for 30 days and in each project you make something using vanilla JS without any frameworks or libraries.&lt;/p&gt;

&lt;p&gt;Today I started the challenge and first project was to make a drum-kit where different sounds are played based on the keyboard button you press. And the corresponding button gets highlighted on screen.&lt;/p&gt;

&lt;p&gt;The key concepts I learned from today's challenge were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting event-listeners with JS&lt;/li&gt;
&lt;li&gt;Working with keyboard events and identifying key-codes&lt;/li&gt;
&lt;li&gt;Playing audio and restarting the audio when it is already playing&lt;/li&gt;
&lt;li&gt;How to sync JS code with CSS transitions&lt;/li&gt;
&lt;li&gt;What value &lt;code&gt;this&lt;/code&gt; takes inside the event listener function&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting event-listeners with JS
&lt;/h2&gt;

&lt;p&gt;The syntax for this is very simple&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;target_element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;event-name&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;function_to_call_when_event_is_fired&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can find a list of supported events &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Events"&gt;here&lt;/a&gt; but beware, the complete list of events is too large and you might never need to use most of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with keyboard events and identifying key-codes
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleKeyDownEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;keydown&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handleKeyDownEvent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we attach an event-listener for the &lt;code&gt;keydown&lt;/code&gt; event to the &lt;code&gt;window&lt;/code&gt; object since we want to capture the events across our whole web-page.&lt;br&gt;
Then we find out which key was pressed based on the &lt;code&gt;keyCode&lt;/code&gt; value passed via the fired event.&lt;/p&gt;

&lt;p&gt;Wes has created a &lt;a href="https://keycode.info"&gt;nifty little website (https://keycode.info)&lt;/a&gt; which we can use to find the corresponding &lt;code&gt;keyCode&lt;/code&gt; for any key.&lt;/p&gt;
&lt;h2&gt;
  
  
  Playing audio and restarting the audio when it is already playing
&lt;/h2&gt;

&lt;p&gt;In this project we have a set of buttons on the screen corresponding to different keys on the keyboard. And each button has a corresponding &lt;code&gt;&amp;lt;audio&amp;gt;&lt;/code&gt; element on the page. The goal is to play the corresponding audio when a valid key is pressed on the keyboard.&lt;/p&gt;

&lt;p&gt;So after detecting which key is pressed using the &lt;code&gt;keyCode&lt;/code&gt; attribute, we select the corresponding &lt;code&gt;&amp;lt;audio&amp;gt;&lt;/code&gt; element using a pre-specified &lt;code&gt;data-key&lt;/code&gt; attribute in HTML and call the &lt;code&gt;audioElement.play();&lt;/code&gt; method on it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;audioElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`audio[data-key="&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;keyCode&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"]`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;audioElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;currentTime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;audioElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;play&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above code-block you might have observed that I set the &lt;code&gt;current-time&lt;/code&gt; of that element to &lt;code&gt;0&lt;/code&gt; before calling &lt;code&gt;play&lt;/code&gt; on it. This is done because when an &lt;code&gt;audio&lt;/code&gt; is still playing (that is it has not finished) and we call &lt;code&gt;.play()&lt;/code&gt; on it, this new call gets ignored. Therefore we reset the play-time for that particular audio element so that it correctly registers and plays 2 consecutive commands of the same key.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to sync JS code with CSS transitions
&lt;/h2&gt;

&lt;p&gt;When a key is pressed, we highlight the corresponding button for a short duration on the screen while playing sound for it. To highlight the button, we just add a class &lt;code&gt;playing&lt;/code&gt; to it which scales it up and changes the border a bit. The second part of that is to remove the &lt;code&gt;playing&lt;/code&gt; class as soon as the transition is finished so as to return the button to normal state.&lt;/p&gt;

&lt;p&gt;For this I initially thought of using &lt;code&gt;setTimeout&lt;/code&gt; with the same time-out as that mentioned in CSS as transition duration. Wes pointed out the flaw in it is that when we decide to update the transition duration we would need to make sure to update it in both the places (in CSS and in JS).&lt;/p&gt;

&lt;p&gt;So a better way to synchronize CSS transitions with JS is again to use &lt;strong&gt;events&lt;/strong&gt;. Here we specifically use &lt;code&gt;transitionend&lt;/code&gt; event on the buttons like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;removeButtonHighlight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;propertyName&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;transform&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;playing&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelectorAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.keys&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;transitionend&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;removeButtonHighlight&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In above code, we select each button and then add the &lt;code&gt;transitionend&lt;/code&gt; event-listener for it so now it will call &lt;code&gt;removeButtonHighlight&lt;/code&gt; only and exactly when any transition on any of those buttons ends.&lt;/p&gt;

&lt;p&gt;After that we simply remove the &lt;code&gt;playing&lt;/code&gt; class from that button so that it returns back to its original/normal state. You might have noticed that I have put a check of &lt;code&gt;event.propertyName === 'transform'&lt;/code&gt;. I would suggest you try and remove that check and &lt;code&gt;console.log&lt;/code&gt; the event to find out why I might have put that check in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What value &lt;code&gt;this&lt;/code&gt; takes inside the event-listener function
&lt;/h2&gt;

&lt;p&gt;One of the toughest concept to learn and correctly understand in JS is &lt;code&gt;this&lt;/code&gt; or more specifically, what &lt;code&gt;this&lt;/code&gt; means at different points of execution in our code.&lt;/p&gt;

&lt;p&gt;In the last code-block you have noticed that I simply did &lt;code&gt;this.classList.remove('playing')&lt;/code&gt; instead of trying to select that particular element using any other method.&lt;/p&gt;

&lt;p&gt;It is so because in that particular case the value of &lt;code&gt;this&lt;/code&gt; is whatever object the function &lt;code&gt;removeButtonHighlight&lt;/code&gt; has been called.&lt;/p&gt;

&lt;p&gt;So when we attach an event-listener on each of our button and on triggering of those events, they call &lt;code&gt;removeButtonHighlight&lt;/code&gt; with the context of that particular button. So there &lt;code&gt;this&lt;/code&gt; is referring directly to the button on which the event listener has been triggered.&lt;/p&gt;

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

&lt;p&gt;Hope I have been able to explain what I learned in today's challenge. Hit me up on twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt; in case you haven't understood something from this article or you have something new for me to learn.&lt;/p&gt;

&lt;p&gt;In any case, don't forget to check out the &lt;a href="https://javascript30.com"&gt;JavaScript30 challenge&lt;/a&gt; from Wes Bos. It is an awesome way to get some JS confidence.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>web</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Switching RxJava schedulers between app code and corresponding tests</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Fri, 16 Aug 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/switching-rxjava-schedulers-between-app-code-and-corresponding-tests-2coo</link>
      <guid>https://dev.to/varunbarad/switching-rxjava-schedulers-between-app-code-and-corresponding-tests-2coo</guid>
      <description>&lt;p&gt;A simple way to switch between using appropriate schedulers in app code and using trampoline schedulers for tests.&lt;/p&gt;

&lt;p&gt;When using RxJava we need to specify the schedulers which we want to use to 1) perform the given task and 2) return result of the operation chain.&lt;/p&gt;

&lt;p&gt;One of the interesting ways I found while working on a take-home challenge for a company was as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;VarunSchedulers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;schedulers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RxSchedulers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RxSchedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;enableTesting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedulers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RxSchedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Test&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;disableTesting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedulers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RxSchedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;computation&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;computation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;schedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RxSchedulers&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt;
        &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;computation&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt;
        &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt;

        &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RxSchedulers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Schedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trampoline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;computation&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Schedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trampoline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Schedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trampoline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Default&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RxSchedulers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Schedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;computation&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Schedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;computation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;Scheduler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AndroidSchedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mainThread&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let’s take the example of fetching a network query, we can simply do this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;pandaService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCutestPanda&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribeOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;VarunSchedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;io&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observeOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;VarunSchedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&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 when we write our unit-tests we can easily turn testing-mode on and off using the &lt;code&gt;@Before&lt;/code&gt; and &lt;code&gt;@After&lt;/code&gt; methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeTestClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Before&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setupTests&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;VarunSchedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enableTesting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@After&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;tearDown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;VarunSchedulers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disableTesting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Write your tests&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now whenever we are inside tests, then automatically the &lt;code&gt;trampoline&lt;/code&gt; schedulers are assigned and respectively appropriate schedulers by default in case we are not in tests.&lt;/p&gt;

&lt;p&gt;If you have more such tricks up your sleeve or something in here isn’t clear to you or you just want to chat then hit me up on twitter &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>rxjava</category>
      <category>kotlin</category>
    </item>
    <item>
      <title>Make git forget a tracked file</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Thu, 06 Jun 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/make-git-forget-a-tracked-file-263h</link>
      <guid>https://dev.to/varunbarad/make-git-forget-a-tracked-file-263h</guid>
      <description>&lt;p&gt;The default &lt;code&gt;.gitignore&lt;/code&gt; file generated by Android Studio’s “New Project” wizard keeps some local IDE configuration files in VCS tracking. Nowadays I tweak the &lt;code&gt;.gitignore&lt;/code&gt; file immediately after creating a new project, so as to avoid getting those files tracked.&lt;/p&gt;

&lt;p&gt;But last week I opened one of my old projects and I hadn’t made those modifications to the &lt;code&gt;.gitignore&lt;/code&gt; back then, so it was tracking those files. I wanted git to stop tracking those files but still wanted to keep them in my file-system.&lt;/p&gt;

&lt;p&gt;So I went searching how to do it and found the below method.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Commit/Stash all your other changes&lt;/li&gt;
&lt;li&gt;Add that file to your &lt;code&gt;.gitignore&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Commit the &lt;code&gt;.gitignore&lt;/code&gt; changes&lt;/li&gt;
&lt;li&gt;Execute the command &lt;code&gt;git rm --cached &amp;lt;file&amp;gt;&lt;/code&gt; (For each file that you want to remove from tracking)&lt;/li&gt;
&lt;li&gt;Commit the removal of those files from VCS.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Word of caution:&lt;/strong&gt; This will keep the files in your directory but the next time these changes are pulled on some other machine, the removed files will be deleted from that machine. So it is advised to make a copy of those files first before pulling and then pasting your copied files back.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://stackoverflow.com/a/1274447"&gt;Stackoverflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was just a quick tip that I needed recently and thought of sharing. If you have any more such tips I would love to hear about them at this &lt;a href="https://twitter.com/varun_barad/status/1136608795973603332"&gt;twitter thread&lt;/a&gt;. Feel free to contact me for anything else that you would like to talk about &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>git</category>
      <category>protip</category>
    </item>
    <item>
      <title>Testing Deep-Link URLs using ADB</title>
      <dc:creator>Varun Barad</dc:creator>
      <pubDate>Thu, 02 May 2019 06:30:00 +0000</pubDate>
      <link>https://dev.to/varunbarad/testing-deep-link-urls-using-adb-151e</link>
      <guid>https://dev.to/varunbarad/testing-deep-link-urls-using-adb-151e</guid>
      <description>&lt;p&gt;ADB is a treasure trove and I regularly keep finding some gem from it. Recently I was working on creating some new deep-link integrations for the company I work at. The below command fires an event similar to one that gets fired when we click on a link in the device itself.&lt;/p&gt;

&lt;p&gt;The pattern for command to trigger the deep-link is as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;adb shell am start &lt;span class="nt"&gt;-a&lt;/span&gt; android.intent.action.VIEW &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"your-link-goes-inside-these-quotes"&lt;/span&gt;

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



&lt;p&gt;Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;adb shell am start &lt;span class="nt"&gt;-a&lt;/span&gt; android.intent.action.VIEW &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s2"&gt;"https://varunbarad.com/blog"&lt;/span&gt;

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



&lt;p&gt;This was just a quick ADB tip that I have learnt and wanted to share with you. If you have any more such tips or tricks, I would love to hear about them on &lt;a href="https://twitter.com/varun_barad/status/1123999186574303234"&gt;twitter thread for this article&lt;/a&gt;. Feel free to contact me for anything else that you would like to talk about &lt;a href="https://twitter.com/varun_barad"&gt;@varun_barad&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>tools</category>
    </item>
  </channel>
</rss>
