<?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: Vipul</title>
    <description>The latest articles on DEV Community by Vipul (@vipulbhj).</description>
    <link>https://dev.to/vipulbhj</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%2F68384%2F38308504-1e41-4ff3-be33-c2f824ffe372.jpeg</url>
      <title>DEV Community: Vipul</title>
      <link>https://dev.to/vipulbhj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vipulbhj"/>
    <language>en</language>
    <item>
      <title>The Curious case of res.render</title>
      <dc:creator>Vipul</dc:creator>
      <pubDate>Wed, 29 Dec 2021 15:06:26 +0000</pubDate>
      <link>https://dev.to/vipulbhj/the-curious-case-of-resrender-4k08</link>
      <guid>https://dev.to/vipulbhj/the-curious-case-of-resrender-4k08</guid>
      <description>&lt;p&gt;If you have ever tried to build a website, chances are you have encountered &lt;code&gt;res.render&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you program in &lt;code&gt;PHP (Laravel)&lt;/code&gt;, you know it as &lt;code&gt;view&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"home"&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;If you are coming from the land of &lt;code&gt;Ruby on Rails&lt;/code&gt;, you know it as &lt;code&gt;render&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="s2"&gt;"home"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you like yourself some &lt;code&gt;ExpressJS(Node)&lt;/code&gt;, you may know it in the form we are addressing it, the infamous &lt;code&gt;res.render&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;home&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Despite being a creature of the multiverse, and existing in so many forms, it has a relatively simple definition.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;res.render&lt;/code&gt; is a function that takes one argument, some sort of name, which represents some static content, for our concerns, &lt;code&gt;HTML&lt;/code&gt;, and sends back that as response to client requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's so curious about it?
&lt;/h2&gt;

&lt;p&gt;When I was thinking about adding template rendering support in &lt;a href="https://github.com/vipulbhj/nraf"&gt;NRAF&lt;/a&gt;, a very interesting question popped up.&lt;/p&gt;

&lt;p&gt;How would I, as a library author, know where does the template exist in the user's project? Sure, I can ask them to tell me an absolute path to that file, but other frameworks like &lt;code&gt;Express&lt;/code&gt; support defaults without asking the user to provide any.&lt;/p&gt;

&lt;p&gt;And so I wondered,&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do they even know where are the files ??, are they somehow finding the absolute path of the file, which call the &lt;code&gt;render&lt;/code&gt; function and thus find the path to root of the project. But, how the hell do you find the path of a file that calls a function ??&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And I had seen them all, &lt;code&gt;the Pledge&lt;/code&gt;, &lt;code&gt;the Turn&lt;/code&gt; and &lt;code&gt;the Prestige&lt;/code&gt;, accepted the magic trick got me. And started looking for clues on what made it tick.&lt;/p&gt;

&lt;h2&gt;
  
  
  The road called "Obvious"
&lt;/h2&gt;

&lt;p&gt;As my first instinct was to somehow find the path of the file which called &lt;code&gt;render&lt;/code&gt;, I started by googling &lt;a href="https://www.google.com/search?q=how+to+find+the+path+of+a+file+which+called+some+function"&gt;"how to find the path of a file which called some function"&lt;/a&gt;, which to my surprise let to some legit &lt;a href="https://stackoverflow.com/questions/13227489/how-can-one-get-the-file-path-of-the-caller-function-in-node-js"&gt;information&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While this is "a way" of hacking together a solution, this just didn't seem right. This would be extremely bad for performance, and shouts &lt;code&gt;HACK-IST&lt;/code&gt; from a distance. Moreover, it just didn't feel right, so I decided to dive into &lt;code&gt;ExpressJS&lt;/code&gt; source code and find how they are doing it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P.S: &lt;em&gt;Thank you open source.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the secret sauce
&lt;/h2&gt;

&lt;p&gt;In the super early days of &lt;a href="https://github.com/vipulbhj/nraf"&gt;NRAF&lt;/a&gt; development, I often referred to &lt;code&gt;Express&lt;/code&gt; source code for "inspiration", "validation of my ideas" and sometimes just pure learning. With that, I had some prior experience and familiarity with the source code which helped in finding things quickly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The good thing with the source code of &lt;code&gt;Express&lt;/code&gt; is, it's not huge and overly complicated by fancy build tools, so it's approachable to people of all experience levels.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I initially started by looking at what &lt;a href="https://github.com/expressjs/express/blob/ea537d907d61dc693587fd41aab024e9df2e14b1/lib/application.js#L70"&gt;default settings&lt;/a&gt; which are used when you initialised an app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;defaultConfiguration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;defaultConfiguration&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;env&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// default settings&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;enable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x-powered-by&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;etag&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;weak&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;env&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;env&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;query parser&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;extended&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;subdomain offset&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;trust proxy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&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="c1"&gt;// default configuration&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;view&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;View&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="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;views&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;views&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the call to &lt;a href="https://github.com/expressjs/express/blob/ea537d907d61dc693587fd41aab024e9df2e14b1/lib/application.js#L115"&gt;this.set('views', resolve('views'));&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This function is responsible for telling &lt;code&gt;Express&lt;/code&gt;, where it will find the templates, so I expected the second argument to be some sort a path, but it's a call to the &lt;code&gt;resolve&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;This means, the resolve function is responsible for finding the path to folder, where we will find the templates. Below is the definition of &lt;code&gt;resolve&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;ext&lt;/span&gt; &lt;span class="o"&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;ext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// &amp;lt;path&amp;gt;.&amp;lt;ext&amp;gt;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;stat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tryStat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&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;stat&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isFile&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// &amp;lt;path&amp;gt;/index.&amp;lt;ext&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;basename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;index&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;stat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tryStat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&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;stat&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;stat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isFile&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;path&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;What, how, wait, huh !!&lt;/p&gt;

&lt;p&gt;I expected this function to do something related to finding the &lt;code&gt;views&lt;/code&gt; folder, but it's doing nothing like that. It just checks if the given path is a file or a directory and accordingly return a new path, joining the two.&lt;/p&gt;

&lt;p&gt;This was strange, and at this point I got really confused, I expected to see something very clear on how they location the absolute path of your project, but it seems like they don't set it explicitly and just magically know somehow.&lt;/p&gt;

&lt;p&gt;After that, I was just browsing through files in the project, to find some more hints on how to find the path, when I noticed the &lt;a href="https://github.com/expressjs/express/blob/ea537d907d61dc693587fd41aab024e9df2e14b1/lib/view.js#L104"&gt;lookup&lt;/a&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;View&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lookup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;roots&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[].&lt;/span&gt;&lt;span class="nx"&gt;concat&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;root&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;lookup "%s"&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;roots&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;roots&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// resolve the path&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;loc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dirname&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;basename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// resolve the file&lt;/span&gt;
    &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;path&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;I figured this was something important, but didn't understand what it was doing precisely. I was about to start putting in some break points in the function definition, when I suddenly noticed, the variable &lt;code&gt;roots&lt;/code&gt;. That gave an idea on what this function might be doing, so I quickly opened up my terminal and started bootstrapping a small project to experiment and validate the idea.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /tmp
&lt;span class="nb"&gt;mkdir &lt;/span&gt;express-experiment
&lt;span class="nb"&gt;cd &lt;/span&gt;express-experiment
yarn init &lt;span class="nt"&gt;-y&lt;/span&gt;
yarn add express ejs
vim index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point I had an &lt;code&gt;Express&lt;/code&gt; project initialised, then I created an endpoint to test the hypothesis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&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;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&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;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&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="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;home&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this should do is, use the defaults and look for a folder called &lt;code&gt;views&lt;/code&gt; in my project and render a file called &lt;code&gt;home.ejs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But there is no &lt;code&gt;views&lt;/code&gt; folder in the project folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules/
  &amp;gt; express

index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I though what if, I add a views folder outside my project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ..
&lt;span class="nb"&gt;mkdir &lt;/span&gt;views
&lt;span class="nb"&gt;cd &lt;/span&gt;views
vim hello.ejs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and added a &lt;code&gt;hello.ejs&lt;/code&gt; like in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello World from Views outside the project folder&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After which I started the &lt;code&gt;Express&lt;/code&gt; server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;express-experiment
node index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and opened &lt;code&gt;http://localhost:3000&lt;/code&gt; in the browser, and there it was, just as I had expected. An ERROR, precisely the one I expected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FubtP4vH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgdvsrybdaxu02efk03c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FubtP4vH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgdvsrybdaxu02efk03c.png" alt="Permission Error" width="880" height="329"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So, what just happened?
&lt;/h2&gt;

&lt;p&gt;Well people, we found what makes the magic trick tick, and it's so clever, I am still giggling about it, but yet so obvious, I feel a little dumb I didn't see it before.&lt;/p&gt;

&lt;p&gt;So, how does &lt;code&gt;Express&lt;/code&gt; know, where to find your &lt;code&gt;views&lt;/code&gt; folder. Well, it doesn't know, but it knows all the possible places it might be at.&lt;/p&gt;

&lt;p&gt;The smart people who wrote the &lt;code&gt;Express&lt;/code&gt; framework, noticed a key detail about how your dependencies are stored inside your projects. The &lt;code&gt;node_modules&lt;/code&gt; folder is part of the project itself, which means any dependencies you install, will be installed as a subfolder in your project, which in-turn means, this &lt;code&gt;views&lt;/code&gt; folder that we are looking for has to be somewhere in this chain.&lt;/p&gt;

&lt;p&gt;And that's exactly what they do, they removed each level of depth from the absolute path, and look for this folder called &lt;code&gt;views&lt;/code&gt; and whenever they find it, &lt;code&gt;BINGO&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I genuinely laughed for five minutes straights after I saw that error, only the few rare times I laughed after seeing an error :p (&amp;gt;_&amp;lt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  How I am going to implement it in &lt;a href="https://github.com/vipulbhj/nraf"&gt;NRAF&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;While I liked how they did it, it also made me think about how this for one can cause a lot of confusion, if not any security issues.&lt;/p&gt;

&lt;p&gt;Imagine a situation where you have some folder structure like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;learning/
  views/
    home.ejs
  coding/
    express-practice/
      index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I know it's a bit of a stretched of imagination, but in this case, if you forget to add a &lt;code&gt;views&lt;/code&gt; folder to your project, it will starts rending files from the &lt;code&gt;views&lt;/code&gt; folder, some level higher up the path, which might end up causing a lot of confusion and become a frustrating problem.&lt;/p&gt;

&lt;p&gt;The probability of this happening to someone who is just starting is very realistic, and so to avoid all this, &lt;a href="https://github.com/vipulbhj/nraf"&gt;NRAF&lt;/a&gt; will require you to tell the path of your template folder explicitly, which is optional in &lt;code&gt;Express&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will use a syntax similar to how you can do it in &lt;code&gt;Express&lt;/code&gt;, using &lt;code&gt;app.set('views', path.join(__dirname, 'views'));&lt;/code&gt;, but would be a mandatory thing to do, and if you forget, it will show you a nice error message telling you what you need to do :p&lt;/p&gt;

&lt;p&gt;&lt;em&gt;And that's about it, thank you so much for reading my brain dump. Hope it wasn't all blabbering.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you wanna talk about anything, open an issue, or &lt;strong&gt;leave a star on &lt;a href="https://github.com/vipulbhj/nraf"&gt;NRAF&lt;/a&gt;, maybe :)&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Haskell 101: A glance</title>
      <dc:creator>Vipul</dc:creator>
      <pubDate>Sat, 13 Nov 2021 05:42:05 +0000</pubDate>
      <link>https://dev.to/vipulbhj/haskell-101-a-glance-5a9j</link>
      <guid>https://dev.to/vipulbhj/haskell-101-a-glance-5a9j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;A &lt;code&gt;general-purpose, statically typed, purely functional, lazy evaluated&lt;/code&gt; programming language, that will hurt you more than &lt;strong&gt;your first breakup(no exaggeration).&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;A language that will make you feel like a noob, doesn't matter how many years of experience you have writing lame &lt;code&gt;JAVA&lt;/code&gt; code.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Born out of the complexities of &lt;a href="https://en.wikipedia.org/wiki/Lambda_calculus"&gt;Lambda Calculus&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Category_theory"&gt;Category Theory&lt;/a&gt;, in the dark valleys of &lt;a href="https://en.wikipedia.org/wiki/Glasgow_Haskell_Compiler"&gt;Glasgow&lt;/a&gt;, &lt;code&gt;Haskell&lt;/code&gt; nerds will make sure you leave that book after the preface and never come back because you realize, you are too dumb for this shit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A language so gorgeous, you will fall in love. A language so powerful, it will make your brain bigger(like physically bigger). A language so sick, even I don't understand half of it(but the other half, that's some kick-ass stuff).&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Step[0]&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you plan to try some of the examples, I would highly recommend setting up &lt;code&gt;Haskell&lt;/code&gt; locally on your machine. You can follow &lt;a href="https://www.haskell.org/platform/"&gt;this&lt;/a&gt; guide to download and set up &lt;code&gt;ghc&lt;/code&gt; and &lt;code&gt;ghci&lt;/code&gt;, we will use &lt;code&gt;ghci&lt;/code&gt; for all examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;Can't I just use an online compiler ??&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Well, in theory sure, if you google, you can find some options, I have used &lt;a href="https://replit.com/languages/haskell"&gt;Haskell replit&lt;/a&gt; in the past, &lt;strong&gt;&lt;em&gt;but&lt;/em&gt;&lt;/strong&gt;(and "a big fat &lt;code&gt;but&lt;/code&gt;, as far as &lt;code&gt;but's&lt;/code&gt; go") there is a problem, you see currently you are a &lt;code&gt;Haskell&lt;/code&gt; baby, all you know is and do is cry, but these &lt;code&gt;REPL&lt;/code&gt; environments are designed for grownups. And if I were to take you from a baby to grownup, we will have to talk about, what &lt;code&gt;purely functional&lt;/code&gt; really means, and how &lt;code&gt;IO&lt;/code&gt; is always impure by design, and then I will have to tell you about how &lt;code&gt;Haskell&lt;/code&gt; ended up going around that issue using &lt;code&gt;IO Monad&lt;/code&gt;, but then you will ask what a &lt;code&gt;Monad&lt;/code&gt; is, and then I will say, "whatever you want it to be" while simultaneously being something "no one really understands, we all just pretend" and then we will have to talk about "that thing" and then "that other thing" and after a while, we reach stuff, even I don't understand and by then you will have a serious headache and man oh man, a serious headache can cause permanent brain damage to a baby, so no I ain't taking that risk, just set up the f**king thing on your machine locally.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick overview
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Haskell&lt;/code&gt; is &lt;code&gt;general-purpose&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Can be used to build applications of all sorts, there is no specific domain for which the language was designed. &lt;code&gt;DSLs&lt;/code&gt;(Domain Specific Languages) like &lt;code&gt;SQL&lt;/code&gt; are examples of some languages which were designed to only solve problems of a smaller, restrictive use-case, but there is nothing like that in &lt;code&gt;Haskell&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also randomly, &lt;code&gt;--&lt;/code&gt; in &lt;code&gt;Haskell&lt;/code&gt; is a single-line comment, while &lt;code&gt;{--}&lt;/code&gt; create multiline comments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- This is a single line comment&lt;/span&gt;

&lt;span class="cm"&gt;{-
I am
multiline comment
-}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;Haskell&lt;/code&gt; is &lt;code&gt;purely functional&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;which implies functions can't have &lt;a href="https://en.wikipedia.org/wiki/Side_effect_(computer_science)"&gt;side-effects&lt;/a&gt; or think of them as more like functions in &lt;code&gt;Mathematics&lt;/code&gt;, they take an input and produce an output. You can apply rules which govern what can be a possible input and what can be a possible output, just like in &lt;code&gt;maths&lt;/code&gt; referring to the &lt;code&gt;Domain&lt;/code&gt; and &lt;code&gt;Range&lt;/code&gt; of a function, and for any given input, the function will produce the same output &lt;strong&gt;&lt;em&gt;always&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are no &lt;code&gt;classes&lt;/code&gt;, &lt;code&gt;objects&lt;/code&gt;, &lt;code&gt;references&lt;/code&gt;, &lt;code&gt;pointers&lt;/code&gt;, &lt;code&gt;mutable variables&lt;/code&gt;, none of that and so almost all you know as an &lt;code&gt;OOPS&lt;/code&gt; &lt;a href="https://github.com/vipulbhj/vipulbhj/blob/main/blogs/ProgrammingLanguages/Haskell/Haskell101/definations.md#btch"&gt;btch&lt;/a&gt; goes out of the window.&lt;/p&gt;

&lt;p&gt;function syntax in &lt;code&gt;Haskell&lt;/code&gt; is easy, let's see a few examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Function called `addTwoNumbers` takes two numbers and returns the result of the addition.&lt;/span&gt;
&lt;span class="n"&gt;addTwoNumbers&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="c1"&gt;-- Function called `multiplyTwoNumbers` takes two numbers and returns the result of multiplication.&lt;/span&gt;
&lt;span class="n"&gt;multiplyTwoNumbers&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope that gave you a bit of intuition, I know it wasn't much but I don't think this can be taught in a flyby overview writeup, so I recommend reading &lt;a href="http://learnyouahaskell.com/syntax-in-functions"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Haskell&lt;/code&gt; is &lt;code&gt;statically typed&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;You can specify types for things in &lt;code&gt;Haskell&lt;/code&gt;, but its kinds optional, till you have that case where it isn't, but very rare to have that case. This optional behaviour is thanks to &lt;code&gt;Haskell's type inference&lt;/code&gt; system, which is &lt;strong&gt;chef's kiss&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;let's look at some examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- You can add types to constants&lt;/span&gt;
&lt;span class="n"&gt;intTypeValuesOnly&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;intTypeValuesOnly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;    &lt;span class="c1"&gt;-- (ok)&lt;/span&gt;
&lt;span class="n"&gt;intTypeValuesOnly&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;6.212&lt;/span&gt; &lt;span class="c1"&gt;-- (bad, gives error, see image below)&lt;/span&gt;

&lt;span class="c1"&gt;-- You can add types to functions&lt;/span&gt;
&lt;span class="n"&gt;addTwoInts&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;addTwoInts&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="n"&gt;addTwoInts&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;  &lt;span class="c1"&gt;-- 30&lt;/span&gt;
&lt;span class="n"&gt;addTwoInts&lt;/span&gt; &lt;span class="mf"&gt;5.2&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;-- error similar to the example above.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H27aN6Vq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opobfqrjtfkg8gl6ulxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H27aN6Vq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opobfqrjtfkg8gl6ulxx.png" alt="code execution result" width="880" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope that all was pretty straightforward, if you wanna learn more about the available types, you can read at &lt;a href="http://learnyouahaskell.com/types-and-typeclasses"&gt;http://learnyouahaskell.com/types-and-typeclasses&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;Haskell&lt;/code&gt; has &lt;code&gt;lazy evaluation&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This one is interesting and a key differentiating factor between &lt;code&gt;Haskell&lt;/code&gt; and some of the other functional programming languages. Lazy evaluation means &lt;code&gt;Haskell&lt;/code&gt; won't execute some code until it needs to use its output, say for printing it to the stdout or something.&lt;/p&gt;

&lt;p&gt;At first glance, this might seem like it, it has no good use-case, but in some scenarios, laziness can come in handy and that's why several programming languages like &lt;code&gt;Ruby&lt;/code&gt; have added support for laziness.&lt;/p&gt;

&lt;p&gt;for example, if you want to write a function that calculates the sum of the first N natural numbers, it will look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;findSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&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="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;sum&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 is how you can implement the same thing in &lt;code&gt;Haskell&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight haskell"&gt;&lt;code&gt;&lt;span class="n"&gt;findSum&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;findSum&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;$&lt;/span&gt; &lt;span class="n"&gt;take&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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;What's happening here is, we are creating an Infinite list, using the range syntax, &lt;code&gt;[1..]&lt;/code&gt;, if you wanted to create a list of the first 6 natural numbers, you can just say &lt;code&gt;[1..6]&lt;/code&gt; and since in our case we haven't specified any upper bound, the list keeps increasing Infinitely and laziness ensures the program does go crazy.&lt;/p&gt;

&lt;p&gt;Next, we use the &lt;code&gt;take&lt;/code&gt; function to take the first &lt;code&gt;n&lt;/code&gt; number of items out of a list. For example, &lt;code&gt;take 3 [1, 2, 3, 4, 5]&lt;/code&gt; will result in &lt;code&gt;[1, 2, 3]&lt;/code&gt;, then we use another built-in function called &lt;code&gt;sum&lt;/code&gt;, which sums up all elements in a list. Combine all these and you have yourself a clean one-line implementation, now tell me that ain't pretty.&lt;/p&gt;

&lt;p&gt;Laziness isn't just limited to creating infinite collections, but it turn out, its great for compiler optimizations as well. laziness gives &lt;code&gt;Haskell&lt;/code&gt; compilers the power of arbritrary restructuring code at compilation time, very cool 😛😛.&lt;/p&gt;

&lt;p&gt;And that's about it I guess 😅😅😅&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Programming Languages - A comparison with Human Languages  </title>
      <dc:creator>Vipul</dc:creator>
      <pubDate>Tue, 25 May 2021 09:14:34 +0000</pubDate>
      <link>https://dev.to/vipulbhj/programming-languages-a-comparison-with-human-languages-3p20</link>
      <guid>https://dev.to/vipulbhj/programming-languages-a-comparison-with-human-languages-3p20</guid>
      <description>&lt;p&gt;&lt;strong&gt;Disclaimer&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This mess is a lighthearted read, and uses the F word once, with some other weird stuff, so if you are sensitive about that sort of thing, &lt;strong&gt;I am sorry and these are just jokes&lt;/strong&gt;. &lt;em&gt;Enjoy the read and hey, if you like some of the jokes, I go by &lt;a class="mentioned-user" href="https://dev.to/vipulbhj"&gt;@vipulbhj&lt;/a&gt;
 on Twitter and I don't hate compliments :p&lt;/em&gt;.&lt;br&gt;&lt;br&gt;
P.S: Wait, does anyone hate compliments, DM me if you do, I am curious and also I love you and being different is awesome.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Programming Languages
&lt;/h1&gt;

&lt;p&gt;I recently encountered this tweet where the user was curious on how programming languages compare to the other more familiar human languages. This reminded me of all times when I wondered if programming in &lt;a href="https://en.wikipedia.org/wiki/Hindi"&gt;Hindi&lt;/a&gt; will it make my code safer, since non &lt;a href="https://en.wikipedia.org/wiki/Hindi"&gt;Hindi&lt;/a&gt; speakers wouldn't be able to read my code :p&lt;/p&gt;

&lt;p&gt;So in this blog(blabbering session), I will try to deep dive(to anxious overthinking depth levels) into this subject. So, let's go....&lt;/p&gt;

&lt;h2&gt;
  
  
  What is language
&lt;/h2&gt;

&lt;p&gt;According to Wikipedia, &lt;strong&gt;Language is&lt;/strong&gt;  ummm.. you know what, forget this. You are reading this just fine, fair enough that proves you know what a language is, no mansplaining here, this is a safe space, or is it (EVIL LAUGH)&lt;/p&gt;

&lt;p&gt;Yeah so whatever, back to the subject. So, since you already know what a language is, you also know it has some components, let's talk about those.&lt;/p&gt;

&lt;h2&gt;
  
  
  Components of Language
&lt;/h2&gt;

&lt;p&gt;Although, based on the perspective and the research you are looking at, googling for &lt;a href="https://www.google.com/search?q=components%20of%20language"&gt;components of language&lt;/a&gt; will give you a lot of results, but it all essentially boils down to, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Letters&lt;/strong&gt; -&amp;gt; atomic core of the language, finite and unchanging.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Words&lt;/strong&gt; -&amp;gt; first layer of abstraction, ordered combination of letters which have a specific meaning(&lt;em&gt;I have no clue how this works, but somehow the whole f&lt;/em&gt;&lt;em&gt;k world just accepts, okay this combination means this, like how, seriously, what's the process of formally expressing denial here&lt;/em&gt;) and rhyme(&lt;em&gt;lets face it, pronunciation is a lame word&lt;/em&gt;), over time new words can be are added and rarely old ones are removed, but trends change, thing become uncool and and people stop using it by choice. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sentences&lt;/strong&gt; -&amp;gt; second layer of abstraction, introduces the concept of &lt;a href="https://en.wikipedia.org/wiki/Grammar"&gt;Grammar&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And using these, I could blabber all the things you just read(sorry, not sorry).&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Programming Language
&lt;/h2&gt;

&lt;p&gt;Who wants to ask Wikipedia, yup no one, just as I figured. &lt;/p&gt;

&lt;p&gt;So, programming language allow humans to communicate with computers, and control them and feel like &lt;strong&gt;&lt;a href="https://powerpuffgirls.fandom.com/wiki/Mojo_Jojo_%281998_TV_series%29"&gt;Mojo Jojo&lt;/a&gt;&lt;/strong&gt;, by making the most awesome evil plan ever to destroy the &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/The_Powerpuff_Girls"&gt;Powerpuff Girls&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yup you can do that, and it's freaking awesome, but also don't, like just don't. And moreover, you already know what a programming language is, else what are you doing here, huh, huh, no tell me NO.&lt;/p&gt;

&lt;p&gt;Btw fun fact, programming languages don't have a notion of Time, Emotion, etc. So the code you write in Java, or Python or something else may feel like it has something to do the English language, but actually has no connection at all. They just share the same character set and that was also just because where these was initially developed.&lt;/p&gt;

&lt;p&gt;Programming languages provide support for arbitrary text. For example, say a document is written in Spanish, and contains a paragraph written in English. Now if a given individual, who has no clue what English looks like, reads that text, to the reader the english bits mean nothing and they will simply just ignore them and move to the bits they can read.&lt;/p&gt;

&lt;p&gt;Now most human languages don't define rules of intermixing of languages, since this is not something they expect the user to be doing and even when it's done, it relies on the consumers understanding of various languages. &lt;/p&gt;

&lt;p&gt;But we frequently use arbitrary text while programming and that raises a problem, how do we know which bits are part of the language and which bits are just strings(arbitrary text to be ignored). And to avoid this confusion, each languages defines it's own special way to make something a string.&lt;/p&gt;

&lt;p&gt;Let's look at the Javascript code below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// prints 'hello world' on the console screen&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Javascript, everything inside double quotes is treated as a string, so apart from the &lt;em&gt;Hello World&lt;/em&gt;, everything is part of the language, even the double quotes, YES the double quotes is not part of the arbitrary text, but part of the Javascript language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Components of Programming Language
&lt;/h2&gt;

&lt;p&gt;In general the notion of letters doesn't exist in programming languages. If the  character set used (&lt;code&gt;ASCII&lt;/code&gt;, &lt;code&gt;UTF-*&lt;/code&gt;, etc) supports the symbol, then it's valid and acceptable.&lt;/p&gt;

&lt;p&gt;Most programming languages start from keywords, which is an ordered collection of symbols with a specific meaning(a.k.a word in human language). These are finite in number and don't change for a specific version of the language. Keywords are things like &lt;code&gt;if, else, for, while, let, const, etc&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The notion of grammar is mostly similar to any other language, defining rules of keyword usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  So can you code in any Language ??
&lt;/h2&gt;

&lt;p&gt;Most definitely yes. At the end of the day, computers only understand assembly language, which are direct instructions your CPU understands. The only problem with writing Assembly directly is, it's not a great experience and an overkill for almost 99.99% cases, but hey if you think you like it, write Assembly, and make those &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/The_Powerpuff_Girls"&gt;Powerpuff Girls&lt;/a&gt;&lt;/strong&gt; suffer (EVIL LAUGH).&lt;/p&gt;

&lt;p&gt;But wait, how does Python work then. Well you already know compiler and interpreter right, yeah that's it. Python uses a compiler to convert your code to binary and that shit feels like magic.&lt;/p&gt;

&lt;p&gt;But also, did you know, your could program with spaces and tabs, or even better with emoji.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Whitespace_%28programming_language%29"&gt;Whitespace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.emojicode.org/"&gt;Emoji Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>languages</category>
      <category>computerscience</category>
      <category>funread</category>
    </item>
    <item>
      <title>HELLO ERRORS MY OLD FRIEND</title>
      <dc:creator>Vipul</dc:creator>
      <pubDate>Thu, 22 Oct 2020 05:00:51 +0000</pubDate>
      <link>https://dev.to/vipulbhj/hello-errors-my-old-friend-58jb</link>
      <guid>https://dev.to/vipulbhj/hello-errors-my-old-friend-58jb</guid>
      <description>&lt;p&gt;Errors get a bad rep in the newborn &lt;a href="https://github.com/shrutikapoor08/devjoke"&gt;#devjokes&lt;/a&gt; lingo, which is just a few years old at this point. Btw did you notice when words like programming, algorithm, software, and AI became part of the everyday expression 🤭🤭, extra points to the person who can tell the catalyst of this reaction 😛😛?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Anyways, my point is errors are not bad at all.&lt;/strong&gt;&lt;br&gt;
I would even go as far as saying errors are great, they are like that friend who tells you his honest opinion and notifies you when you do something wrong.&lt;/p&gt;

&lt;p&gt;Enough babbling, let's look at an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;TASKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 1&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="s2"&gt;Task 2&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="s2"&gt;Task 3&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="s2"&gt;Task 4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getNextTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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 created a function that returns the next task from the &lt;code&gt;TASKS&lt;/code&gt; Array, using &lt;code&gt;currentTask&lt;/code&gt; as its input. We can try the function and it works, &lt;em&gt;until&lt;/em&gt; it doesn't.&lt;/p&gt;

&lt;p&gt;If we look closely, we will observe that we left a &lt;code&gt;segmentation fault&lt;/code&gt; error in our code("segmentation fault" is just an OG way of the compiler telling us, it can't find the thing we are looking for as the index is incorrect).&lt;/p&gt;

&lt;p&gt;And since we have found out the problem, it's an easy fix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;TASKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 1&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="s2"&gt;Task 2&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="s2"&gt;Task 3&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="s2"&gt;Task 4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getNextTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&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;currentIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastIdx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;Looking so much better already :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing Uncertainty 😈
&lt;/h2&gt;

&lt;p&gt;Happy with the implementation we deploy it to production. Few days in, you suddenly observe that the function seems to have some error and the code is failing in production. &lt;/p&gt;

&lt;p&gt;Our initial investigation tells us the function keeps returning the first element of the array. &lt;strong&gt;Can you guess what might be causing the issue ??&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Congratulations&lt;/em&gt; to everyone, you all win the explanation, ready here goes 🥁 🥁 🥁&lt;/p&gt;

&lt;h2&gt;
  
  
  The curious case of the missing item
&lt;/h2&gt;

&lt;p&gt;Our investigation has finished, the errors seem to be caused when &lt;code&gt;currentIdx&lt;/code&gt; is &lt;code&gt;-1&lt;/code&gt;, which happens when the item is not found in the array.&lt;/p&gt;

&lt;p&gt;Now that we know the issue, an obvious solution seems like handling this cause.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;TASKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 1&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="s2"&gt;Task 2&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="s2"&gt;Task 3&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="s2"&gt;Task 4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getNextTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&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;currentIdx&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&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;currentIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastIdx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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 fixed the issue and our function seems to work fine now.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Considering the above function as a reference we fixed the issue by adding a defense condition which work. But the function &lt;code&gt;getNextTask&lt;/code&gt; was designed to return the next task which we accidentally changed to &lt;code&gt;validateTaskAndReturnNextTask&lt;/code&gt;, this is an issue but not a deal-breaker. The bigger problem here is, we didn't fix the problem, we just patched the inception point of the error, which can be lead to more unexpected issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error Friendly Functions
&lt;/h2&gt;

&lt;p&gt;We need to rethink how we write functions, make them more robust so that they make our software better.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;TASKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 1&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="s2"&gt;Task 2&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="s2"&gt;Task 3&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="s2"&gt;Task 4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getNextTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&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;currentIdx&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Invalid task &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;currentTask&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastIdx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;When you look at the code above not a lot has changed, we updated the defense condition from a return to an error. &lt;br&gt;
You might say I just increased your work and now you need to write &lt;code&gt;try-catch&lt;/code&gt; everywhere, to which I will say "No good sir you don't".&lt;/p&gt;
&lt;h2&gt;
  
  
  Don't handle the error
&lt;/h2&gt;

&lt;p&gt;Yes, I am serious "don't handle errors". Yes I know, that would mean your application will crash, but that's exactly what we want. &lt;br&gt;
Consider our &lt;code&gt;getNextTask&lt;/code&gt; function. In an ideal world, we will never get the case where &lt;code&gt;currentTask&lt;/code&gt; is not found in the &lt;code&gt;TASKS&lt;/code&gt; array. And since our function works on that assumption, it should shout on us when its conditions are not met and hence we throw an error. &lt;br&gt;
With this idea of letting our applications crash, we made an unexpected superhero friend &lt;a href="https://en.wikipedia.org/wiki/Stack_trace"&gt;The Stack Trace&lt;/a&gt;. &lt;br&gt;
Stack Traces are great, they give you an exact history of what happened and how it happened to maintain both state and order of execution of operations, which is extremely helpful in finding the actual error, which in our case would be the statement that makes the &lt;code&gt;currentTask&lt;/code&gt; an invalid entry in &lt;code&gt;TASKS&lt;/code&gt; array so that we can solve the actual problem and not just patch the problem inception point 🚀🚀🚀🚀.&lt;/p&gt;

&lt;p&gt;But leaving unhandled exceptions in our code is a terrible idea, especially when deploying to production. So, to deal with that we create a helper.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;reportError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;PRODUCTION&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="c1"&gt;// Call to your logging service or bug tracker&lt;/span&gt;
    &lt;span class="c1"&gt;// MyBugService.error(message)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Task 1&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="s2"&gt;Task 2&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="s2"&gt;Task 3&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="s2"&gt;Task 4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;getNextTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentTask&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;currentIdx&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;reportError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Invalid task &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;currentTask&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastIdx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;TASKS&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentIdx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;And with that, you just created a better critic for your code which can help you catch hidden bugs in the development phase and will help your make more robust software from the get-go.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>react</category>
    </item>
    <item>
      <title>Removing duplicate objects from an Array</title>
      <dc:creator>Vipul</dc:creator>
      <pubDate>Wed, 30 Sep 2020 06:34:30 +0000</pubDate>
      <link>https://dev.to/vipulbhj/removing-duplicate-objects-from-an-array-4iak</link>
      <guid>https://dev.to/vipulbhj/removing-duplicate-objects-from-an-array-4iak</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WioNJVL_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7xychv48u44czeq5qyp9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WioNJVL_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7xychv48u44czeq5qyp9.png" alt="Algorithm"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today I came across this &lt;a href="https://bitsofco.de/removing-duplicate-objects-from-an-array-is-hard/"&gt;Article&lt;/a&gt; which talks about, how it is difficult(computationally) to remove duplicate objects from array. The algorithm used is standard and still not very performant on scale, so I thought "can we do better, can we come up with a faster algorithm".&lt;/p&gt;

&lt;p&gt;Before our further discussion, let me summarize the original article real quick. Looking at the code sample below, what do you think will be the output of the last line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-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;people&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vipul&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vipul&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&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;people&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The answer is &lt;code&gt;false&lt;/code&gt;, because objects in JavaScript are reference types, meaning when you compare two objects instead of comparing there keys, there references are compared. Since we are creating new objects inline, we get new references each time, and thus the answer is &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introducing Symbols
&lt;/h3&gt;

&lt;p&gt;ES6 added bunch of new features to the JavaScript language which gave us some new cool features to play with. One of those is &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol"&gt;Symbols&lt;/a&gt;, which are really cool and can help us solve out problem better.&lt;/p&gt;

&lt;h3&gt;
  
  
  Our Faster Algorithm
&lt;/h3&gt;



&lt;div class="highlight js-code-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;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Doe&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="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;example&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;example&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="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Doe&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;removeDuplicates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;symbolValues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;keyStr&lt;/span&gt; &lt;span class="o"&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;firstName&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;lastName&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;symbolValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;keyStr&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="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;symbolValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;symbolValue&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;symbolValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;symbolValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;p&gt;In out algorithm, we are using two core features of Symbols&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Symbol.for(key)&lt;/code&gt; returns the same value, for the same key throughout the program.&lt;/li&gt;
&lt;li&gt;Symbols can be compared with other Symbols.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First we iterate over the array and create equivalent Symbol values using &lt;code&gt;Symbol.for&lt;/code&gt; where the key is a combination of the keys of the object. Then we simply just filter the original array based on conditions of not finding any existing Symbol with the same values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benchmarks
&lt;/h3&gt;

&lt;p&gt;I did some tests, just for fun and turns out this solution is pretty scale able too. Here are some of the results&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For 100 elements or so, it takes about 5.5ms where as the approach used in the original article takes 2.2ms.&lt;/li&gt;
&lt;li&gt;For 500 elements or so, it takes 5.7ms whereas the other algorithm takes 11.7ms.&lt;/li&gt;
&lt;li&gt;For 1500 elements or so, it takes 3.886ms whereas the other algorithm takes 32.82ms.&lt;/li&gt;
&lt;li&gt;For 8000 elements or so, it takes 5.57ms whereas the other algorithm takes 60.71ms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And after that I was obviously bored, so If someone finds this useful and does some testing on a bigger and may be more real world data, I would love to know stats.&lt;/p&gt;

&lt;p&gt;If you want to talk more about the implementation or anything else, you can find me on Instagram or Twitter as &lt;a class="mentioned-user" href="https://dev.to/vipulbhj"&gt;@vipulbhj&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;Thank you so much for reading, don’t forget to share If you find the information useful.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>react</category>
      <category>performance</category>
    </item>
    <item>
      <title>Offline-first Apps, huh ?</title>
      <dc:creator>Vipul</dc:creator>
      <pubDate>Fri, 21 Aug 2020 06:52:57 +0000</pubDate>
      <link>https://dev.to/vipulbhj/offline-first-apps-huh-1m9n</link>
      <guid>https://dev.to/vipulbhj/offline-first-apps-huh-1m9n</guid>
      <description>&lt;p&gt;Chances are, when I say the term "offline-first" apps, you will go like "huh🤔 what's that". And If you just did that, let me introduce you to this exciting new idea of building apps, that work for everyone(doesn't matter if you have a internet connectivity or not).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;But Wait a minute, all apps on my phone work alright with or without internet. What's new in this ?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Well, that's a correct observation, and this is often the case that trips people while thinking about offline-first apps. &lt;em&gt;How are they any different from apps like calculator, notes, etc&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Subtle, but Critical difference
&lt;/h2&gt;

&lt;p&gt;Apps like calculator, notes, etc are what is knows as offline apps, because of the property of having no dependence on your internet connectivity. But with offline-first apps, users need to connect to internet every once in a while for the app to function correctly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Sounds, weird🙄&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let look at an example, Imagine we have an app like Netflix, which is primarily a streaming platform, i.e users need internet connectivity to be able to use there platform. Now, say that they want to expand into countries, where either internet is not available at all, or the internet speeds are just too slow, or the prices are just to high. So to take there business to a place where users don't have consistent internet connectivity, they would have to change there app architecture to support working without internet.&lt;/p&gt;

&lt;p&gt;This theoretically can be achieved by an offline-first app. They can keep, a selection of say like 10 movies within the app and when the user is done watching those, then they can connect to the internet and can get more videos.&lt;/p&gt;

&lt;p&gt;Now the example above might not make complete sense in terms, like this is exactly similar to downloading movies. But for some companies this makes more sense. Take for example &lt;a href="https://www.ulesson.com"&gt;uLesson&lt;/a&gt;, which is an &lt;a href="https://www.talentlms.com/elearning/what-is-elearning"&gt;eLearning&lt;/a&gt; company, operating in Western Africa Regions, they ship data dongles to there customers, which has the learning material. Students can learn, give tests, take exams, etc. After a set amount of time, the user has to connect to the internet briefly to sync there progress, get new content, etc. For them, the offline-first app model makes sense, since without it they can't reach there customers. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>android</category>
      <category>ios</category>
    </item>
  </channel>
</rss>
