<?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: Muhammad Wasi Naseer</title>
    <description>The latest articles on DEV Community by Muhammad Wasi Naseer (@wasinaseer).</description>
    <link>https://dev.to/wasinaseer</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%2F491391%2F84f0a1f4-06da-4fc8-aba0-59b7e553c908.png</url>
      <title>DEV Community: Muhammad Wasi Naseer</title>
      <link>https://dev.to/wasinaseer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wasinaseer"/>
    <language>en</language>
    <item>
      <title>Microservices Communication Pattern</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Wed, 09 Mar 2022 19:47:54 +0000</pubDate>
      <link>https://dev.to/wasinaseer/microservices-communication-pattern-3900</link>
      <guid>https://dev.to/wasinaseer/microservices-communication-pattern-3900</guid>
      <description>&lt;p&gt;Microservice is a single application in a suite of small services. Each microservice communicates via a lightweight protocol e.g HTTP, Message Queues. In accordance with the database per service pattern, each of them has its own database. They need to communicate in order to access others data.&lt;/p&gt;

&lt;p&gt;Basically, there are two patterns that microservices can use to talk to each other. &lt;/p&gt;

&lt;h2&gt;
  
  
  Synchronous Communication
&lt;/h2&gt;

&lt;p&gt;In this communication, a microservice communicates via another microservice's API endpoint and waits for its response. The call will be blocked until the response is received. It's easy to understand and implement but it comes with its own drawbacks e.g high latency, downtime, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asynchronous Communication
&lt;/h2&gt;

&lt;p&gt;A microservice sends a message to another microservice via a message queue and does not wait for the response. It's a non-blocking call. There are a lot of benefits of implementing the asynchronous pattern. Let's discuss some of them here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of using Asynchronous Communication
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Reduced Coupling
&lt;/h4&gt;

&lt;p&gt;A sender does not need to know about the receiver interface. They can communicate via standard message queues. It increases decoupling in code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Multiple Subscribers
&lt;/h4&gt;

&lt;p&gt;In asynchronous communication, microservices communicate via message queues. The message can be consumed by multiple subscribers using the publisher/subscribers model.&lt;/p&gt;

&lt;h4&gt;
  
  
  Failure Isolation
&lt;/h4&gt;

&lt;p&gt;When a microservice goes down, the other microservices can do their work and write messages onto the queue. The failed microservice can read them whenever it will be up and running. The message queues ensure fault tolerance and message reliability.&lt;/p&gt;

&lt;h4&gt;
  
  
  Responsiveness
&lt;/h4&gt;

&lt;p&gt;Response time will be reduced much if we do the bare minimum work synchronously in the HTTP call sent by the client and do the rest asynchronously and notify the client about the updates.. &lt;/p&gt;

&lt;h3&gt;
  
  
  Drawbacks of Asynchronous Communication
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Coupling with Message Infrastructure
&lt;/h4&gt;

&lt;p&gt;Since we use message queues a lot in asynchronous communication, there is a high chance that our code can be coupled with message infrastructure. &lt;/p&gt;

&lt;h4&gt;
  
  
  Complexity
&lt;/h4&gt;

&lt;p&gt;Handling asynchronous messages is quite complex. We need to deal with duplicated messages and make the request idempotent. We must identify the cost of consuming multiple messages. There are multiple patterns of handling messages e.g at least one message, at most one message, and only one message.&lt;/p&gt;

&lt;h4&gt;
  
  
  Transaction Management
&lt;/h4&gt;

&lt;p&gt;Dealing with transactions in asynchronous communication is a huge topic to discuss on its own. What if something fails in the transaction then how we can revert the transaction.&lt;/p&gt;

&lt;p&gt;To conclude, there is always a tradeoff when we use any pattern. We must identify systems requirements and choose based on them. &lt;/p&gt;

&lt;p&gt;Thank you so much for the reading. Please checkout my &lt;a href="http://www.wasinaseer.com"&gt;blog&lt;/a&gt; for more content.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/azure/architecture/microservices/design/interservice-communication"&gt;https://docs.microsoft.com/en-us/azure/architecture/microservices/design/interservice-communication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html"&gt;https://www.enterpriseintegrationpatterns.com/ramblings/18_starbucks.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>microservices</category>
      <category>pattern</category>
      <category>asynchronous</category>
      <category>synchronous</category>
    </item>
    <item>
      <title>Why do we need threads along with processes?</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Mon, 20 Dec 2021 19:45:03 +0000</pubDate>
      <link>https://dev.to/wasinaseer/why-do-we-need-threads-along-with-processes-4gg4</link>
      <guid>https://dev.to/wasinaseer/why-do-we-need-threads-along-with-processes-4gg4</guid>
      <description>&lt;p&gt;Let’s first define what a process is. A process is an independent isolated program to which an OS gives resources, file handles, or credentials. An OS can have many processes with its own address space, heap, or stack. When processes need to communicate, they need to use coarse-grained communication mechanisms such as a socket, shared memory, files, etc. Nothing is directly shared among processes.&lt;/p&gt;

&lt;p&gt;A process can have many threads. A thread is like a lightweight process but with a major difference of &lt;strong&gt;sharing state&lt;/strong&gt;. Threads share process-wide resources like files or memory which enables fine-grained communication. All threads within a process have their own stack, program counter, and local variables except heap. This sharing of state among multiple threads leads to concurrency problems which we’ll discuss in our upcoming posts.&lt;/p&gt;

&lt;p&gt;Along with the benefits of multiprocessing(resource utilization, convenience, fairness), threads enable us to use the same process-wide resources without using any communication mechanisms.&lt;/p&gt;

&lt;h5&gt;
  
  
  REFERENCE
&lt;/h5&gt;

&lt;p&gt;Topic#1.1: &lt;a href="https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601"&gt;https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601&lt;/a&gt;&lt;/p&gt;

</description>
      <category>thread</category>
      <category>process</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>Isolation Levels in Transaction</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Fri, 17 Dec 2021 21:24:01 +0000</pubDate>
      <link>https://dev.to/wasinaseer/isolation-levels-in-transaction-5gkj</link>
      <guid>https://dev.to/wasinaseer/isolation-levels-in-transaction-5gkj</guid>
      <description>&lt;p&gt;As we have already discussed in the &lt;a href="https://dev.to/wasinaseer/transaction-in-database-46c4"&gt;article&lt;/a&gt;, isolation specifies that each transaction should be executed independently of other transactions. The level specifies how much isolation can be provided to each transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read Uncommitted
&lt;/h2&gt;

&lt;p&gt;It is the lowest level of isolation in a transaction. At this level, data changed during a transaction, which is essentially uncommitted data, will be directly visible to other transactions.&lt;/p&gt;

&lt;p&gt;It can lead to a &lt;em&gt;Dirty Read&lt;/em&gt; problem because the data visible during the transaction can either be committed or rollbacked in case of failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read Committed
&lt;/h2&gt;

&lt;p&gt;In the read-committed transactions, data will only be visible to read after the transaction has been committed.&lt;/p&gt;

&lt;p&gt;This level may seem okay at first but it has a problem of &lt;em&gt;Non-repeatable Read&lt;/em&gt; for other transactions. During a course of a transaction, a non-repeatable read occurs when a row is fetched twice and gives different results each time. The reason is simple. It is because, at the first read, the other transaction didn’t commit the row updates, and on the second read, the update has been committed. This problem can be resolved at the next level by acquiring locks in each transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-Repeatable Read
&lt;/h2&gt;

&lt;p&gt;We know that a non-repeatable read occurs when two transactions are executed at the same time. And, one transaction commits a result which the other transaction has already read and on the second read, it gets the committed result.&lt;/p&gt;

&lt;p&gt;The problem can be resolved when any transaction has to acquire a read lock if it’s only reading data, and write lock if it needs to write data. In the article &lt;a href="https://dev.to/wasinaseer/read-lock-vs-write-lock-5181"&gt;Read Lock vs Write Lock&lt;/a&gt;, the locks have already been discussed in detail.&lt;/p&gt;

&lt;p&gt;Let’s assume two transactions A and B. Transaction A wants to update a row. It has to acquire a write lock which prevents other transactions to acquire a read/write lock. Then transaction B also wants to update some other row but it first needs to query the row that will be changed by transaction A. In order to do that, transaction B has to acquire a read lock which it cannot acquire as transaction A has already acquired a write lock on it.&lt;/p&gt;

&lt;p&gt;In short, the locks prevent the problem of non-repeatable read. Hence, this isolation level is called a Non-repeatable read.&lt;/p&gt;

&lt;p&gt;However, at this level, we have a new problem known as &lt;em&gt;Phantom Read&lt;/em&gt;. A phantom read occurs when we execute a query two times during a transaction and each time we get a different number of rows. It’s because some rows have either been inserted or deleted. In non-repeatable read, we get the same number of rows but the rows have been updated because of update query.&lt;/p&gt;

&lt;h2&gt;
  
  
  Serializable
&lt;/h2&gt;

&lt;p&gt;It is the highest level of isolation in transactions. Instead of holding the lock on a row, it holds a lock on multiple rows satisfying a WHERE condition. Here, we are holding the range lock.&lt;/p&gt;

&lt;p&gt;When we hold a lock on the range then we can avoid the phantom read problem because the newly inserted rows satisfying the condition have to wait for releasing the lock and the same goes for deletion.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Transaction in Database</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Thu, 16 Dec 2021 20:33:06 +0000</pubDate>
      <link>https://dev.to/wasinaseer/transaction-in-database-46c4</link>
      <guid>https://dev.to/wasinaseer/transaction-in-database-46c4</guid>
      <description>&lt;p&gt;A transaction is a unit of work that must maintain ACID properties. ACID is an acronym of Atomicity, Consistency, Isolation, and Durability&lt;/p&gt;

&lt;p&gt;Let’s look at each of them in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Atomicity
&lt;/h2&gt;

&lt;p&gt;A transaction contains a query or a set of queries. In order to maintain atomicity property, the transaction either has to execute all the queries or none of them. In short, all the queries will be treated as atomic. If any of the queries fails, the transaction has to roll back all the previous executed queries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Consistency
&lt;/h2&gt;

&lt;p&gt;First, let’s understand what a consistent state means in a database. A database is in a consistent state if it follows all the rules defined using constraints, triggers, or cascades.&lt;/p&gt;

&lt;p&gt;A transaction must always lead the database to a consistent state.&lt;/p&gt;

&lt;p&gt;For example, if a table &lt;code&gt;user&lt;/code&gt; has a unique constraint on the &lt;code&gt;national_id&lt;/code&gt; column and a query in my transaction tries to insert the duplicate &lt;code&gt;national_id&lt;/code&gt;, the query has to be failed. Otherwise, the database will not be in a consistent state as it will violate the unique constraint on our &lt;code&gt;user&lt;/code&gt; table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Isolation
&lt;/h2&gt;

&lt;p&gt;All transactions must be executed independently of other transactions. Each transaction will be executed as if it’s the only transaction executed in the database.&lt;/p&gt;

&lt;p&gt;There are multiple levels of isolation levels that we can define in a database. We have discussed them in detail in our &lt;a href="https://dev.to/wasinaseer/isolation-levels-in-transaction-5gkj"&gt;this article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Durability
&lt;/h2&gt;

&lt;p&gt;If a transaction commits data, it must be able to persist them even if the system fails or restart. In short, committed transactions means the data has been persisted, and if it’s not able to save the data due to system failure, it must persist at the system start.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Read Lock vs Write Lock</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Tue, 14 Dec 2021 22:42:17 +0000</pubDate>
      <link>https://dev.to/wasinaseer/read-lock-vs-write-lock-5181</link>
      <guid>https://dev.to/wasinaseer/read-lock-vs-write-lock-5181</guid>
      <description>&lt;p&gt;A lock is a synchronization primitive which limits access to a shared resource among multiple threads. We don’t need locks when only one thread is running or there is no shared resource because lock is always acquired on a shared resource. Let’s look at what locks can be acquired.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read Lock
&lt;/h2&gt;

&lt;p&gt;Multiple threads can acquire read lock at the same time. However, while holding the lock, they can only read the data. Also, no other thread can update the data while the read lock is acquired.&lt;/p&gt;

&lt;p&gt;In other words, during the period of holding the read lock on data, it’s guaranteed that the data haven’t been changed by any thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write Lock
&lt;/h2&gt;

&lt;p&gt;Write lock can be acquired by only one thread at a time. During the lock holding period, the thread can read or write data. If a write lock has been acquired, no other threads can read the data or acquire a read lock on the data.&lt;/p&gt;

&lt;p&gt;In short, during the period of holding the write lock on data, it’s guaranteed that the data haven’t been read or changed by any thread.&lt;/p&gt;

&lt;p&gt;Write lock is more strict than the read lock and ensures better synchronization. In a multithreaded application, we usually go for the read lock if we’re only reading data and taking decisions based on the read data. That’s why we need to ensure that whatever we have read for taking the decisions must be true until the processing has been done and a response has been sent to the client.&lt;/p&gt;

</description>
      <category>lock</category>
      <category>multithreading</category>
      <category>concurrency</category>
    </item>
    <item>
      <title>What are SOLID principles?</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Mon, 13 Dec 2021 20:46:47 +0000</pubDate>
      <link>https://dev.to/wasinaseer/what-are-solid-principles-3pb5</link>
      <guid>https://dev.to/wasinaseer/what-are-solid-principles-3pb5</guid>
      <description>&lt;p&gt;SOLID is an acronym of below five object-oriented design principles.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;S – Single Responsibility&lt;/li&gt;
&lt;li&gt;O – Open Closed&lt;/li&gt;
&lt;li&gt;L – Liskov Substitution&lt;/li&gt;
&lt;li&gt;I – Interface Segregation&lt;/li&gt;
&lt;li&gt;D – Dependency Inversion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Single Responsibility Principle
&lt;/h2&gt;

&lt;p&gt;Every entity (class, method, or module) should have a single responsibility. In other words, every entity must have a single reason to change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Open Closed Principle
&lt;/h2&gt;

&lt;p&gt;Every class should be Open for extension and Closed for modification.&lt;/p&gt;

&lt;p&gt;For example, if there is a method to read a file and the filename is hardcoded in the function, we need to change the method whenever the file name changes. Instead of hardcoding the filename, we can pass it via a parameter in the method.&lt;/p&gt;

&lt;p&gt;However, there is an exception to fixing bugs in the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Liskov Substitution
&lt;/h2&gt;

&lt;p&gt;In simple words, it is to replace the supertype object with the subtype object and the behavior is not undesirable then this test has been passed.&lt;/p&gt;

&lt;p&gt;If S is a subclass of T, then the object of T can be replaced with the object of S without getting any undesirable behaviour.&lt;/p&gt;

&lt;p&gt;For example, if we create a class Rectangle with properties width and height then we create its subclass Square which sets the width and height the same whenever we update any of them. When we added the constraint in the square, it leads to undesirable behaviour when we substitute the square object for the rectangle object. In this example, it did not pass.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Square&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setWidth&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;width&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setHeight&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;height&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;span class="cm"&gt;/** 
Let's create a Rectangle object (supertype)
Rectangle r = new Rectangle();
Now checking Liskov Substitution by replacing Rectangle object with the Square object.
The square objects ensures the constraint that width and height must be same. 
However the reference type is rectangle. Hence, the liskov substitution test has not passed.
**/&lt;/span&gt;

&lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="n"&gt;r&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;Square&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setWidth&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Interface Segregation
&lt;/h2&gt;

&lt;p&gt;An interface should only have those methods which can be implemented by any client. In other words, no client should be forced to implement methods that it does not use. If that’s the case, we should divide the interface into two or more interfaces so that every client implements only those methods that it’s using.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dependency Inversion
&lt;/h2&gt;

&lt;p&gt;Whenever we have a dependency on objects, they should be passed via the constructor. We should not create the objects and make the class tightly coupled with the objects. High-level modules should not depend on the low-level modules, instead, they should rely on abstractions.&lt;/p&gt;

&lt;p&gt;It also helps in unit testing. We can mock the dependencies by providing their mocks.&lt;/p&gt;

</description>
      <category>design</category>
      <category>solid</category>
      <category>designpatterns</category>
      <category>java</category>
    </item>
    <item>
      <title>Why Strings are Immutable in Java?</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Sun, 12 Dec 2021 21:27:48 +0000</pubDate>
      <link>https://dev.to/wasinaseer/why-strings-are-immutable-in-java-1dkp</link>
      <guid>https://dev.to/wasinaseer/why-strings-are-immutable-in-java-1dkp</guid>
      <description>&lt;p&gt;Before understanding why strings are immutable in java, we need to think about why do we make something immutable?&lt;/p&gt;

&lt;p&gt;Immutable means once created we cannot change it. The one reason that we can think of making anything immutable is synchronization when it’s shared. It’s the same reason that strings are immutable.&lt;/p&gt;

&lt;p&gt;In Java, String objects are shared and cached in String Pool. It’s a designated place in a heap where strings are stored and shared among multiple threads if they have the same value. For example: In String Pool, if there is already a string with the value “test” and the program wants to create another string object with the same value then it will get the same reference instead of creating a new string object. Now we know how strings are stored in heap. Let’s look at the reasons why they are immutable. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The first reason is to make it thread safe. Since strings are shared among multiple threads in string pool, we need to restrict any random thread to change it. Any change in a string can be impacted to other threads which are accessing the same string. If a thread wants to update the value of a string, it needs to create another string and takes it reference.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Usually, we use String as a key in Map. If Strings were mutable, anyone can change the value of the strings, and we would lose the actual key.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>strings</category>
      <category>programming</category>
    </item>
    <item>
      <title>Memory Management in Java | Heap vs Stack</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Sun, 31 Oct 2021 11:41:08 +0000</pubDate>
      <link>https://dev.to/wasinaseer/memory-management-in-java-heap-vs-stack-3i63</link>
      <guid>https://dev.to/wasinaseer/memory-management-in-java-heap-vs-stack-3i63</guid>
      <description>&lt;p&gt;In Java, a variable can either hold a primitive type or a reference of an object. This holds true for all cases. Primitive types are stored in stack and objects are stored in heap. To store objects, it needs to maintain their structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uRc3Tivo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53k0grjdlezurvapcl5n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uRc3Tivo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53k0grjdlezurvapcl5n.png" alt="Variable holds either a primitive type or the reference of an object&amp;lt;br&amp;gt;
" width="880" height="534"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ve always heard that a variable can be passed by value or passed by reference. But, essentially, in Java, there is no such thing as pass-by-reference. It always passes variables by value. But, you need to see what the variable holds. If it holds primitive type then we call it to pass by value or in the case of the reference of an object, we call it to pass by reference. However, it always passes the copy of whatever it holds.&lt;/p&gt;

&lt;p&gt;Remember that whenever a variable is passed, its copy will be passed. And, if the variable contains the pointer of an object then the copy will also contain the same pointer. So, if we change anything in the referred object, it will also reflect all variables that hold the same pointer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Variables and Const Correctness
&lt;/h2&gt;

&lt;p&gt;Final variables in java are not essentially final. I know we cannot change the value assigned to the variable but if you assign a reference to an object. Then, we can change the referred object without changing the reference which the final variable holds. This is against the concept of const correctness.&lt;/p&gt;

&lt;p&gt;Const correctness is basically the concept that if you pass a final variable to a function and the function is able to modify the state of the referred object. Then, the language lacks const correctness.&lt;/p&gt;

</description>
      <category>heap</category>
      <category>stack</category>
      <category>memory</category>
      <category>java</category>
    </item>
    <item>
      <title>What is static and non-static in OOP?</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Sat, 21 Aug 2021 22:34:51 +0000</pubDate>
      <link>https://dev.to/wasinaseer/what-is-static-and-non-static-in-oop-1a3d</link>
      <guid>https://dev.to/wasinaseer/what-is-static-and-non-static-in-oop-1a3d</guid>
      <description>&lt;p&gt;Object Oriented Programming is all about classes and objects. We first create a class in which we specify two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;data (variables). We use it for storing data&lt;/li&gt;
&lt;li&gt;behaviors (functions). We use it for manipulating/retrieving the variables.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We then create object of a class by using the &lt;code&gt;new&lt;/code&gt; keyword. &lt;/p&gt;

&lt;p&gt;The variables or behaviors can either be accessed at &lt;strong&gt;&lt;em&gt;class level&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;object level&lt;/em&gt;&lt;/strong&gt;. &lt;br&gt;
The class level access means that we can directly access them using the class name. For example:&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="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxRoomInHouse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;House&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MAX_ROOMS&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The object level means that we can only access them using the object of the class. The object level access is the default access of variables or functions unless we specify &lt;code&gt;static&lt;/code&gt;.&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="nc"&gt;House&lt;/span&gt; &lt;span class="n"&gt;myHouse&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;House&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;floors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myHouse&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getNumberOfFloors&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 make a function or variable to be accessed at class level by specifying &lt;code&gt;static&lt;/code&gt;. We don't need to create an object to access static variables or functions. &lt;/p&gt;

&lt;h3&gt;
  
  
  When to use static and non-static?
&lt;/h3&gt;

&lt;p&gt;We should use &lt;code&gt;static&lt;/code&gt; variables or functions wherever we need global common variable or behavior across all the objects of the class. For example, in our house example, the &lt;code&gt;House.MAX_FLOORS&lt;/code&gt; is the common field that we will be same for all objects of the House class, and same goes for &lt;code&gt;House.getMaxFloors()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;We should not specify variable or function static whenever the function or variables belongs to each individual object. For example, in &lt;code&gt;myHouse.getNumberOfFloors()&lt;/code&gt; is basically specifying how many floors does &lt;code&gt;myHouse&lt;/code&gt; object has?&lt;/p&gt;

</description>
      <category>oop</category>
      <category>java</category>
      <category>static</category>
      <category>nonstatic</category>
    </item>
    <item>
      <title>Difference Between Aggregation and Composition</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Mon, 09 Aug 2021 20:09:31 +0000</pubDate>
      <link>https://dev.to/wasinaseer/difference-between-aggregation-and-composition-1p1h</link>
      <guid>https://dev.to/wasinaseer/difference-between-aggregation-and-composition-1p1h</guid>
      <description>&lt;p&gt;Aggregation and Composition are basically the concepts about how to manage objects. &lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregation
&lt;/h2&gt;

&lt;p&gt;Aggregation uses &lt;em&gt;loose coupling&lt;/em&gt;, which means that it does not hold the actual object in the class. Instead, the object is being passed via getter or setter function. It gives the benefit that when the object of the parent class dies then the child class object remains intact.&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;class&lt;/span&gt; &lt;span class="nc"&gt;House&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;Table&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

 &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setTable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Table&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;table&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;Table&lt;/span&gt; &lt;span class="nf"&gt;getTable&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;table&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;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

 &lt;span class="nc"&gt;Table&lt;/span&gt; &lt;span class="n"&gt;table&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;Table&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

 &lt;span class="c1"&gt;// this house' table is set externally and will not be deleted &lt;/span&gt;
 &lt;span class="c1"&gt;// upon deleting this House object&lt;/span&gt;
 &lt;span class="nc"&gt;House&lt;/span&gt; &lt;span class="n"&gt;house&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;House&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
 &lt;span class="n"&gt;house&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setTable&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;table&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;h2&gt;
  
  
  Composition
&lt;/h2&gt;

&lt;p&gt;Composition uses &lt;em&gt;tight coupling&lt;/em&gt;, which means that it holds the object. An instance variable is created and the object is stored in it. Whenever the object of the parent class removed/deleted then the child class object will also be deleted.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

 &lt;span class="c1"&gt;// This heart will be deleted and nowhere to access once its &lt;/span&gt;
 &lt;span class="c1"&gt;// human gets deleted&lt;/span&gt;
 &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Heart&lt;/span&gt; &lt;span class="n"&gt;heart&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;Heart&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="kd"&gt;class&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;

 &lt;span class="c1"&gt;// this human's heart is set internally and will be deleted &lt;/span&gt;
 &lt;span class="c1"&gt;// upon deleting this Human object&lt;/span&gt;
 &lt;span class="nc"&gt;Human&lt;/span&gt; &lt;span class="n"&gt;human&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;Human&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;



</description>
      <category>java</category>
      <category>aggregation</category>
      <category>composition</category>
      <category>oop</category>
    </item>
    <item>
      <title>What are Views in Database</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Sun, 01 Aug 2021 22:44:33 +0000</pubDate>
      <link>https://dev.to/wasinaseer/what-are-views-in-database-1cdk</link>
      <guid>https://dev.to/wasinaseer/what-are-views-in-database-1cdk</guid>
      <description>&lt;p&gt;Database View is a virtual/logical table that is made up of a SQL query on the physical tables. A simple view itself does not exist physically at all but a query under a name. &lt;/p&gt;

&lt;h3&gt;
  
  
  Reading data from View
&lt;/h3&gt;

&lt;p&gt;When we read data from view, it's basically a query which gets executed under a name&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating data from View
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Insert query
&lt;/h4&gt;

&lt;p&gt;We can insert data into simple views if Primary key and Not null fields are included in the View. If they are not included, constraint error will be thrown&lt;/p&gt;

&lt;h4&gt;
  
  
  Update query
&lt;/h4&gt;

&lt;p&gt;We can update data in simple views&lt;/p&gt;

&lt;h4&gt;
  
  
  Delete query
&lt;/h4&gt;

&lt;p&gt;We can also delete data in simple View. Behind the scenes, it executes delete query on the physical table&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Views
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Simple Views&lt;br&gt;
They are made up of single table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Complex Views&lt;br&gt;
They are made up of more than one table.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why we use it?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To simplify complex queries&lt;/li&gt;
&lt;li&gt;To hide the complexity of underlying tables to the end user or external apps&lt;/li&gt;
&lt;li&gt;To enable backward compatibility. If we make changes in the schema, we can provide views to the existing apps so that they won't break.&lt;/li&gt;
&lt;li&gt;To provide extra security layer using read-only views&lt;/li&gt;
&lt;li&gt;To provide computed columns as database should not save computed columns&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are Materialized Views?
&lt;/h3&gt;

&lt;p&gt;In order to increase performance in reading data, the data is actually stored on disk. The next time user queries the data from the view, the result wouldn't be calculated on the fly from multiple tables but &lt;strong&gt;it's a query on the view&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is used when the view is made up of multiple tables using JOIN. As we know that joining multiple table decreases performance.&lt;/p&gt;

</description>
      <category>views</category>
      <category>database</category>
    </item>
    <item>
      <title>Automating to copy a set of commits from one branch to another</title>
      <dc:creator>Muhammad Wasi Naseer</dc:creator>
      <pubDate>Mon, 26 Oct 2020 17:42:10 +0000</pubDate>
      <link>https://dev.to/wasinaseer/automating-to-copy-a-set-of-commits-from-one-branch-to-another-1ff8</link>
      <guid>https://dev.to/wasinaseer/automating-to-copy-a-set-of-commits-from-one-branch-to-another-1ff8</guid>
      <description>&lt;p&gt;This article will describe how to copy a set of contiguous commits from one branch to another branch in a repository&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jW0chUcd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/10000/0%2ACqwz58a0G9gTMYbg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jW0chUcd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/10000/0%2ACqwz58a0G9gTMYbg" alt="Photo by [Yancy Min](https://unsplash.com/@yancymin?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)"&gt;&lt;/a&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@yancymin?utm_source=medium&amp;amp;utm_medium=referral"&gt;Yancy Min&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Script
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/Muhammadwasi/05fa262f3afd92163896fbe8295b9b16"&gt;Code Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The final version of our bash script copy_commitswould be like this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;copy_commits.sh -p &amp;lt;project_path&amp;gt; -s &amp;lt;source-branch&amp;gt; -t &amp;lt;target-branch&amp;gt; -f &amp;lt;commit-start&amp;gt; -e &amp;lt;commit-end&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s assume that we have two branches &lt;strong&gt;dev1&lt;/strong&gt; and &lt;strong&gt;dev2&lt;/strong&gt;, and we want to move a set of contiguous commits from &lt;strong&gt;dev1&lt;/strong&gt; to &lt;strong&gt;dev2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;dev1&lt;/strong&gt; branch has 5 commits and the &lt;strong&gt;dev2&lt;/strong&gt; branch has 1 commit. And, we want to copy the 4 extra commits of &lt;strong&gt;dev1&lt;/strong&gt; to the &lt;strong&gt;dev2&lt;/strong&gt;  branch by specifying the start commit and end commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;p&gt;We will do the following steps to automate the task.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, we’ll parse the parameters passed at the time of the script execution. The values will be store in the following five variables:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;$PROJECT_PATH&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;$SOURCE_BRANCH&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;$TARGET_BRANCH&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;$FROM_COMMIT&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;$TO_COMMIT&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We will first check out to &lt;strong&gt;dev1&lt;/strong&gt; branch (source branch) and get all the commits in between the start and end commit (inclusive) in an array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We’ll then check out to &lt;strong&gt;dev2&lt;/strong&gt;  branch, and we’ll take each of the commits from the array and cherry-pick the commit on &lt;strong&gt;dev2&lt;/strong&gt;  (target branch)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 1: Parsing parameters and storing parameters into variables
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while getopts ":p:s:t:f:e:" o; do
    case "${o}" in
    p)
            PROJECT_PATH="${OPTARG}"
            ;;
        s)
            SOURCE_BRANCH="${OPTARG}"
            ;;
        t)
            TARGET_BRANCH="${OPTARG}"
            ;;
        f)
            FROM_COMMIT="${OPTARG}"
            ;;
        e)
            TO_COMMIT="${OPTARG}"
            ;;
        *)
            abnormal_exit
            ;;
    esac
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Following is the code for the &lt;code&gt;abnormal_exit()&lt;/code&gt; function&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Prints usage
usage() {                                      
  echo "Usage: $0 -p &amp;lt;project_path&amp;gt; -s &amp;lt;source-branch&amp;gt; -t &amp;lt;target-branch&amp;gt; -f &amp;lt;commit-start&amp;gt; -e &amp;lt;commit-target&amp;gt;  " 1&amp;gt;&amp;amp;2 
}

# Prints usage and exit
abnormal_exit() {                              
  usage
  exit 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Step 2: Getting commits’ hash from the source branch and storing it into an array
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;commits=($(git rev-list "$FROM_COMMIT^..$TO_COMMIT"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;git rev-list&lt;/code&gt; is used to list the git commits in reverse chronological order. Therefore, the latest commit will be at the start of the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;lt;start_commit_hash&amp;gt;..&amp;lt;end_commit_hash&amp;gt;&lt;/code&gt; is a notation to specify that start from the &lt;strong&gt;start_commit_hash&lt;/strong&gt; (not included) to the &lt;strong&gt;end_commit_hash&lt;/strong&gt; (included). If we want to get &lt;strong&gt;start_commit_hash&lt;/strong&gt; in the result then we have to specify the parent of the start commit. The parent of the start commit can be specified using caret ^ at the end of the commit hash. That is why a caret has been used at the end of the start commit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;$FROM_COMMIT^&lt;/code&gt; : It means that start from the parent of the &lt;code&gt;$FROM_COMMIT&lt;/code&gt;. The caret (^) is used to specify the parent of the commit otherwise the &lt;code&gt;$FROM_COMMIT&lt;/code&gt; will not be included in the final result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;$(some_command_here)&lt;/code&gt; : This notation is used when we want to run some command but want to store the output of the command into a variable. Hence, we can assign its output to a variable on the left.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;($(some_command_here))&lt;/code&gt; : If we encapsulate some text into parentheses, then bash will convert them into an array based on the IFS(Internal Field Separator). It is by default whitespace. Hence the output of the &lt;code&gt;$(some_command_here)&lt;/code&gt; will be converted into an array.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Step 3: Applying commits on the target branch by picking them from the commits array
&lt;/h3&gt;

&lt;p&gt;We have got all the commits into the commits array. Now, we have to check out the target branch.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout $TARGET_BRANCH

if ! [[ $? -eq 0 ]]
then
    echo $'\u274c' "ERROR: Failed to checkout to $TARGET_BRANCH"
    exit 2
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We have to pick each commit one by one and run the &lt;code&gt;cherry-pick&lt;/code&gt; command. But, remember that we have to pick the commits in reverse order because the latest commit is at the start of the array. That is why we will start picking commits from length-1 index of the commits array.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# For each commit
for ((i=$((${#commits[@]}-1));i&amp;gt;=0;i--))
do
    echo "Cherry Picking ${commits[i]} ..."
    git cherry-pick "${commits[i]}"
    # If the above command fails, then exit with proper message
    if ! [[ $? -eq 0 ]]
    then
        echo "ERROR: Cherry pick commit with ${commits[i]} failed, exiting..."
        exit 2
    fi
    # Show success message for the copied commit
    echo "DONE: Commit ${commits[i]} copied"
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;#&lt;/code&gt; is used to find the length of the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The loop starts at length-1, and we will decrementing i until it is less than 0.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;$?&lt;/code&gt; is used to get the exit status code of the most recent command executed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Final Script Screenshot
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cySvQqea--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ALniWy3M5iXMjBVUiKMYYHQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cySvQqea--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2ALniWy3M5iXMjBVUiKMYYHQ.jpeg" alt="Git Project Commit Log"&gt;&lt;/a&gt;&lt;em&gt;Git Project Commit Log&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h7GzxxeL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A6LxT64HczwoiZlhI0DJQpA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h7GzxxeL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2A6LxT64HczwoiZlhI0DJQpA.jpeg" alt="Screenshot of the script when executed"&gt;&lt;/a&gt;&lt;em&gt;Screenshot of the script when executed&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>bash</category>
      <category>automation</category>
      <category>commits</category>
    </item>
  </channel>
</rss>
