<?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: Shubham Kumar</title>
    <description>The latest articles on DEV Community by Shubham Kumar (@shubhamkumar10).</description>
    <link>https://dev.to/shubhamkumar10</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%2F493403%2Fd97cc3e0-0dfd-4789-9e16-0051b80b07df.jpeg</url>
      <title>DEV Community: Shubham Kumar</title>
      <link>https://dev.to/shubhamkumar10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubhamkumar10"/>
    <language>en</language>
    <item>
      <title>Open Source 101: Getting Ready for Hacktoberfest 2023</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sat, 30 Sep 2023 15:03:11 +0000</pubDate>
      <link>https://dev.to/shubhamkumar10/demystifying-open-source-what-it-truly-means-3h48</link>
      <guid>https://dev.to/shubhamkumar10/demystifying-open-source-what-it-truly-means-3h48</guid>
      <description>&lt;p&gt;With Hacktoberfest 2023 kicking off tomorrow, developers are gearing up to contribute to open-source projects. But have you ever stopped and wondered: What does "open source" truly mean?&lt;/p&gt;

&lt;p&gt;When I kicked off my coding journey, "open source" sounded like just another buzzword for sharing code. But as I read more about it, I realized its significance came from the ideas of community, collaboration, and freedom. This philosophy is defined by the Open Source Initiative (or OSI, to keep it short) which sets clear standards for what genuinely counts as "open source."&lt;/p&gt;

&lt;p&gt;The OSI presents these 10 points that software must check off to wear the open-source badge proudly. While I've listed them below for a quick glance, dive deeper into &lt;a href="https://opensource.org/osd/"&gt;The Open Source Definition&lt;/a&gt; if you're curious.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Free Redistribution
&lt;/h4&gt;

&lt;p&gt;You can share the software freely, sell it or give it away. No one should charge you just for the right to share it.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Source Code
&lt;/h4&gt;

&lt;p&gt;The software should come with its recipe (code). If it doesn't, there should be an easy way to get it, preferably for free.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Derived Works
&lt;/h4&gt;

&lt;p&gt;You can make changes or improvements to the software, and you should be able to share those too under the same rules/license.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Integrity of The Author’s Source Code
&lt;/h4&gt;

&lt;p&gt;If you change the original software, you might have to mark your changes or give it a new version number. It’s a way to respect the original creator's work.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. No Discrimination Against Persons or Groups
&lt;/h4&gt;

&lt;p&gt;The software is for everyone, no matter who they are.&lt;/p&gt;

&lt;h4&gt;
  
  
  6. No Discrimination Against Fields of Endeavor
&lt;/h4&gt;

&lt;p&gt;Whether you're using the software for work, play, research, or anything else, you're good to go.&lt;/p&gt;

&lt;h4&gt;
  
  
  7. Distribution of License
&lt;/h4&gt;

&lt;p&gt;When you share the software, the rules should go with it. Nobody should have to get a new license to use or share it.&lt;/p&gt;

&lt;h4&gt;
  
  
  8. License Must Not Be Specific to a Product
&lt;/h4&gt;

&lt;p&gt;The rules should be the same, whether the software is part of a package or stands alone.&lt;/p&gt;

&lt;h4&gt;
  
  
  9. License Must Not Restrict Other Software
&lt;/h4&gt;

&lt;p&gt;If you want to bundle this software with another, the license shouldn’t have issues or conditions about the other software.&lt;/p&gt;

&lt;h4&gt;
  
  
  10. License Must Be Technology-Neutral
&lt;/h4&gt;

&lt;p&gt;No matter what technology you're using (old, new, or futuristic), the software rules shouldn't favor one over another.&lt;/p&gt;

&lt;p&gt;So as you can see, the spirit of open source goes beyond software development. Think about it like sharing a recipe or jamming together in a garage band. It’s about everyone chipping in, whether it’s in projects, art, or even that side-project app, hence upholding the principles of redistribution, collaboration, and non-discrimination&lt;/p&gt;

&lt;p&gt;So, as you gear up for Hacktoberfest 2023, remember that each of your contributions is more than just a pull request — it's a step toward a vision of shared knowledge and innovation. Each bug report, feature enhancement, or documentation update carries the weight of this philosophy.&lt;/p&gt;

&lt;p&gt;Every single contribution, however small, will be impactful, one way or the other. This Hacktoberfest, as you contribute, know that you're not only adding to a project, but you're also embracing and promoting a philosophy that believes in the limitless potential of collaborative innovation.&lt;/p&gt;

&lt;p&gt;Happy Hacking !! &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>hacktoberfest23</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Converting date to relative time format in TypeScript</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sat, 16 Oct 2021 05:31:02 +0000</pubDate>
      <link>https://dev.to/shubhamkumar10/converting-date-to-relative-time-format-in-typescript-4iap</link>
      <guid>https://dev.to/shubhamkumar10/converting-date-to-relative-time-format-in-typescript-4iap</guid>
      <description>&lt;p&gt;If you are developing an application which has to display timestamp for posts/comments/notifications, you might come across this requirement where you have to format time in "from now" format. This is sometimes called timeago or relative time. &lt;/p&gt;

&lt;p&gt;You can either use a third party library like &lt;code&gt;dayjs&lt;/code&gt; or &lt;code&gt;momentjs&lt;/code&gt; for this, but for small projects, it is better to come up with your own utility function. In this tutorial we will write a util function which will accept a javascript Date object/ a string in ISO YYYY-MM-DD format/ or a date timestamp and convert it to relative time.&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;Eg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="mi"&gt;2020&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="nx"&gt;will&lt;/span&gt; &lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="nx"&gt;converted&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;an year ago&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript Date objects represent a single moment in time in a platform-independent format.It is is fundamentally specified as the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC.&lt;/p&gt;

&lt;p&gt;To convert the an input date to relative format, first we will define our time boundries in unit which we want to keep out least count. I am taking it in seconds, so the smallest difference that my method can return will be in seconds.&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;units&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;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;year&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;31536000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;month&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2592000&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;week&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;604800&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;day&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;86400&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hour&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;minute&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;seconds&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;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's write our util method now. We will input a date and find the difference between the input and the current moment. We can get the current date by using the Date API and calling the valueOf method on a new instance of Date object. The inout date is then subtracted from this value. Now we find the nearest unit to the input date. The method &lt;code&gt;calculateTimeDifference&lt;/code&gt; will take the milliseconds equivalent of Date for simplifying calculations. The nearest unit will be the one which has the quotient greater than 1 as we have defined our units in decending order. Once we get the quotient greater than 1, we will return that value with the respective unit. That will be used to contruct our string. You can check the snippets 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timeAgo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;Date&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;time&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;date&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&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;interval&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;unit&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculateTimeDifference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&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;suffix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;interval&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="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;s&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;interval&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;unit&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;suffix&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; ago`&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;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;calculateTimeDifference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&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="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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;units&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;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;seconds&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;interval&lt;/span&gt; &lt;span class="o"&gt;&amp;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;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;label&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;interval&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="na"&gt;unit&lt;/span&gt;&lt;span class="p"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Want to avoid git's annoying password prompts for every push? Configure SSH keys for Github</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sat, 02 Oct 2021 21:52:47 +0000</pubDate>
      <link>https://dev.to/shubhamkumar10/connect-to-github-with-ssh-2nk2</link>
      <guid>https://dev.to/shubhamkumar10/connect-to-github-with-ssh-2nk2</guid>
      <description>&lt;p&gt;Are you using HTTPS protocol to communicate between your local repos and Github? If yes, then just think–all of that time you spent entering your email address and password into the terminal every time you push a commit could have been spent coding. Guess what? you could have saved all that time by simply enabling SSH for your GitHub account. In this post, we will try to create an SSH key and add it to your Github account for "better" way of GitHub authentication.&lt;/p&gt;

&lt;h3&gt;
  
  
  What the heck is SSH anyway?
&lt;/h3&gt;

&lt;p&gt;SSH or Secure Socket Shell, is a network protocol that gives users a secure way to access a computer over an unsecured network. It allows the remotely login to a server using the connection established between your system's shell and the server's shell.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's Setup SSH Keys for your GitHub
&lt;/h3&gt;

&lt;p&gt;Navigate to your $HOME and change into &lt;code&gt;.ssh&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Inside the .ssh folder, you can generate a key using the following command:&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;ssh&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;keygen&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="nx"&gt;rsa&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="mi"&gt;4096&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;C&lt;/span&gt; &lt;span class="nx"&gt;your&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;com&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Replacing &lt;a href="mailto:your@email.com"&gt;your@email.com&lt;/a&gt; with your Github email. You can either keep the default filename &lt;code&gt;id_rsa&lt;/code&gt; or you can provide a custom file name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fditb5a9v96jctn7ghla6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fditb5a9v96jctn7ghla6.png" alt="Create ssh key"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Dissecting the command
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;ssh-keygen&lt;/code&gt; command creates RSA and DSA keys for public key authentication. It can also used to edit properties of existing keys, and to convert file formats. The command takes the following options&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ssh-keygen [-b bits] -t alog-type [-N [passphrase]] [-C comment] [-f output_keyfile]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When no options are specified,ssh-keygengenerates a 2048-bit RSA key pair and queries you for a key name and a passphrase to protect the private key. Public keys are created using the same base name as the private key, with an added .pub extension. The key location is displayed when key generation is complete.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbdk3q7gy2d2zk4m6brt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbdk3q7gy2d2zk4m6brt.png" alt="Keys Generated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Important Note:&lt;/em&gt;&lt;/strong&gt; You should never reveal your private key, and only use your public key for things like GitHub authentication. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Add your SSH key to ssh-agent
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ssh-agent&lt;/code&gt; is a program that starts when you log in to your system. It has all your private keys. To add the key to the &lt;code&gt;ssh-agent&lt;/code&gt; first we need to check if it is up and running.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// If you are on Mac/Linux:&lt;/span&gt;
&lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;$(ssh-agent -s)&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;Mac&lt;/span&gt; &lt;span class="nx"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;Linux&lt;/span&gt;

&lt;span class="c1"&gt;// If you are using Windows use:&lt;/span&gt;
&lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="s2"&gt;`ssh-agent -s`&lt;/span&gt;
&lt;span class="nx"&gt;ssh&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;agent&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt; 


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

&lt;/div&gt;

&lt;p&gt;If it is running you will see and output like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1h1fx15blucyiev750jf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1h1fx15blucyiev750jf.png" alt="SSH Agent"&gt;&lt;/a&gt;&lt;br&gt;
This tells us that the agent and up and running on the specified port number.&lt;/p&gt;

&lt;p&gt;You can then move ahead to add your key to the agent by using the following command:&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;ssh&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="sr"&gt;/.ssh/i&lt;/span&gt;&lt;span class="nx"&gt;d_rsa&lt;/span&gt;


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

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; If you used a custom file name for your keys, use that instead of &lt;code&gt;id_rsa&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we have to copy our public key:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// For linux/mac terminal use this:&lt;/span&gt;
&lt;span class="nx"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="sr"&gt;/.ssh/i&lt;/span&gt;&lt;span class="nx"&gt;d_rsa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pub&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nx"&gt;Linux&lt;/span&gt;

&lt;span class="c1"&gt;//For Windows use this:&lt;/span&gt;
&lt;span class="nx"&gt;clip&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="sr"&gt;/.ssh/i&lt;/span&gt;&lt;span class="nx"&gt;d_rsa&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pub&lt;/span&gt;


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

&lt;/div&gt;
&lt;h3&gt;
  
  
  Adding the public keys to your Github account:
&lt;/h3&gt;

&lt;p&gt;Go to your GitHub Account -&amp;gt; Settings -&amp;gt; SSH &amp;amp; GPG Keys.&lt;br&gt;
Click on add new key.&lt;br&gt;
Provide a title and paste in your public (id_rsa.pub) key:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgw88811bkm850anpp5co.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgw88811bkm850anpp5co.png" alt="Add key"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Testing Github Authentication
&lt;/h3&gt;

&lt;p&gt;Try this in the terminal&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;ssh&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="nx"&gt;git&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;github&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;com&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;If everything goes well, it will return:&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;Hi&lt;/span&gt; &lt;span class="nx"&gt;GithubName&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nx"&gt;You&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ve successfully authenticated, but GitHub does not provide shell access.


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; If you provided a passphrase during generating the key, you might have to use that as the password.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it you have successfully generated and configured an SSH key for your Github authentication. Happy Coding !!!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>github</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript Hoisting - behind the scenes</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sun, 26 Sep 2021 08:49:35 +0000</pubDate>
      <link>https://dev.to/shubhamkumar10/javascript-hoisting-behind-the-scenes-3849</link>
      <guid>https://dev.to/shubhamkumar10/javascript-hoisting-behind-the-scenes-3849</guid>
      <description>&lt;p&gt;In this post, I want to talk about how hoisting mechanism occurs in JavaScript.Before we dive in, let’s define what hoisting is.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Consider this code. Can you guess what is the output at line 3? :&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&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;a&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Many of us would expect it as  'undefined' , since the 'var a' statement comes after the a = 2 , and it would seem natural to assume that the variable is redefined, and thus assigned the default undefined . However, the output will be 2, due to hoisting.&lt;/p&gt;

&lt;p&gt;Hence,hositing allows us to use variables and call functions before even writing them in our code. Now this can be very confusing if you are coming from a different language like Java, where you need to define a variable first before using them.&lt;/p&gt;

&lt;p&gt;Let's consider another example, can you guess the output now?:&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;a&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output here will be &lt;code&gt;undefined&lt;/code&gt;. This goes on to show that the hoisting mechanism only moves the declaration. The assignments are left in place.&lt;/p&gt;

&lt;p&gt;But, this doesn't mean that during the compilation the JS Engine magically restructures your code to move all the declarations to the top of the scope. This behaviour is a result of the two phases that the program goes through - the compilation and the execution phase.&lt;/p&gt;

&lt;p&gt;During compilation phase, the code lexing and tokenizing phenomenon occurs. This simply means it will split our code into atomic tokens like &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;=&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;.(A token is a single element of a programming language).&lt;br&gt;
Note that no assignment or evaluation takes place during this phase.&lt;/p&gt;

&lt;p&gt;Each time the compiler encounters a declaration, it sends it to the scope manager to create the binding. For each declaration it allocates memory for that variable. &lt;code&gt;Just allocates memory, doesn’t modify the code to push the declaration up in the codebase.&lt;/code&gt; And as you know, in JS, allocating memory means setting the default value &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;After the compilation phase we go to execution phase, Where each time the engine encounters an assignment or an evaluation(e.g a function call/expression evaluation), it asks the scope for the binding. If not found in the current scope, it goes up into parent scopes until it finds it, and then the evaluation is done with that binding.&lt;/p&gt;

&lt;p&gt;Hence the second snippet is excecuted as follows:&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;Compilation&lt;/span&gt; &lt;span class="nx"&gt;phase&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="nx"&gt;pass&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// skipped as it is an evaluation&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="cm"&gt;/* 
This has two parts -
    1. A declaration part: var a 
    2. An assignment part: a = 2. 
The compiler only deals with the declaration part, 
and allocates memory for variable 'a'. 
The assignment will happen in excecution phase.
*/&lt;/span&gt;

&lt;span class="nx"&gt;Execution&lt;/span&gt; &lt;span class="nx"&gt;phase&lt;/span&gt; &lt;span class="nx"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="nx"&gt;pass&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="cm"&gt;/*
console.log() function called with a. 
The engine looks for the variable 'a' in the scope, and finds it,
for now has the value undefined, so prints it.
*/&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
&lt;span class="cm"&gt;/* 
The engine executes the assignment operation.
Looks for the variable 'a' in the scope chain and finds it.
Assign 2 to it.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This also happens with function declarations. Let's look at this 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="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;a&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Compilation Phase:&lt;/span&gt;
&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// As this is evaluation, this line is skipped&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;a&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="cm"&gt;/* 
The complier sees a declaration with identifier foo, hence memory is allocated to it
As it is a function and a new scope is also created.
It then again encounters a declaration for an indentifier a, so it allocates it to the memory.
*/&lt;/span&gt;

&lt;span class="c1"&gt;// Excecution Phase:&lt;/span&gt;
&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="cm"&gt;/* 
The engine looks for the identifier foo in the
scope chain. 
It finds and pull out the value that foo is referencing to- the statements inside it.
() executes the function,and the excecution moves inside foo function
It encounters a call to console.log() with argument a, which at this time is 'undefined'.
It prints undefined.
Execution moves to next line and encouters an assignment.
It looks for identifier a in the function scope and assigns value 2 to it.
The execution moves outside to global scope.
There are no more execution statements, so the program stops.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should also note that, Function declarations are hoisted, as we just saw, but function expressions are not. The reason being, assignment is done during execution phase.&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="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// not ReferenceError, but TypeError!&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Inside bar&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;Here, the variable identifier foo is hoisted and attached to the global scope, so foo() doesn’t fail as a ReferenceError . &lt;br&gt;
But foo has no value yet, so foo() is attempting to invoke the undefined value, which is a illegal operation. Hence it throws &lt;code&gt;TypeError&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; Both function declarations and variable declarations are hoisted, but as function declarations take precedence to variable declarations, functions are hoisted first and then variables.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This can be illustrated by below snippet:&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="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output is: 1&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hositing with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Variables declared with let and const are also hoisted, but unlike for var the variables are not initialized with a default value of undefined. Until the line in which they are initialized is executed, any code that accesses these variables will throw an exception.&lt;/p&gt;

&lt;p&gt;Variables declared with the keyword &lt;code&gt;let&lt;/code&gt; are block scoped and not function scoped. It just means that the variable’s scope is bound to the block in which it is declared and hence during compile time, memory is allocated for the variable declared with &lt;code&gt;let&lt;/code&gt;, but it is initialized to a value only when a parser evaluates it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt;  Unlike &lt;code&gt;var&lt;/code&gt;, accessing the variable declared as &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; before the initialization results in a &lt;code&gt;ReferenceError&lt;/code&gt;. For &lt;code&gt;var&lt;/code&gt; variables, they return a value of &lt;code&gt;undefined&lt;/code&gt; if they are accessed before they are declared.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hence, the below code snippet will return  &lt;code&gt;ReferenceError&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="c1"&gt;// Output: ReferenceError: a is not defined ...&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Output: ReferenceError: b is not defined ...&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To conclude, we can say that a JS program is parsed and executed in two passes, because of which concept of hoisting comes in to picture.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>hoisting</category>
      <category>programming</category>
    </item>
    <item>
      <title>Spring Projections</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Thu, 31 Dec 2020 14:04:58 +0000</pubDate>
      <link>https://dev.to/shubhamkumar10/spring-projections-46il</link>
      <guid>https://dev.to/shubhamkumar10/spring-projections-46il</guid>
      <description>&lt;p&gt;If you have used Spring Data JPA, you must be knowing that all the query methods of Repository classes will return an entity object. There may be cases where we do not want an entire entity from the query method. We may be interested only in few attributes of that entity or subset of that entity with some manipulation in it. In those cases, we can use "Projection" which projects us only the required data out of entire entity class.&lt;/p&gt;

&lt;p&gt;For example, lets say we have an entity class for Employee and we want to query our database based on this entity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@Table&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"employee"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Serializable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;GenerationType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;IDENTITY&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unique&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nullable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"firstName"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"lastName"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"designation"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;designation&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"salary"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@ManyToOne&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FetchType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;LAZY&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@JoinColumn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"departmentId"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updatable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;insertable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"departmentId"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="n"&gt;departmentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getLastName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getDesignation&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;designation&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="nf"&gt;getSalary&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Department&lt;/span&gt; &lt;span class="nf"&gt;getDepartment&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;department&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="nf"&gt;getDepartmentId&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;departmentId&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And instead of returning the whole entity we only need Id, First Name, Last Name, and Department Name of Employee.&lt;/p&gt;

&lt;p&gt;One way could be to use Data Transfer Object(DTO). The goal of a DTO class is to provide an efficient and strongly typed representation of the data returned by your query. To achieve that, a DTO class typically only defines a set of attributes, getter and setter methods for each of them, and a constructor that sets all attributes. But, this process can be simplifed by using spring projections.&lt;/p&gt;

&lt;p&gt;There are two ways in which we can use projections while using Spring Data JPA:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interface based&lt;/li&gt;
&lt;li&gt;Class based&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we will discuss Interface based projections.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interface based projections
&lt;/h2&gt;

&lt;p&gt;As the name suggests we will use an interface here. One can create an interface with only getter methods of properties we want from an entity class. This interface can then be the return type of query method we write in Spring Data JPA’s Repository interface.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1 -
&lt;/h3&gt;

&lt;p&gt;First we will create a interface with our desired properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;EmployeeProjectionOne&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getFirstName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getLastName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;DepartmentDetails&lt;/span&gt; &lt;span class="nf"&gt;getDepartment&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;DepartmentDetails&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can simply add this interface as a return type of query method in your Repository interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;EmployeeRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JpaRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EmployeeProjectionOne&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByFirstName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This type of projection is called "Closed Projection" and in this case, the getter methods of interface match exactly with the getter methods of Entity’s properties.Also note that, recursive projections only work if we traverse from the owning side to the inverse side(Employee to Department). Were we to do it the other way around, the nested projection would be set to null.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2 -
&lt;/h3&gt;

&lt;p&gt;One can also use so called Open Projection,where we create interface with getter methods of selective properties only with Spring Expression Language(SpEL) expression. These projections enable us to define interface methods with unmatched names and with return values computed at runtime.&lt;br&gt;
The SpEL expression will help us to define a new property from existing properties. Like in the above case if we want a property “fullName” then we can define it using SpEL expression and @Value  annotation. Likewise, we can also define a new property from nested property of entity class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;EmployeeProjectionTwo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Long&lt;/span&gt; &lt;span class="nf"&gt;getId&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"#{target.firstName} #{target.lastName}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getFullName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"#{target.department.name}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getDepartmentName&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in the respository we can simply use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;EmployeeRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JpaRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EmployeeProjectionTwo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByLastName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also define default methods in the projection-type interface to achieve the same thing, but it will just like writing boilerplate code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3
&lt;/h3&gt;

&lt;p&gt;Now, based on your needs you can define 'n' number of projections for your entity class, but defining separate repository interfaces or methods for these multiple return types is too much of work.&lt;br&gt;
To simplify this Spring data enables you to define what projection type to return from the query method at runtime dynamically. One will often come across this scenario while developing REST APIs so this is really a handy one. &lt;br&gt;
Include a new argument to query method of type Class and set it with your projection type as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Repository&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;EmployeeRepository&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JpaRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

 &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;findByFirstName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tClass&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then invoke the repository method from the service class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;EmployeeProjectionTwo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getAllByDynamicProjection&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;employeeRepository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findByFirstName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;EmployeeProjectionTwo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This type is called Dynamic projection because the attributes to return from the query method can be set during runtime.&lt;/p&gt;

&lt;p&gt;These are the 3 types of Interface based projections that can be used in Spring Data JPA- Open, Closed and dynamic types. I will also be covering the class-based projects in a separate article, so if you liked this article stay tuned for part 2.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Git Rebase vs. Git Merge</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Mon, 28 Dec 2020 19:44:11 +0000</pubDate>
      <link>https://dev.to/shubhamkumar10/git-rebase-vs-git-merge-4ikf</link>
      <guid>https://dev.to/shubhamkumar10/git-rebase-vs-git-merge-4ikf</guid>
      <description>&lt;p&gt;Every developer from the Git community has either faced the &lt;code&gt;git merge&lt;/code&gt; vs &lt;code&gt;git rebase&lt;/code&gt; dilemma or has been a part of this debate. Any git beginner is advised to stay away from &lt;code&gt;git rebase&lt;/code&gt; as it is a destructive process, but I find it as a great tool to simplify our development process within a team if used carefully. &lt;br&gt;
In this post, I’ll compare &lt;code&gt;git rebase&lt;/code&gt; with the related git merge command and identify all of the potential situations where we can use rebasing in the typical Git workflow.&lt;/p&gt;

&lt;p&gt;Rebase and merge, both solve the same problem - both these commands are designed to integrate changes from one branch into another branch. They differ in the manner in which they achieve this.&lt;/p&gt;

&lt;p&gt;To understand the working of these commands, let assume that you started working on a new project. You head to the repository, fork it, set up your local workspace, and start working in your feature branch. Meanwhile, a colleague of yours makes a commit in the master branch of the repository. And your history looks something like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft7w053ulihxde02pqfla.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft7w053ulihxde02pqfla.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get these new commits into your feature branch, you have two options: merging or rebasing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git Merge
&lt;/h2&gt;

&lt;p&gt;Suppose you use the following commands and merge your changes:&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;git&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;
&lt;span class="nx"&gt;git&lt;/span&gt; &lt;span class="nx"&gt;merge&lt;/span&gt; &lt;span class="nx"&gt;feature&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;branch&lt;/span&gt; &lt;span class="nx"&gt;master&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This creates a new “merge commit” in the feature branch that ties together the histories of both branches, giving you a branch structure that looks like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Far5hsfa42wywbe5ddina.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Far5hsfa42wywbe5ddina.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This will also create a new merge commit in your log. Merging is nice because it’s a non-destructive operation. The existing branches are not changed in any way and the history of your branch is preserved.&lt;/p&gt;

&lt;p&gt;But, every time you sync your fork with the upstream you will be creating an extra merge commit to incorporate the changes. If your master is very active, your feature branch history will be loaded with the merge commits. This might give hard time to someone who goes through your branch/project history.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git Rebase
&lt;/h2&gt;

&lt;p&gt;As an alternative to &lt;code&gt;git merge&lt;/code&gt; you can do:&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;git&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;
&lt;span class="nx"&gt;git&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="nx"&gt;feature&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;branch&lt;/span&gt;
&lt;span class="nx"&gt;git&lt;/span&gt; &lt;span class="nx"&gt;rebase&lt;/span&gt; &lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;master&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This moves the entire feature branch to begin on the tip of the master branch by combining all the changes of the master branch into a single “patch” and then integrate it onto the feature branch. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the master branch. In the process, unwanted history is eliminated. Hence, your tree will look like something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhkg8e4ipcrl70z3etrvw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhkg8e4ipcrl70z3etrvw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this is the major benefit of rebasing - A clear and linear project history. It has eliminated the unnecessary merge commits required by git merge. And now that you have a linear project history, you can follow the tip of the feature all the way to the beginning of the project without any forks. This makes it easier to navigate your project with commands like git log or git bisect. The only downside of using this is that now we will never know when was the upstream changes incorporated in our feature branch.&lt;/p&gt;

&lt;p&gt;But what if I told you that you can completely alter the project history and make it look as you want it? Powerful isn't it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Interactive rebasing - rewrite history your way
&lt;/h2&gt;

&lt;p&gt;Interactive rebasing will give you complete command over your commit history as you can alter commits as they are moved to the new branch. Usually, we use it to clean up a messy history before merging a feature branch into master. For example combining previous "work in progress" commits, which have been completed.&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;git&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="nx"&gt;feature&lt;/span&gt; &lt;span class="nx"&gt;git&lt;/span&gt; &lt;span class="nx"&gt;rebase&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="nx"&gt;master&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This will open the editor with the list of your commits, and you can choose to pick, squash the commits to make the history look as you want.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use rebase???
&lt;/h2&gt;

&lt;p&gt;If you are working on your branch individually, rebasing makes a lot of sense. You can use rebasing to streamline a complex history, you can also change the commit history by interactive rebase. You can remove undesired commits, squash two or more commits into one, or edit the commit messages before creating your Pull Request. This will also simplify the work of the reviewer as he/she will have to go through a clean commit history.&lt;/p&gt;

&lt;p&gt;If you want to see the history completely the same as it happened and avoid the risk of re-writing public commits, you should use git merge.&lt;/p&gt;

&lt;p&gt;The most important thing to learn about rebasing is when not to do it. The golden rule of git rebase is to never use it on public branches, the common branch which all developers will use as it will create inconsistent repositories which can result in your fork diverging from the main repository.&lt;/p&gt;

&lt;p&gt;For example, let's say we rebased our master onto our feature branch using &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;git&lt;/span&gt; &lt;span class="nx"&gt;checkout&lt;/span&gt; &lt;span class="nx"&gt;master&lt;/span&gt;
&lt;span class="nx"&gt;git&lt;/span&gt; &lt;span class="nx"&gt;rebase&lt;/span&gt; &lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;feature&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;branch&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This action will move all the commits in your feature branch first, then the commits of the master will be put on its tip. The problem is that this only happened in your repository. All of the other developers are still working with the original master. Since rebasing results in brand new commits, Git will think that your master branch’s history has diverged from everybody else’s.&lt;/p&gt;

&lt;p&gt;The only fix now is to merge the two master branches together, but it will result in an extra commit with both sets of changes, the original master commits, and your rebased feature branch too...&lt;/p&gt;

&lt;p&gt;So, before you run git rebase, always ask yourself, “Is anyone else looking at this branch?” If the answer is yes, bad news, stick to the normal git merge process, otherwise, feel free to rewrite history as you like.&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>5 things I learnt in 2020 for productive git collaboration.</title>
      <dc:creator>Shubham Kumar</dc:creator>
      <pubDate>Sat, 26 Dec 2020 19:32:28 +0000</pubDate>
      <link>https://dev.to/shubhamkumar10/5-things-i-learnt-in-2020-for-productive-git-collaboration-4253</link>
      <guid>https://dev.to/shubhamkumar10/5-things-i-learnt-in-2020-for-productive-git-collaboration-4253</guid>
      <description>&lt;p&gt;Until last year, I used git quite recklessly, writing bad commit messages and never taking a pause to think about the code and the manner in which it was being pushed. Things changed as I collaborated more and more with other developers for projects and after working with agile teams for over a year now, here are 5 things I have learned form my experience:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Keep your PR's "Atomic"
&lt;/h2&gt;

&lt;p&gt;Let’s say you are working on designing the layout of your web app, and you were tasked with a simple bug fix in the same component. Though it is tempting to commit both the changes and to raise a PR together, our layout changes and the bugfix should go as separate PRs. By doing that you are creating an organized log of commits, which makes it easy for other developers to read and maintain the code base. It will also help your code reviewers, who can glance through the commit history and find what they are looking at easily. &lt;/p&gt;

&lt;p&gt;You should also be aware of what you are committing in your repository, and not just do &lt;code&gt;git add .&lt;/code&gt; blindly every time and put it up in your PR.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Use branch workflows
&lt;/h2&gt;

&lt;p&gt;Try to create and follow a git workflow within the team, the main advantage of doing so is that you will be keeping your development process organized and improving the overall code quality. A good git workflow ensures a clean state of branches at all times.&lt;/p&gt;

&lt;p&gt;In my current project, we follow the feature-based git workflow and have 4 types of branch, each for a specific purpose:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;master: "The golden copy" of the source code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;develop: The stable branch from where new branches are cut. How is this different from master? The master branch stores the official release history, and the develop branch serves as an integration branch for the features.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;feature:  When we start working on a new feature, we cut off a feature branch for it from develop branch. For example, if we start working on a login page for a web app, we can create a branch as &lt;code&gt;feature-login&lt;/code&gt;.&lt;br&gt;
This helps in tracking the progress of user stories/defects in an agile development environment and also keeps your features separated in commit logs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;release: For every release, a new branch is cut and maintained. Why? Because the new additional features of v2.0, should not be present in v1.0. It is cut from develop branch when the release is production ready.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Mark incomplete commits with "WIP"
&lt;/h2&gt;

&lt;p&gt;If you are not finished with the feature you have been working on by the end of the day, make a temporary commit with “WIP” appended to the message. This signifies that your commit is a “Work in progress”. When finished, one can squash all the commits in a single one and create the PR.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Give meaningful commit messages:
&lt;/h2&gt;

&lt;p&gt;When you are ready with your changes and ready to make a commit, do your best to make it a meaningful one by giving a proper message. Before crafting a commit message, take a minute and put yourself in the shoes of your fellow developers seeing this change for the first time. Would you understand what code has changed in this commit? Would you know why the code changed?&lt;br&gt;
Your commit message is supposed to convey these messages. I follow this pattern:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;[feature/defect id] - type: description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The general types of commits one can encounter are as follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;feat - a new feature&lt;/li&gt;
&lt;li&gt;fix - a bug fix&lt;/li&gt;
&lt;li&gt;docs - changes in documentation&lt;/li&gt;
&lt;li&gt;style - everything related to styling&lt;/li&gt;
&lt;li&gt;refactor - code changes that neither fixes a bug or adds a feature&lt;/li&gt;
&lt;li&gt;test - everything related to testing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Write unit tests
&lt;/h2&gt;

&lt;p&gt;One thing that I always ignored while learning programming was unit testing. If a developer performs proper unit testing, his defect density can drop significantly.&lt;br&gt;
Writing unit tests forces us to consider how well the production code is designed in order to make it suitable for running our tests. It will also help you to code from a different perspective, encouraging you to consider corner cases and error conditions in the implementation. Most importantly, it identifies defects at early stages and also helps in safely refactor your code later, since tests can be re-run quickly to validate that behavior has not changed.&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
