<?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>Forem: Hammad Malik</title>
    <description>The latest articles on Forem by Hammad Malik (@tomatohammado).</description>
    <link>https://forem.com/tomatohammado</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%2F56474%2F41e61b37-425c-4a35-a2f8-f66770a67c5f.jpeg</url>
      <title>Forem: Hammad Malik</title>
      <link>https://forem.com/tomatohammado</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tomatohammado"/>
    <language>en</language>
    <item>
      <title>Refactoring: Try-Catch for Variable Assignment in JS</title>
      <dc:creator>Hammad Malik</dc:creator>
      <pubDate>Tue, 05 May 2020 22:22:06 +0000</pubDate>
      <link>https://forem.com/tomatohammado/refactoring-try-catch-for-variable-assignment-in-js-2b99</link>
      <guid>https://forem.com/tomatohammado/refactoring-try-catch-for-variable-assignment-in-js-2b99</guid>
      <description>&lt;p&gt;I was refactoring an Express application when I came across a curious bit of code:&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;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&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;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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 believe this code is attempting to throw an error if the "name" property is missing in &lt;code&gt;req.body&lt;/code&gt; – if &lt;code&gt;req.body.name&lt;/code&gt; did not exist the assignment would cause an error and it would stop execution of the rest of the code.&lt;/p&gt;

&lt;p&gt;However, that's not what happens! &lt;code&gt;req.body.email&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt;, so it is perfectly possible to assign &lt;code&gt;undefined&lt;/code&gt; as the value for the &lt;code&gt;name&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;I think &lt;code&gt;hasOwnProperty&lt;/code&gt; is more reliable way to make sure &lt;code&gt;req.body&lt;/code&gt; container an &lt;code&gt;email&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&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;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&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;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;ReferenceError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;no name in request body.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&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;body&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;PS: For Express specifically, I'm interested in learning more about &lt;a href="https://stackoverflow.com/a/16819689/5578727"&gt;validating the req.body using JSON Schema or Joi&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>refactoring</category>
      <category>express</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Something I Learned Today: Using `git diff` and `git log -u` to see changes in a file between commit</title>
      <dc:creator>Hammad Malik</dc:creator>
      <pubDate>Mon, 20 Aug 2018 16:15:07 +0000</pubDate>
      <link>https://forem.com/tomatohammado/something-i-learned-today-using-git-diff-and-git-log--u-to-see-changes-in-a-file-between-commit-1lg4</link>
      <guid>https://forem.com/tomatohammado/something-i-learned-today-using-git-diff-and-git-log--u-to-see-changes-in-a-file-between-commit-1lg4</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;I have to make changes to a template file, but on my current branch I was working with the database. So I want to make a new branch just for the Template updates, but need to double check that I didn’t make a change to that file in the commit history of the separate branch&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/a/3338145"&gt;Stack Overflow: using &lt;code&gt;git diff&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/a/3338901"&gt;Stack Overflow: using &lt;code&gt;git log -u&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Findings
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;commit_0&amp;gt;..&amp;lt;commit_1&amp;gt;&lt;/code&gt; is shorthand for checking the differences between two commits, not to be confused with...&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;commit_0&amp;gt;...&amp;lt;commit_1&amp;gt;&lt;/code&gt; - with three dots - which also covers all the commits &lt;em&gt;between&lt;/em&gt; the two commits inclusively. Two dots simply compares one with another.&lt;/li&gt;
&lt;li&gt;And you can also exclude one of the commit references, and it will fall back as if &lt;code&gt;HEAD&lt;/code&gt; were used as the value instead. Oh, and you can just use &lt;code&gt;HEAD&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Further Research
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It appears you can do stuff like &lt;code&gt;HEAD^^&lt;/code&gt; and &lt;code&gt;HEAD~2&lt;/code&gt;, but I don’t know what that means yet 😬&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>learning</category>
      <category>git</category>
    </item>
  </channel>
</rss>
