<?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: Viral Patel</title>
    <description>The latest articles on Forem by Viral Patel (@viralpatelblog).</description>
    <link>https://forem.com/viralpatelblog</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%2F93663%2Fa1e3280b-4599-4e8b-a127-7c239e4a0a7f.jpeg</url>
      <title>Forem: Viral Patel</title>
      <link>https://forem.com/viralpatelblog</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/viralpatelblog"/>
    <language>en</language>
    <item>
      <title>Deliberate practice in software development</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Sat, 05 Sep 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/deliberate-practice-in-software-development-2jl</link>
      <guid>https://forem.com/viralpatelblog/deliberate-practice-in-software-development-2jl</guid>
      <description>&lt;h2&gt;
  
  
  What is Deliberate Practice (DP)?
&lt;/h2&gt;

&lt;p&gt;Deliberate practice is common phenomenon in sports, arts and other industries. Athletes have a dedicated time each and every single working day for deliberate practice. It is same with musicians, actors, painters, singers, dancers. Actors dedicate some time perfecting dialogue delivery by deliberately practicing it for hours and hours. Singers tries to perfect their pitch by intentionally focusing only on a particular pitch while practicing. Dancers and sportsman and sportswoman practices a single move over and over and over again until it becomes natural as part of their natural reflection.Deliberate practice is nothing but an intentional practice. A time slot dedicated to focus on only one core unit of your craft and perfecting it until you understand and have knowledge of all possible aspect of the component that you are trying learn and practice.&lt;/p&gt;

&lt;p&gt;Anders Ericsson, the psychology professor quoted above, coined the term &lt;em&gt;deliberate practice&lt;/em&gt; (DP) to describe this special type of work. If you read Malcom Gladwell’s &lt;a href="https://www.amazon.com/Outliers-Story-Success-Malcolm-Gladwell/dp/0316017922/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1262813920&amp;amp;sr=8-1"&gt;Outliers&lt;/a&gt; , you probably have an answer: &lt;strong&gt;the 10,000 hour rule.&lt;/strong&gt; This concept, which was first introduced in academic circles in the early 1970s, was popularized by Gladwell in his 2008 book.To summarize in Anders Ericsson’s words&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[A]ctivities designed, typically by a teacher, for the sole purpose of effectively improving specific aspects of an individual’s performance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is not Deliberate Practice?
&lt;/h2&gt;

&lt;p&gt;Reading a book, watching tutorial videos, going through online courses or reading blogs are not part of “Deliberate Practice”. These are the resources for knowledge so as a knowledge worker, you definitely should be doing combination of things mentioned in a previous line to gain new knowledge, a new perspective or to clear your existing false assumptions, but, they do not account towards your 10,000 Hour rule. However, coding exercises, preparing notes by hand, trying to solve the same problem in many different ways, or, trying to solve many different problems from the same concept, all of this can account for deliberately practiced hours since all of these activities engages your brain in many ways and doing these over and over again will help you grasp the acquired knowledge in a long-term memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Read this amazing article by Cal Newport &lt;a href="https://www.calnewport.com/blog/2010/01/06/the-grandmaster-in-the-corner-office-what-the-study-of-chess-experts-teaches-us-about-building-a-remarkable-life/"&gt;The Grandmaster in the Corner Office: What the Study of Chess Experts Teaches Us about Building a Remarkable Life&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>craftsmanship</category>
    </item>
    <item>
      <title>Git branch naming conventions</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Fri, 10 Apr 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/git-branch-naming-conventions-4aef</link>
      <guid>https://forem.com/viralpatelblog/git-branch-naming-conventions-4aef</guid>
      <description>&lt;p&gt;As you have spent a couple of years in software or web development and if Git has become part of your work routine, this small tip of the day is going to help you optimize your workflow and increase productivity. The benefit of adopting this habit is long term and will add a lot of value.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;There are only two hard things in Computer Science: cache invalidation and naming things.&lt;/em&gt;&lt;em&gt;— Phil Karlton&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As we have all struggled naming things whether it is variables, classes or even at times naming our projects is hard. I bet a lot of developers still struggle to name their git branches as well or do not put enough thought into naming them since they are not something that will come and bite us in the future but trust me even if they do not bite, it still leaves certain marks on our projects.&lt;/p&gt;

&lt;p&gt;I have a convention that I follow myself to make it easy to find branches later in the future and identifying intention behind creating this branch just by looking at their name.&lt;/p&gt;

&lt;p&gt;Feel free to modify this convention best to your workflow and the way your mental modal works and identifies words.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout -b YOUR_INITIAL/TYPE_OF_WORK/JIRATICKETNUMBER-BRANCH-NAME
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I know this might look a bit tedious to create branches with such long names but again the idea is to be able to interpret intention behind created branch just by looking at their name or you can even search with the pattern.&lt;/p&gt;

&lt;p&gt;For example, I name my branches as,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vp/feature/124-create-login-page
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This can help you a few months/years down the road to find the branches you’ve worked on easier either through Github UI shown in the screenshot,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IyXWZW0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnADxFk5S0QUGu1rVQFMiMQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IyXWZW0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AnADxFk5S0QUGu1rVQFMiMQ.png" alt="Github screenshot"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OR by the command line&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git branch --list | grep "vp/feature/"

git branch --list "vp/feature/*"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;To break it down the convention,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;YOUR_INITIAL/TYPE_OF_WORK/JIRATICKETNUMBER-BRANCH-NAME
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;YOUR_INITIAL: It can be anything you want, your first name, your GitHub username&lt;/p&gt;

&lt;p&gt;TYPE_OF_WORK: It can be of any category i.e. feature, fix, refactor, task, prototype, throwaway&lt;/p&gt;

&lt;p&gt;JIRATICKETNUMBER-BRANCH-NAME: You can have a JIRA ticket number or if you are not using a ticket-based system it can be MMYY timestamp format followed with the branch name.&lt;/p&gt;

&lt;p&gt;I’d love to know what do you guys use for naming your branches and how do you optimize your workflow.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: Cover Photo by Yancy Min on Unsplash.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>git</category>
      <category>webdev</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Tests are executable documentation</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Sat, 08 Feb 2020 00:00:00 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/tests-are-executable-documentation-3mfa</link>
      <guid>https://forem.com/viralpatelblog/tests-are-executable-documentation-3mfa</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;A well-written test case tells a clear story, communicating the intent of code. Learn how to write expressive tests and why duplication isn't always a bad thing in your test cases. - phrase from ThoughtBot blog about TDD.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Well written Tests&lt;/strong&gt; are always a source of truth for a living documentation for that perticular project. Other form of documentation such as git wikis, code comments, code commits, pull request descriptions, google doc, Slack comments can easily get out of the sync with the production code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Well written Tests&lt;/strong&gt; guides your example documentantion which can be used by the developers using your library. Developers of your library should never have to read your source code, code comments to figure out how to use your library. If you see yourself reading source code or code comments of other libraries you are using, it is an indication to you that the library does not have a well written test suite or example documentation to go through.&lt;/p&gt;

&lt;p&gt;You should be revealing your &lt;strong&gt;intents&lt;/strong&gt; through your tests. Developers using your library should never have to worry about how it is &lt;strong&gt;implemented&lt;/strong&gt;. They should be able to use your library by reading your tests and examples only. We need to seperate the intent from the implementation.&lt;/p&gt;

</description>
      <category>tdd</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Understanding MySQL Index Basics</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Wed, 11 Dec 2019 17:12:52 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/understanding-mysql-index-basics-ha7</link>
      <guid>https://forem.com/viralpatelblog/understanding-mysql-index-basics-ha7</guid>
      <description>&lt;p&gt;An index is an on-disk or in-memory structure associated with a table or view that speeds retrieval of rows from the table or view. An index contains keys built from one or more columns in the table or view. For on-disk indexes, these keys are stored in a structure (B-tree) that enabled the SQL server to find the row or rows associated with the key values quickly and efficiently.&lt;/p&gt;

&lt;p&gt;An index stores data logically organized as a table with rows on columns, and physically stored in a row-wise data format called row-store, or stored in a column-wise data format called column-store.&lt;/p&gt;

&lt;p&gt;The selection of the right indexes for a database and its workload is a complex balancing act between query speed and update cost. Narrow indexes, or indexes with few columns in the index key, require less disk space and maintenance overhead. Wide indexes, on the other hand, cover more queries. You may have to experiment with several different designs before finding the most efficient index. Indexes can be added, modified and dropped without affecting the database schema or application design. Therefore, you should not hesitate to experiment with different indexes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;row-store has been the traditional way to store relational table data.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The larger the table, the more these costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. This is much faster than reading every row sequentially.&lt;/p&gt;

&lt;h5&gt;
  
  
  Here is a sample command to add an index to the existing table
&lt;/h5&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;user_name_index&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;user&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;if using b-tree,&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;user_name_index&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;user&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;BTREE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This blog was originally published on my personal blog &lt;a href="https://viralpatel.blog"&gt;Viral Patel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>mysql</category>
      <category>sql</category>
      <category>developer</category>
    </item>
    <item>
      <title>Understanding React stateless components</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Wed, 11 Dec 2019 17:11:15 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/understanding-react-stateless-components-2b6f</link>
      <guid>https://forem.com/viralpatelblog/understanding-react-stateless-components-2b6f</guid>
      <description>&lt;p&gt;Since React v14, a simpler way was introduced to define stateless functional components. These components use plain Javascript functions. With React 16.6+, you can declare "pure" functional components via &lt;code&gt;React.memo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The simplest way to define a stateless component is to write a Pure Javascript function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;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's why React's stateless components are amazing,&lt;/p&gt;

&lt;h3&gt;
  
  
  Props are Read-Only
&lt;/h3&gt;

&lt;p&gt;Whether you declare a component as a function or a class, it must never modify its own props. Consider the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sum&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="nx"&gt;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here's an anti-pattern&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  No Class Needed
&lt;/h3&gt;

&lt;p&gt;Plain functions are generally preferable over ES6 classes and eliminating the class-related cruft like extends and the constructor in the example above a nice win.&lt;/p&gt;

&lt;h3&gt;
  
  
  No this keyword
&lt;/h3&gt;

&lt;p&gt;The entire component becomes easier to understand without the &lt;code&gt;this&lt;/code&gt; keyword. All the annoying and confusing quirks with Javascript's &lt;code&gt;this&lt;/code&gt; keyword can be avoided.&lt;/p&gt;

&lt;p&gt;Dumping classes eliminates the need for calling bind to pass the &lt;code&gt;this&lt;/code&gt; context around. Given how confusing Javascript's &lt;code&gt;this&lt;/code&gt; keyword is to many developers, avoiding it is a nice win.&lt;/p&gt;

&lt;h3&gt;
  
  
  Enforced Best Practices
&lt;/h3&gt;

&lt;p&gt;Stateless functional components are useful for dumb components. Presentational components focus on the UI rather than behavior so it's important to avoid using state in presentational components. Instead, the state should be managed by higher-level "container" components, or via state management libraries like Redux.&lt;/p&gt;

&lt;p&gt;Stateless components don't support state or lifecycle methods. This is a good thing. Why? Because it protects from laziness. Stateless functional components programmatically enforce keeping the component pure. You're forced to put state management where it belongs: in higher-level container components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Easy to Understand
&lt;/h3&gt;

&lt;p&gt;When you see a stateless functional component, you know it's simply a function that takes props and spits out HTML. It's a pure function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Easy to Test
&lt;/h3&gt;

&lt;p&gt;Since it is a pure function, your assertions are very straightforward: Given these values for props, I expect it to return this markup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;p&gt;Finally, stateless functional components offer improved performance as well. Since there are no state or lifecycle methods to worry about.&lt;/p&gt;

&lt;p&gt;To consider downsides of using React's stateless components, &lt;a href="https://medium.freecodecamp.org/7-reasons-to-outlaw-reacts-functional-components-ff5b5ae09b7c"&gt;visit this link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This blog was originally published on my personal blog &lt;a href="https://viralpatel.blog"&gt;Viral Patel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>functional</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Setup aliases and shortcuts for git commands</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Wed, 11 Dec 2019 17:06:55 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/setup-aliases-and-shortcuts-for-git-commands-3lh2</link>
      <guid>https://forem.com/viralpatelblog/setup-aliases-and-shortcuts-for-git-commands-3lh2</guid>
      <description>&lt;p&gt;Here is how you setup aliases and shortcuts for git commands. Edit &lt;code&gt;.gitconfig&lt;/code&gt; file in your &lt;code&gt;$HOME&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;alias&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="s"&gt;co = checkout&lt;/span&gt;
  &lt;span class="s"&gt;ci = commit&lt;/span&gt;
  &lt;span class="s"&gt;st = status&lt;/span&gt;
  &lt;span class="s"&gt;br = branch&lt;/span&gt;
  &lt;span class="s"&gt;hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short&lt;/span&gt;
  &lt;span class="s"&gt;type = cat-file -t&lt;/span&gt;
  &lt;span class="s"&gt;dump = cat-file -p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Refer to the branch you've checked out when you use &lt;code&gt;git push&lt;/code&gt; and/or &lt;code&gt;git pull&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="s"&gt;default = current&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here are some more goodies in your aliases list. Thank me later 😎&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;alias&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="c1"&gt;# Adds and commits files with a single command&lt;/span&gt;
  &lt;span class="s"&gt;add-commit = !git add -A &amp;amp;&amp;amp; git commit&lt;/span&gt;

  &lt;span class="s"&gt;# Delete all local branches but master and the current one, only if they are fully merged with master.&lt;/span&gt;
  &lt;span class="s"&gt;br-delete-useless = "!f(){\git branch | grep -v "master" | grep -v ^* | xargs git branch -d;\}; f"&lt;/span&gt;

  &lt;span class="s"&gt;# Delete all local branches but master and the current one.&lt;/span&gt;
  &lt;span class="s"&gt;br-delete-useless-force = "!f(){\git branch | grep -v "master" | grep -v ^* | xargs git branch -D;\}; f"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This blog was originally published on my personal blog &lt;a href="https://viralpatel.blog"&gt;Viral Patel&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>bash</category>
      <category>developer</category>
    </item>
    <item>
      <title>Why use React Hooks? Explained</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Wed, 11 Dec 2019 15:34:13 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/why-use-react-hooks-explained-1n6p</link>
      <guid>https://forem.com/viralpatelblog/why-use-react-hooks-explained-1n6p</guid>
      <description>&lt;h3&gt;
  
  
  Start with why?
&lt;/h3&gt;

&lt;p&gt;If you want to use Hooks in &lt;code&gt;React&lt;/code&gt; in your application. Make sure you update your react version to &lt;code&gt;&amp;gt;=16.8&lt;/code&gt; since it was introduced in &lt;code&gt;React v16.8&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before React Hooks was a feature in React the only way to use state in your component was to write your component as ES6 Class. Now, with React hooks you can use state and other React features without writing a class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=dpw9EHDh2bM"&gt;Click here to watch a youtube video from React Conf 2018&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Completely opt-in
&lt;/h4&gt;

&lt;p&gt;You can try Hooks in a few components without rewriting any existing code. But you don't have to learn or usee Hooks right now if you don't want to. It is 100% backwards-compatible.&lt;/p&gt;

&lt;h4&gt;
  
  
  Hooks don't replace your knowledge of React concepts
&lt;/h4&gt;

&lt;p&gt;Hooks only is an alternative way to use React concepts you already know: props, state, context, refs, and lifecycle but without classes. Hooks allow you to reuse stateful logic without changing your component hierarchy&lt;/p&gt;

&lt;h5&gt;
  
  
  How do lifecycle methods correspond to Hooks?
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Function components don't need a constructor. You can initialize the state in the &lt;code&gt;useState&lt;/code&gt; call. If a computing the initial state is expensive, you can pass a function to &lt;code&gt;useState&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instead of using &lt;code&gt;shouldComponentUpdate&lt;/code&gt; lifecycle, use &lt;code&gt;React.memo&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hook called &lt;code&gt;useEffect&lt;/code&gt; can express all combinations of all the standard lifecycles like &lt;code&gt;componentDidMount&lt;/code&gt;, &lt;code&gt;componentDidUpdate&lt;/code&gt; and &lt;code&gt;componentWillUnmount&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are no hooks yet for handling exceptions in your component.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Next steps
&lt;/h4&gt;

&lt;p&gt;Hope this article helps you understanding a motivation behind why Hooks were introduced but many details will be probably unclear. Click &lt;a href="https://reactjs.org/docs/hooks-overview.html"&gt;this link to deep dive into Hooks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This blog was originally published on my personal blog &lt;a href="https://viralpatel.blog"&gt;Viral Patel&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>functional</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Docker Cleanup Commands</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Wed, 11 Dec 2019 15:18:01 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/docker-cleanup-commands-2097</link>
      <guid>https://forem.com/viralpatelblog/docker-cleanup-commands-2097</guid>
      <description>&lt;p&gt;If you see &lt;strong&gt;Docker&lt;/strong&gt; claiming lot of space on your computer and if you want to reclaim all of your space back from Docker, use following Docker commands for cleanup. Make sure you are aware that this will delete images and containers from your machine, make sure you do not run this on production machines unless you know what you are doing  😎.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to remove docker’s unused data?
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker system prune —force
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To delete everything&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker system prune &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nt"&gt;--volumes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  How to delete all docker containers including its volumes use?
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-vf&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;docker ps &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  How to delete all docker images?
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker rmi &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;docker images &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nt"&gt;-q&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These commands helped me to reclaim more than &lt;strong&gt;10GB&lt;/strong&gt; of data for only few months of work with Docker.&lt;/p&gt;

&lt;p&gt;This blog was originally published on my personal blog &lt;a href="https://viralpatel.blog"&gt;Viral Patel&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>sysadmin</category>
      <category>bash</category>
    </item>
    <item>
      <title>How to dockerize a Rails application</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Wed, 11 Dec 2019 15:15:17 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/how-to-dockerize-a-rails-application-gp7</link>
      <guid>https://forem.com/viralpatelblog/how-to-dockerize-a-rails-application-gp7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Microservices architecture is a particular way of designing software applications so that it can be deployed independently, test independently,  and scaled independently. Read  &lt;a href="https://microservices.io/"&gt;What are microservices?&lt;/a&gt;  to get more deep understanding about this particular architecture pattern.&lt;/p&gt;

&lt;p&gt;One of the first few steps you as a developer or ops person can do to move towards microservices architecture is to start &lt;em&gt;containerizing&lt;/em&gt;  your applications.&lt;/p&gt;

&lt;p&gt;If  &lt;code&gt;Rails&lt;/code&gt; is part of your stack, this blog will explain you how to dockerize your Rails application.  This blog is not a &lt;code&gt;docker&lt;/code&gt;  tutorial, yet you will be instructed with some best practices with docker. With assumption  that docker is installed on your system and you have a working rails application with PostgreSQL database. If you haven’t installed &lt;strong&gt;Docker&lt;/strong&gt; already, download it from &lt;a href="https://www.docker.com/products/docker-desktop"&gt;official docker website&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create &lt;code&gt;Dockerfile&lt;/code&gt; in the root folder of the project
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Dockerfile&lt;/code&gt; describes all the steps that are required to create an image and would usually be contained within the root directory of the source code repository for your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# rails-app/&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;rails-app/
&lt;span class="nb"&gt;touch &lt;/span&gt;Dockerfile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;First step of writing any &lt;code&gt;Dockerfile&lt;/code&gt; is to choose the base image. We are going to use official ruby docker image but a &lt;strong&gt;slim&lt;/strong&gt; version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; ruby:2.6.3-slim&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Applying labels to images and containers allows you to add metadata via key/value pairs that can later be used to search for and identify Docker images and containers. You can see the labels applied to any image using the docker inspect command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;LABEL&lt;/span&gt;&lt;span class="s"&gt; "maintainer"="viral@fakemail.com"&lt;/span&gt;
&lt;span class="k"&gt;LABEL&lt;/span&gt;&lt;span class="s"&gt; "app_type"="backend"&lt;/span&gt;
&lt;span class="k"&gt;LABEL&lt;/span&gt;&lt;span class="s"&gt; "service_type"="API"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Maintainer is a deprecated field in the Dockerfile specification.&lt;/p&gt;

&lt;p&gt;Docker runs all processes within the container as &lt;code&gt;root&lt;/code&gt; but it is recommended to use &lt;code&gt;USER&lt;/code&gt; instruction to potentially avoid any security risks. Production containers should almost always be run under the context of a non-privileged user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt; root&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;One of the most important instruction &lt;code&gt;ENV&lt;/code&gt; allows you to set shell environment variables that can be used by your running application for configuration and during the build process to simplify the Dockerfile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; APP /usr/src/app&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; RAIL_ENV development&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, we are going to introduce &lt;code&gt;RUN&lt;/code&gt; instruction. We will be using these commands to create the file structure you need, and install required software dependecies. You will also be able to use shell environment variables defined in the previous step. Let’s write few &lt;code&gt;RUN&lt;/code&gt; instructions required for our rails application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nv"&gt;$APP&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get &lt;span class="nt"&gt;-y&lt;/span&gt; update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="nt"&gt;--no-install-recommends&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;    libgmp-dev &lt;span class="se"&gt;\
&lt;/span&gt;      gcc make git &lt;span class="se"&gt;\
&lt;/span&gt;    postgresql-client &lt;span class="se"&gt;\
&lt;/span&gt;    dnsutils &lt;span class="se"&gt;\
&lt;/span&gt;      libpq-dev libglib2.0-dev build-essential patch zlib1g-dev liblzma-dev &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;First instruction creates an app directory and second instruction runs few commands and installs require software, a Postgres client into a single step.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember that every instruction creates a new Docker image layer, so it often makes sense to combine a few logically grouped commands onto a single line. It is even possible to use the ADD instruction in combination with the RUN instruction to copy a complex script to your image and then execute that script with only two commands in the &lt;em&gt;Dockerfile&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next instruction, &lt;code&gt;WORKDIR&lt;/code&gt;, is use to change the working directory in the image for the remaining build instructions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; $APP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;COPY&lt;/code&gt; instruction copies file from the host machine to your container image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; Gemfile $APP/Gemfile&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; Gemfile.lock $APP/Gemfile.lock&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;bundler &lt;span class="nt"&gt;-v&lt;/span&gt; 1.17.3
&lt;span class="k"&gt;RUN &lt;/span&gt;bundle &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . $APP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Install required gems from &lt;code&gt;Gemfile&lt;/code&gt;. Remember we are in currently root folder of the app because of the &lt;code&gt;WORKDIR&lt;/code&gt; instruction use.&lt;/p&gt;

&lt;p&gt;This completes installing required dependency. Now we only need to boot up the application and expose the public endpoint so that public internet can access our application. Before we do that, it is important remove any unnecessary dependency to reduce our overall image size which eventually comes in handy when downloading and running applications.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get purge &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="nt"&gt;--auto-remove&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;      gcc make git &lt;span class="se"&gt;\
&lt;/span&gt;      libpq-dev libglib2.0-dev build-essential patch zlib1g-dev liblzma-dev &lt;span class="se"&gt;\
&lt;/span&gt;        &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;CMD&lt;/code&gt; instruction which defines the command that launches the process that you want to run within the container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; [ "bundle", "exec", "puma", "-C", "config/puma.rb", "config.ru" ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And finally we use &lt;code&gt;EXPOSE&lt;/code&gt; instruction to expose application running process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 3000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here is your final &lt;code&gt;Dockerfile&lt;/code&gt; looks like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; ruby:2.6.3-slim&lt;/span&gt;

&lt;span class="k"&gt;LABEL&lt;/span&gt;&lt;span class="s"&gt; “maintainer”=“viral@fakemail.com”&lt;/span&gt;
&lt;span class="k"&gt;LABEL&lt;/span&gt;&lt;span class="s"&gt; “app_type”=“backend”&lt;/span&gt;
&lt;span class="k"&gt;LABEL&lt;/span&gt;&lt;span class="s"&gt; “service_type”=“API”&lt;/span&gt;

&lt;span class="k"&gt;USER&lt;/span&gt;&lt;span class="s"&gt; root&lt;/span&gt;

&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; APP /usr/src/app&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; RAIL_ENV development&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="nv"&gt;$APP&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get &lt;span class="nt"&gt;-y&lt;/span&gt; update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; —no-install-recommends &lt;span class="se"&gt;\
&lt;/span&gt;    libgmp-dev &lt;span class="se"&gt;\
&lt;/span&gt;    gcc make git &lt;span class="se"&gt;\
&lt;/span&gt;    postgresql-client &lt;span class="se"&gt;\
&lt;/span&gt;    dnsutils &lt;span class="se"&gt;\
&lt;/span&gt;    libpq-dev libglib2.0-dev build-essential patch zlib1g-dev liblzma-dev &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class="k"&gt;*&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; $APP&lt;/span&gt;

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; Gemfile $APP/Gemfile&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; Gemfile.lock $APP/Gemfile.lock&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;bundler &lt;span class="nt"&gt;-v&lt;/span&gt; 1.17.3
&lt;span class="k"&gt;RUN &lt;/span&gt;bundle &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . $APP&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get purge &lt;span class="nt"&gt;-y&lt;/span&gt; —auto-remove &lt;span class="se"&gt;\
&lt;/span&gt;      gcc make git &lt;span class="se"&gt;\
&lt;/span&gt;      libpq-dev libglib2.0-dev build-essential patch zlib1g-dev liblzma-dev &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class="k"&gt;*&lt;/span&gt;


&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; [ “bundle”, “exec”, “puma”, “-C”, “config/puma.rb”, “config.ru” ]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Build your docker image by running,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; user/repo_name:tag &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But wait, before you can run this docker image locally, make sure your database yaml file is configured properly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# database.yaml&lt;/span&gt;
&lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nl"&gt;&amp;amp;default&lt;/span&gt;
  &lt;span class="na"&gt;adapter&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgresql&lt;/span&gt;
  &lt;span class="na"&gt;encoding&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unicode&lt;/span&gt;
  &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
  &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
  &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# For details on connection pooling, see Rails configuration guide&lt;/span&gt;
  &lt;span class="c1"&gt;# https://guides.rubyonrails.org/configuring.html#database-pooling&lt;/span&gt;
  &lt;span class="na"&gt;pool&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;%= ENV.fetch(“RAILS_MAX_THREADS”) { 5 } %&amp;gt;&lt;/span&gt;

&lt;span class="na"&gt;development&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="s"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;*default&lt;/span&gt;
  &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rails_docker_blog_development&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now copy this &lt;code&gt;docker-compose&lt;/code&gt; file to your root folder of the project.  And run &lt;code&gt;docker-compose up -d --build&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;‘3’&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./tmp/db:/var/lib/postgresql/data&lt;/span&gt;
  &lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bash -c “rm -f tmp/pids/server.pid &amp;amp;&amp;amp; bundle exec puma -C config/puma.rb config.ru”&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;RAILS_ENV=development&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.:/usr/src/app&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;“3000:3000”&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, you need to create the database. In another terminal, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose run web rake db:create
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That’s it. Your app should now be running on port 3000 on your Docker daemon.&lt;/p&gt;

&lt;p&gt;On Docker Desktop for Mac and Docker Desktop for Windows, go to &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; on a web browser to see the Rails Welcome.&lt;/p&gt;

&lt;p&gt;If you are using  &lt;a href="https://docs.docker.com/machine/overview/"&gt;Docker Machine&lt;/a&gt; , then docker-machine ip MACHINE_VM returns the Docker host IP address, to which you can append the port &lt;code&gt;&amp;lt;Docker-Host-IP&amp;gt;:3000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--420xo-8Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.docker.com/compose/images/rails-welcome.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--420xo-8Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://docs.docker.com/compose/images/rails-welcome.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>docker</category>
      <category>postgres</category>
      <category>devops</category>
    </item>
    <item>
      <title>Optional Chaining in JavaScript</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Wed, 08 May 2019 00:00:00 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/optional-chaining-in-javascript-5e3a</link>
      <guid>https://forem.com/viralpatelblog/optional-chaining-in-javascript-5e3a</guid>
      <description>&lt;p&gt;Optional chaining enables capabilities for Javascript developers to perform a nil check on the objects before trying to access any object properties.&lt;/p&gt;

&lt;p&gt;As an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.props?.user?.email
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Optional Chaining behaves similarly to Ruby’s pretzel(&amp;amp;) operator.&lt;/p&gt;

&lt;p&gt;Check out the official babel documentation. &lt;a href="https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining"&gt;Babel - Optional Chaining&lt;/a&gt;&lt;/p&gt;

</description>
      <category>es6</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Toggle NOT NULL constraint with Rails migration</title>
      <dc:creator>Viral Patel</dc:creator>
      <pubDate>Wed, 08 May 2019 00:00:00 +0000</pubDate>
      <link>https://forem.com/viralpatelblog/toggle-not-null-constraint-with-rails-migration-1j9h</link>
      <guid>https://forem.com/viralpatelblog/toggle-not-null-constraint-with-rails-migration-1j9h</guid>
      <description>&lt;p&gt;Take advantage of &lt;code&gt;change_column_null&lt;/code&gt; to add or remove &lt;strong&gt;NOT NULL&lt;/strong&gt; constraint on a column. The &lt;code&gt;null&lt;/code&gt; flag indicates whether the value can be &lt;strong&gt;NULL&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To add the constraint (says column cannot be NULL):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;change_column_null :users, :name, false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To remove the constraint (allows the column to be NULL):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;change_column_null :users, :name, true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The method accepts an optional fourth argument to replace existing +NULL+s with some other value. Use that one when enabling the constraint if needed, since otherwise, those rows would not be valid.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Please note the fourth argument does not set a column’s default.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>database</category>
      <category>mysql</category>
      <category>rails</category>
      <category>migrations</category>
    </item>
  </channel>
</rss>
