<?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: Amr Saeed</title>
    <description>The latest articles on Forem by Amr Saeed (@amrsaeedhosny).</description>
    <link>https://forem.com/amrsaeedhosny</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%2F513970%2F95e8e497-6dd1-4a87-952a-c6674cb1b358.jpg</url>
      <title>Forem: Amr Saeed</title>
      <link>https://forem.com/amrsaeedhosny</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/amrsaeedhosny"/>
    <language>en</language>
    <item>
      <title>Symmetric vs. Asymmetric Encryption</title>
      <dc:creator>Amr Saeed</dc:creator>
      <pubDate>Thu, 03 Feb 2022 16:02:19 +0000</pubDate>
      <link>https://forem.com/amrsaeedhosny/symmetric-vs-asymmetric-encryption-big</link>
      <guid>https://forem.com/amrsaeedhosny/symmetric-vs-asymmetric-encryption-big</guid>
      <description>&lt;p&gt;In the past, when two persons wanted to send messages to each other in a secure way, they resorted to something called &lt;em&gt;Symmetric Encryption&lt;/em&gt;. Symmetric encryption is a type of encryption that uses one secret key to encrypt and decrypt messages. Hence, the two parties must have this key to communicate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H8v4n_ln--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://storage.googleapis.com/amrsaeed.com/posts/symmetric-vs-asymmetric-encryption/symmetric-encryption.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H8v4n_ln--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://storage.googleapis.com/amrsaeed.com/posts/symmetric-vs-asymmetric-encryption/symmetric-encryption.png" alt="Sending a message from you to me using symmetric encryption." width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The issue with this approach is that saving and protecting the keys is very difficult because these two persons have to know the key. So, if one of them has been hacked, the key could be stolen easily. Moreover, even if the two persons decided to send the key directly on the network and change it periodically, someone could be intercepting the network to get the key; this is commonly known as a &lt;a href="https://en.wikipedia.org/wiki/Man-in-the-middle_attack"&gt;Man-in-the-middle&lt;/a&gt; attack.&lt;/p&gt;

&lt;p&gt;This type of encryption in its simple form has caused many problems until a new type of encryption arose, which is &lt;em&gt;Asymmetric Encryption&lt;/em&gt;, also known as &lt;em&gt;Public-key Cryptography&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;In this type of encryption, each device generates a key pair, one is public, and the other is private. Both of the two keys are connected mathematically in a way that if you encrypt a message with a public key, it can only be decrypted by the private key and vice versa.&lt;/p&gt;

&lt;p&gt;The main idea is that if you want to send me an encrypted message, I'll have to generate a public and private key pair. After that, I'll send you the public key then you can use it to encrypt your message and send it over the network. If anyone tried to intercept your message, they wouldn't understand it because it's encrypted. Also, if they tried to decrypt it, they wouldn't be able to do so because they need the private key. Luckily in this approach, the private key only resides on my machine and is never sent over the network.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Jfes_yU3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://storage.googleapis.com/amrsaeed.com/posts/symmetric-vs-asymmetric-encryption/asymmetric-encryption.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Jfes_yU3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://storage.googleapis.com/amrsaeed.com/posts/symmetric-vs-asymmetric-encryption/asymmetric-encryption.png" alt="Sending a message from you to me using asymmetric encryption." width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same idea would apply to me if I decided to reply to your message. You've to send me your public key to decrypt my response and send it.&lt;/p&gt;

&lt;p&gt;Notice that there is no danger in sending the public key over the network or even uploading it to the internet as this is its main purpose. Additionally, the current machines' limited capabilities make it difficult to derive the private key from the public key. This operation can take a huge amount of time.&lt;/p&gt;

&lt;p&gt;Till now, you should have understood the core difference between symmetric and asymmetric encryption. Follow the coming articles if you want to understand their applications and their essential to you as a software engineer.&lt;/p&gt;

</description>
      <category>security</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Some Reasons Why You Should Follow Naming Conventions</title>
      <dc:creator>Amr Saeed</dc:creator>
      <pubDate>Fri, 15 Jan 2021 10:57:49 +0000</pubDate>
      <link>https://forem.com/amrsaeedhosny/some-reasons-why-you-should-follow-naming-conventions-3k33</link>
      <guid>https://forem.com/amrsaeedhosny/some-reasons-why-you-should-follow-naming-conventions-3k33</guid>
      <description>&lt;p&gt;Writing clean and readable code is not an easy task. It requires you to keep reading about good practices and apply them as much as you can to avoid the bad ones.&lt;/p&gt;

&lt;p&gt;Today we're going to talk about the benefits of one of those practices to understand their importance. But before touching upon this, let's understand what does naming conventions mean.&lt;/p&gt;

&lt;p&gt;Naming conventions are some rules that help you to name things in your code such as variables, functions, and classes.&lt;/p&gt;

&lt;p&gt;One of the common naming conventions which are adopted by many programming languages such as Java is the &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Camel_case" rel="noopener noreferrer"&gt;Camel Case&lt;/a&gt;&lt;/em&gt;. In &lt;em&gt;Camel Case&lt;/em&gt;, you name your variables and functions in the following way &lt;code&gt;myNameIsAwesome&lt;/code&gt;. The first word starts with a lower case letter followed by as many words as you want but they start with upper case letters.&lt;/p&gt;

&lt;p&gt;Python and similar languages such as Ruby adopt another naming convention which is &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Snake_case" rel="noopener noreferrer"&gt;Snake Case&lt;/a&gt;&lt;/em&gt; &lt;code&gt;my_name_is_awesome&lt;/code&gt;. You can clearly tell the difference between &lt;em&gt;Camel Case&lt;/em&gt; and &lt;em&gt;Snake Case&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now it's time to understand why naming things in your code is very important. You might see it as a trivial task with no benefits at all and you don't have to care about it. But believe me, it is not a waste of time as it looks like.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unified Style Enhances Readability
&lt;/h2&gt;

&lt;p&gt;Imagine a large project with hundreds of developers who participate. If every developer decided to name variables and functions with their own style, the project is going to be a mess. &lt;/p&gt;

&lt;p&gt;Developers who follow the same naming convention across the project contribute to its readability. The project will look as if it was written by a single person.&lt;/p&gt;

&lt;h2&gt;
  
  
  Differentiate Items by Their Names
&lt;/h2&gt;

&lt;p&gt;Another advantage of following naming conventions that also contributes to the project readability is that you can differentiate items easily by their names. If in Java, for example, you find an item that is named in &lt;em&gt;Camel Case&lt;/em&gt; &lt;code&gt;myItem&lt;/code&gt;, that means it's a variable, object, or method. If it's named in &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Camel_case" rel="noopener noreferrer"&gt;Pascal Case&lt;/a&gt;&lt;/em&gt; &lt;code&gt;MyItem&lt;/code&gt;, that means it's a class. If it's named in &lt;em&gt;&lt;a href="https://en.wikipedia.org/?title=SCREAMING_SNAKE_CASE&amp;amp;redirect=no" rel="noopener noreferrer"&gt;Screaming Snake Case&lt;/a&gt;&lt;/em&gt; &lt;code&gt;MY_ITEM&lt;/code&gt;, that means it's a constant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Circle&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt;&lt;span class="o"&gt;)&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;radius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;radius&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;double&lt;/span&gt; &lt;span class="nf"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;radius&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="nc"&gt;Main&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;static&lt;/span&gt; &lt;span class="kt"&gt;void&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="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Circle&lt;/span&gt; &lt;span class="n"&gt;circle&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;Circle&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;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;circle&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getArea&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Search Becomes Easier
&lt;/h2&gt;

&lt;p&gt;Following a naming convention makes it easy for you to search for items in the project. If for example, you want to find the method that is responsible for authenticating users, you'll be expecting to find it with the name &lt;code&gt;login&lt;/code&gt; or &lt;code&gt;signIn&lt;/code&gt; for the &lt;em&gt;Camel Case&lt;/em&gt; convention.&lt;/p&gt;

&lt;p&gt;The same idea applies when you're using a built-in class such as &lt;code&gt;ArrayList&lt;/code&gt; in Java. If you want to find the size of an &lt;code&gt;ArrayList&lt;/code&gt; object, you'll be expecting to find a method with the name &lt;code&gt;length&lt;/code&gt; or &lt;code&gt;size&lt;/code&gt;.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  More Focus on the Necessary Details
&lt;/h2&gt;

&lt;p&gt;Time is one of the most valuable resources, if not the most valuable. When it comes to software projects, we always want to achieve more in less time. To do that, we've to make repetitive tasks such as naming things much faster. As you can see, following the naming convention of the programming language we use can contribute to that. By doing so, we're reducing the debates among the developers about the best naming convention to follow, because we're following the standard. In other words, we let the developers focus on the necessary details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Convention Over Configuration
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Convention over configuration is a software design paradigm used by software frameworks that attempts to decrease the number of decisions that a developer using the framework is required to make without necessarily losing flexibility. &lt;a href="https://en.wikipedia.org/wiki/Convention_over_configuration" rel="noopener noreferrer"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Simply, &lt;em&gt;Convention Over Configuration&lt;/em&gt; is an approach that is used by modern frameworks. It makes a deal with you. If you follow the conventions that the framework adopts, the framework will automatically do configuration tasks for you. Otherwise, you'll have to do them manually by yourself and lose this advantage.&lt;/p&gt;

&lt;p&gt;For example, the framework may till you to name your classes in the &lt;em&gt;Pascal Case&lt;/em&gt; naming convention &lt;code&gt;User&lt;/code&gt; so that it can automatically create a corresponding table in the database with the name &lt;code&gt;users&lt;/code&gt; for you. If you decided to name the class with another naming convention &lt;code&gt;__user&lt;/code&gt; the framework won't do the mapping task for you and you've to write some configurations to create the table correctly. That's because you didn't follow the conventions that the framework adopts.&lt;/p&gt;

&lt;p&gt;Now you understand why following naming conventions is essential, and although it seems like a trivial task, it really contributes to your projects.&lt;/p&gt;

&lt;p&gt;If you know any other advantage for following naming conventions, please share it with us.&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>programming</category>
      <category>java</category>
      <category>python</category>
    </item>
    <item>
      <title>Soft Deletion in Database</title>
      <dc:creator>Amr Saeed</dc:creator>
      <pubDate>Sat, 26 Dec 2020 18:15:21 +0000</pubDate>
      <link>https://forem.com/amrsaeedhosny/soft-deletion-26jc</link>
      <guid>https://forem.com/amrsaeedhosny/soft-deletion-26jc</guid>
      <description>&lt;p&gt;One of the common problems you may face when you design your database is that you want to delete a row from some table in the database without actually deleting it.&lt;/p&gt;

&lt;p&gt;The name of this concept is called &lt;em&gt;Soft Deletion&lt;/em&gt;. You can apply soft deletion by creating a column called &lt;code&gt;IS_DELETED&lt;/code&gt; in the table you want. This column is of the type &lt;code&gt;Boolean&lt;/code&gt;. If its value is &lt;code&gt;True&lt;/code&gt;, then the row has been deleted or invisible to be accurate.&lt;/p&gt;

&lt;p&gt;It's a straightforward concept to apply, but it comes with costs. You may face problems where you are building complex queries on this table; then later, you discover by coincidence that you forgot to consider this column in your queries from the wrong data they produce. At this moment, you discover that you have to include &lt;code&gt;IS_DELETED&lt;/code&gt; in nearly every single query you build which has this table.&lt;/p&gt;

&lt;p&gt;That's why some people try to avoid this method to apply soft deletion by creating an archive table. When you delete some row from the original table, just insert it into the archive table. Hence, no query mistakes on the original table and no &lt;code&gt;IS_DELETED&lt;/code&gt; overhead.&lt;/p&gt;

&lt;p&gt;Try to avoid soft deletion as much as you can, and try not to use it unless there is a real need to do so. Notice that soft deletion has nothing to do with backups. Backups have to be done regardless you're applying soft deletion or not. They have to be applied periodically, and the period itself depends on your application.&lt;/p&gt;

</description>
      <category>database</category>
      <category>webdev</category>
      <category>java</category>
      <category>csharp</category>
    </item>
    <item>
      <title>Countries Data</title>
      <dc:creator>Amr Saeed</dc:creator>
      <pubDate>Fri, 27 Nov 2020 15:33:29 +0000</pubDate>
      <link>https://forem.com/amrsaeedhosny/countries-data-1i2b</link>
      <guid>https://forem.com/amrsaeedhosny/countries-data-1i2b</guid>
      <description>&lt;p&gt;For the last couple of months, I was working on a large project in which I needed to use the list of countries in both English and Arabic languages. At first glance, I thought finding this list was going to be easy as it looks like a common problem, right? But unfortunately, it wasn't.&lt;/p&gt;

&lt;p&gt;Most of the data I've found weren't organized properly, and it was too hard to extract the necessary data without putting in a huge effort. &lt;/p&gt;

&lt;p&gt;I decided to use some API having this data already, but I faced some problems. Most of the APIs provide you with a limited number of requests per month. If you want more requests, you've to pay more and more. Unfortunately, the free ones weren't reliable.&lt;/p&gt;

&lt;p&gt;Despite all of that, I had two major problems. The first one, we may have situations in which we want to relate some data to some countries in our database with foreign keys. So, the APIs won't be useful, and I have to store the data in the database. For the second one, all the solutions I've found weren't providing Arabic data.&lt;/p&gt;

&lt;p&gt;So, I decided to build the data on my own. With the help of &lt;a href="https://www.geonames.org"&gt;GeoNames&lt;/a&gt; public data and with some extra effort to translate the data and add extra fields, I created &lt;a href="https://github.com/amrsaeedhosny/countries"&gt;amrsaeedhosny/countries&lt;/a&gt; repository on GitHub.&lt;/p&gt;

&lt;h2&gt;
  
  
  Country Data
&lt;/h2&gt;

&lt;p&gt;Each country has the following data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;English Name.&lt;/li&gt;
&lt;li&gt;Arabic Name.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.iso.org/iso-3166-country-codes.html"&gt;ISO 3166-1&lt;/a&gt; Alpha-2 Code.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.iso.org/iso-3166-country-codes.html"&gt;ISO 3166-1&lt;/a&gt; Alpha-3 Code.&lt;/li&gt;
&lt;li&gt;Phone Code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Available File Formats
&lt;/h2&gt;

&lt;p&gt;The countries data are stored in multiple file formats:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CSV:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;English Name&lt;/th&gt;
&lt;th&gt;Arabic Name&lt;/th&gt;
&lt;th&gt;Alpha-2 Code&lt;/th&gt;
&lt;th&gt;Alpha-3 Code&lt;/th&gt;
&lt;th&gt;Phone Code&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Egypt&lt;/td&gt;
&lt;td&gt;مصر&lt;/td&gt;
&lt;td&gt;EG&lt;/td&gt;
&lt;td&gt;EGY&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;JSON:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="nl"&gt;"english_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Egypt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="nl"&gt;"arabic_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"مصر"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="nl"&gt;"alpha2_code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"EG"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="nl"&gt;"alpha3_code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"EGY"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="nl"&gt;"phone_code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"20"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;SQL:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;COUNTRY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ENGLISH_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ARABIC_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ALPHA2_CODE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ALPHA3_CODE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PHONE_CODE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Egypt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'مصر'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'EG'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'EGY'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'20'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Custom File Format
&lt;/h2&gt;

&lt;p&gt;There is also a simple python script that you can use to build your own file format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;csv&lt;/span&gt;

&lt;span class="n"&gt;my_file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"my_file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"w"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'countries.csv'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;csv_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;csv_file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;csv_reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;csv_reader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;my_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;","&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;my_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;my_file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the problem has been solved once, and I can use the data easily whenever I need it. &lt;/p&gt;

&lt;p&gt;By reaching this, you've to know that you're free to use this data in your own projects. Actually, that was the whole point of this article. So, enjoy!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>java</category>
      <category>python</category>
    </item>
    <item>
      <title>How to Choose Your First Programming Language?</title>
      <dc:creator>Amr Saeed</dc:creator>
      <pubDate>Sun, 15 Nov 2020 16:13:18 +0000</pubDate>
      <link>https://forem.com/amrsaeedhosny/how-to-choose-your-first-programming-language-6ai</link>
      <guid>https://forem.com/amrsaeedhosny/how-to-choose-your-first-programming-language-6ai</guid>
      <description>&lt;p&gt;One of the questions that are frequently asked by new programmers is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are tons of programming languages, how to make sure I am starting with the most appropriate one?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer to this question has been a debate for a long time. Some experts may tell you to start with C++. Other experts may prefer Python over C++ for beginners. You also may hear different opinions from people in the industry to start with a programming language like C# or Java.&lt;/p&gt;

&lt;p&gt;“I’m confused, is there only one correct approach?”&lt;/p&gt;

&lt;p&gt;The answer is no. The fact is that most experts prefer some approaches based on their personalities and perspectives. You’ll find that most of the people who suggest C++ tend to start with the details first. Learning C++ first is like trying to drive a manual car. On the contrary, python is very easy to learn, so some people prefer to start with it for that reason. It’s like trying to drive an automatic car. Some other people like to learn programming through projects as they get bored so quickly. So, they try to start with a language like Java to create Android applications or start with a language like PHP to create web applications.&lt;/p&gt;

&lt;p&gt;Now, after knowing the secret behind those different opinions, we’ve to choose your first programming language. To do that, we’re going to divide people into three groups. You’ve to determine which group you fall into and start with one of the suggested languages under this group.&lt;/p&gt;

&lt;p&gt;The three groups are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You’re a computer science student or have a long-term plan.&lt;/li&gt;
&lt;li&gt;You want to get a job as soon as possible or have a short-term plan.&lt;/li&gt;
&lt;li&gt;You want to do programming as a hobby or to teach your child.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  You’re a Computer Science Student or Have a Long-Term Plan
&lt;/h2&gt;

&lt;p&gt;If you’re a computer science student or a person who decided to enter this field with a long-term plan that can take more than one year, your objective has to become focusing on the basics of computer science. You’ve to become a person who can learn any technology easily regardless of its complexity. The languages that help you to achieve that objective are languages like C++, Java, and C#. Those languages help you to learn the basics of computer science such as Object-Oriented Programming, Data Structures, and Algorithms in a detailed manner. Learning those types of programming languages makes it very easy for you to learn modern programming languages such as Python in the future. That’s because you’ve already started with the hardest and most detailed ones.&lt;/p&gt;

&lt;p&gt;Personally, I prefer C++, and it’s the one I’ve started with when I was in college. It’s fast, flexible, and has some important features that don’t exist in many languages. It also teaches you several concepts that are related to memory. But it’s okay if you’re a student whose college decided to start with Java or C#, you won’t miss a lot as they have similar syntax.&lt;/p&gt;

&lt;h2&gt;
  
  
  You Want to Get a Job as Soon as Possible or Have a Short-Term Plan
&lt;/h2&gt;

&lt;p&gt;If your first priority is to get a job as soon as possible or you have a very tight time plan which may take less than a year, the best option for you is to study a language which is related to some track of your interest.&lt;/p&gt;

&lt;p&gt;For example, if you’re interested in Mobile Development, you can learn languages like Java or Kotiln. If you’re interested in Web Development, you can learn languages like JavaScript, PHP, Python, or Ruby. Notice that languages like JavaScript or PHP are not the most suitable languages to learn programming concepts such as OOP and Data Structures comparing to C++ or Java. So, when you start learning any one of them, you’ve to make sure you’re following the best practices and not writing a bad code.&lt;/p&gt;

&lt;p&gt;Also, you’ve to know that even if you’re focusing on a specific track, you’ve to focus on the programming concepts as much as you can. The difference between you and the person who decided to follow a long-term plan is that you’re learning the concepts through your specialization directly. You may not face all the programming concepts they will but, you’ve to make sure you understand the concepts you study. And if you have time in the future, try to learn those concepts separately.&lt;/p&gt;

&lt;h2&gt;
  
  
  You Want to Do Programming as a Hobby or to Teach Your Child
&lt;/h2&gt;

&lt;p&gt;If you want to do programming as a hobby or you want to teach your child programming, python is the clear winner for that purpose. The basics of this programming language are very easy to grasp. It will help you to learn the fundamentals of programming without facing the syntax complexity of other programming languages such as C++.&lt;/p&gt;

&lt;p&gt;This is an example of a code written in C++ which prints “Hello World” on the screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the equivalent code but written in python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that the Python code is very short compared to the C++ one.&lt;/p&gt;

&lt;p&gt;Now, we’ve reached the end of this article. But, you’ve to know that It’s always preferable to have a long-term plan so that you can practice the different concepts. But if you don’t have this option, try to pick one of the two other plans and work hard to achieve what you want.&lt;/p&gt;

&lt;p&gt;One last note, these are some useful resources I personally tried. They can help you to learn the programming language you want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/user/thenewboston"&gt;The New Boston&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.codecademy.com"&gt;Codeacademy&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.sololearn.com"&gt;SoloLearn&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.tutorialspoint.com"&gt;Tutorialspoint&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.w3schools.com"&gt;W3Schools&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.udemy.com"&gt;Udemy&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.coursera.org"&gt;Coursera&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.edx.org"&gt;Edx&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And always remember that “practice makes perfect”.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>cpp</category>
      <category>python</category>
    </item>
    <item>
      <title>Liskov Substitution Principle</title>
      <dc:creator>Amr Saeed</dc:creator>
      <pubDate>Sat, 14 Nov 2020 16:00:19 +0000</pubDate>
      <link>https://forem.com/amrsaeedhosny/liskov-substitution-principle-ofc</link>
      <guid>https://forem.com/amrsaeedhosny/liskov-substitution-principle-ofc</guid>
      <description>&lt;p&gt;You know, when I heard the name of the Liskov Substitution Principle for the first time, I thought it would be the most difficult one in SOLID principles. The principle’s name sounded very strange to me. I judged the book by its cover, and I convinced myself that I wouldn’t grasp it. Eventually, it turned out that it was one of the easiest and straight forward principles in SOLID principles.&lt;/p&gt;

&lt;p&gt;So, let’s start our journey by putting a simple definition for the Liskov Substitution Principle:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It’s the ability to replace any object of a parent class with any object of one of its child classes without affecting the correctness of the program.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I know it sounds strange to you but let’s break it into pieces. Suppose we have a program that has a parent class. The parent class has some child classes who inherit from. If we decided to create some objects from the parent class in our program, we’ve to be able to replace any one of them with any object of any child class, and the program should work as expected without any errors.&lt;/p&gt;

&lt;p&gt;In other words, we have to be able to substitute objects of a parent class with objects of child classes without causing the program to break. That’s why the principle has the keyword ‘substitution’ in its name. As for Liskov, it’s the name of the scientist Barbara Liskov who developed a scientific definition for that principle. You can read this article Liskov substitution principle on Wikipedia for more information about that definition.&lt;/p&gt;

&lt;p&gt;Now, let’s try to link the definition we’ve just discussed to a famous example to understand the principle.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Bird&lt;/code&gt; is a class which has the two methods &lt;code&gt;eat()&lt;/code&gt; and &lt;code&gt;fly()&lt;/code&gt;. It represents a base class that any type of bird can extend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&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;eat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I can eat."&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;fly&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I can fly."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Swan&lt;/code&gt; is a type of bird that can eat and fly. Hence, it has to extend the &lt;code&gt;Bird&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Swan&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&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;eat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"OMG! I can eat pizza!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&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;fly&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I believe I can fly!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;code&gt;Main&lt;/code&gt; is the main class of our program which contains its logic. It has two methods, &lt;code&gt;letBirdsFly(List&amp;lt;Bird&amp;gt; birds)&lt;/code&gt; and &lt;code&gt;main(String[] args)&lt;/code&gt;. The first method takes a list of birds as a parameter and invokes their fly methods. The second one creates the list and passes it to the first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&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;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;letBirdsFly&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="nl"&gt;bird:&lt;/span&gt; &lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;bird&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fly&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;static&lt;/span&gt; &lt;span class="kt"&gt;void&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="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;birds&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&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;Bird&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="n"&gt;letBirdsFly&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program simply creates a list of birds and lets them fly. If you try to run this program, it will output the following statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I can fly.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let’s try to apply the definition of this principle to our main method and see what happens. We are going to replace the &lt;code&gt;Bird&lt;/code&gt; object with the &lt;code&gt;Swan&lt;/code&gt; object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&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="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;birds&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;       
    &lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&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;Swan&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;letBirdsFly&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we try to run the program after applying the changes, it will output the following statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I believe I can fly!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that the principle applies to our code perfectly. The program works as expected without any errors or problems. But, what if we tried to extend the &lt;code&gt;Bird&lt;/code&gt; class by a new type of bird that cannot fly?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&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;eat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Can I eat taco?"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&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;fly&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;UnsupportedOperationException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Help! I cannot fly!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;We can check whether the principle still applied to our code or not by adding a &lt;code&gt;Penguin&lt;/code&gt; object to the list of birds and run the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&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="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;birds&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;       
    &lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&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;Swan&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&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;Penguin&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="n"&gt;letBirdsFly&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;birds&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I believe I can fly!
Exception in thread "main" 
java.lang.UnsupportedOperationException: Help! I cannot fly!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ops! it didn’t work as expected!&lt;/p&gt;

&lt;p&gt;We can see that with the &lt;code&gt;Swan&lt;/code&gt; object, the code worked perfectly. But with the &lt;code&gt;Penguin&lt;/code&gt; object, the code threw &lt;code&gt;UnsupportedOperationException&lt;/code&gt;. This violates the Liskov Substitution Principle as the &lt;code&gt;Bird&lt;/code&gt; class has a child that didn’t use inheritance correctly, hence caused a problem. The &lt;code&gt;Penguin&lt;/code&gt; tries to extend the flying logic, but it can’t fly!&lt;/p&gt;

&lt;p&gt;We can fix this problem using the following if check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;letBirdsFly&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="nl"&gt;bird:&lt;/span&gt; &lt;span class="n"&gt;birds&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!(&lt;/span&gt;&lt;span class="n"&gt;bird&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;bird&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fly&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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this solution is considered a bad practice, and it violates the Open-Closed Principle. Imagine if we add another three types of birds that cannot fly. The code is going to become a mess. Notice also that one of the definitions for the Liskov Substitution Principle, which is developed by Robert C. Martin is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is not the case with our solution, as we’re trying to know the type of the &lt;code&gt;Bird&lt;/code&gt; object to avoid the misbehavior of the non-flying birds.&lt;/p&gt;

&lt;p&gt;One of the clean solutions to solve this issue and refollow the principle is to separate the flying logic in another class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&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;eat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I can eat."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&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;fly&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I can fly."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Swan&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&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;eat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"OMG! I can eat pizza!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&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;fly&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I believe I can fly!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Can I eat taco?"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Now we can edit the &lt;code&gt;letBirdsFly&lt;/code&gt; method to support flying birds only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&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;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;letBirdsFly&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;FlyingBird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;flyingBirds&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="nl"&gt;flyingBird:&lt;/span&gt; &lt;span class="n"&gt;flyingBirds&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;flyingBird&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fly&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;static&lt;/span&gt; &lt;span class="kt"&gt;void&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="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;FlyingBird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;flyingBirds&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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;FlyingBird&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;();&lt;/span&gt;     
        &lt;span class="n"&gt;flyingBirds&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&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;Swan&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="n"&gt;letBirdsFly&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;flyingBirds&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason we forced the &lt;code&gt;letBirdsFly&lt;/code&gt; method to accept flying birds only is to guarantee that any substitution for the &lt;code&gt;FlyingBird&lt;/code&gt; will be able to fly. Now the program works as expected and outputs the following statements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I believe I can fly!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that the Liskov Substitution Principle is about using the inheritance relationship in the correct manner. You’ve to create subtypes of some parent if and only if they’re going to implement its logic properly without causing any problems.&lt;/p&gt;

&lt;p&gt;We’ve reached the end of this journey, but we still have another two principles to cover. So take your time reading about this principle and make sure that you understand it before moving on. Stay tuned!&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Open Closed Principle</title>
      <dc:creator>Amr Saeed</dc:creator>
      <pubDate>Sat, 14 Nov 2020 10:18:20 +0000</pubDate>
      <link>https://forem.com/amrsaeedhosny/open-closed-principle-2deh</link>
      <guid>https://forem.com/amrsaeedhosny/open-closed-principle-2deh</guid>
      <description>&lt;p&gt;Now it’s the time to start another journey with the second SOLID principle, the Open-Closed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open-Closed principle states:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What does that even mean? How could a software entity be opened and closed at the same time!&lt;/p&gt;

&lt;p&gt;Let’s start explaining by using a real-world scenario to illustrate the problem, then moving into the solution and the technical details.&lt;/p&gt;

&lt;p&gt;Photo viewers are used everywhere, literally. You can find them on social media platforms such as facebook or twitter. Also, you can find them on your smartphone as gallery applications. They’re considered a key feature in many apps.&lt;/p&gt;

&lt;p&gt;If you decided to develop a photo viewer with your own, users would expect it to open photos of any type. It could be a JPEG photo. It might be a PNG. Sometimes it’s not the case that a specific type is supported by your photo viewer. Hence, you need to change the source code of your photo viewer to support that type.&lt;/p&gt;

&lt;p&gt;How to guarantee that a new type added to your photo viewer won’t affect an already existing one? You may edit your application to support a GIF type, but it crashes the application when you open a PNG photo. How to prevent that from happening? And how to guarantee enough flexibility to add and remove other types in the future without affecting each other.&lt;/p&gt;

&lt;p&gt;The solution is the Open-Closed principle, as you certainly would guess.&lt;/p&gt;

&lt;p&gt;Recalling the PhotoViewer class from the Single Responsibility article with a slight change.&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;PhotoViewer&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;openPhoto&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Open JPEG photo!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you wrote your photo viewer class, and you’re quite sure that it has to open JPEG photos only. Later you realized that your users are continuously trying to view PNG photos. And because your app doesn’t support it, it raises an error. So, you decided to edit the source code so that it supports PNG.&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;PhotoViewer&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;openPhoto&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"JPEG"&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;      
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Open JPEG photo!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;    
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"PNG"&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Open PNG photo!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;    
        &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Photo type is not supported!"&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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, what are the problems of your code after making the changes?&lt;/p&gt;

&lt;p&gt;What if you decided to add another type? Which is 100% going to happen now or then?&lt;/p&gt;

&lt;p&gt;At that moment, you would add another else if statement. Adding a new type after another makes open photo function too large as a function. And because all types exist at the same place, removing some lines of code from one type may crash the others.&lt;/p&gt;

&lt;p&gt;So the code is open for extension, which means you can extend it by other features. But not closed for modification, because every time you add a type you modify the open photo function lines of code. Many changes at the same block of code for different reasons is an indication of bad design.&lt;/p&gt;

&lt;p&gt;Okay, I know you might have thought about separating JPEG and PNG into different functions at the photo viewer class. I’m with you, let’s try it!&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;PhotoViewer&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;openJpegPhoto&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;    
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Open JPEG photo!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;openPngPhoto&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Open PNG photo!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your code is open for extension. You can add new types easily by adding new functions. But the same problem still exists. The solution is not 100% closed for modification, as the class lines of code are going to be affected by the changes. Also notice that adding another type to the photo viewer class is going to make it larger with more responsibilities. This violates the Single Responsibility Principle.&lt;/p&gt;

&lt;p&gt;I know you’re tired, but believe me, it worth it.&lt;/p&gt;

&lt;p&gt;The solution is about using Object-Oriented Programming features by using a generic interface to represent a generic photo type that any new type can implement.&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;interface&lt;/span&gt; &lt;span class="nc"&gt;Photo&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;open&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;PhotoJpeg&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Photo&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;    
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Open JPEG photo!"&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;class&lt;/span&gt; &lt;span class="nc"&gt;PhotoPng&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Photo&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;    
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Open PNG photo!"&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;class&lt;/span&gt; &lt;span class="nf"&gt;PhotoViewer&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;  
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;openPhoto&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Photo&lt;/span&gt; &lt;span class="n"&gt;photo&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;    
        &lt;span class="n"&gt;photo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;open&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see that adding a new type won’t change a single line of code from the source code. You only need to implement the photo interface with the new type and send the photo to the photo viewer to be opened.&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;PhotoGif&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Photo&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;    
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Open GIF photo!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The solution is 100% open for extension and closed for modification. Notice that the single responsibility principle is also applied.&lt;/p&gt;

&lt;p&gt;Now you know that Open-Closed means that your code is open to be extended by new functionalities and closed in terms of changing the source code, but appending to it.&lt;/p&gt;

&lt;p&gt;Wait for a new journey with the third SOLID principle, Liskov Substitution!&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>Single Responsibility Principle</title>
      <dc:creator>Amr Saeed</dc:creator>
      <pubDate>Fri, 13 Nov 2020 17:14:41 +0000</pubDate>
      <link>https://forem.com/amrsaeedhosny/single-responsibility-principle-3ajo</link>
      <guid>https://forem.com/amrsaeedhosny/single-responsibility-principle-3ajo</guid>
      <description>&lt;p&gt;If you are a passionate developer who’s interested in learning how to write high-quality software solutions, then you came into the right place, my friend.&lt;/p&gt;

&lt;p&gt;One of the most common and effective software engineering principles is SOLID principles introduced by Robert C. Martin who is known as Uncle Bob. They are intended to make software designs more understandable, flexible, and maintainable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SOLID&lt;/strong&gt; is an acronym for 5 design principles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;S&lt;/strong&gt;ingle responsibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O&lt;/strong&gt;pen-closed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;L&lt;/strong&gt;iskov substitution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I&lt;/strong&gt;nterface segregation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;D&lt;/strong&gt;ependency inversion.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this article, we’re going to talk about the first and easiest one which is the &lt;strong&gt;Single Responsibility&lt;/strong&gt; principle.&lt;/p&gt;

&lt;p&gt;Single responsibility principle says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“A class should have only one reason to change”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But what does the phrase &lt;strong&gt;“only one reason”&lt;/strong&gt; mean!&lt;/p&gt;

&lt;p&gt;You know, I believe examples are the best teacher. Let’s read the following code snippet in order to understand.&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;MusicPlayer&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;playMusic&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
      &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Let the fun begins!"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
   &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;openPhoto&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
      &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Really! should I open the photo now?"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we have a music player class that can play music and open photos.&lt;/p&gt;

&lt;p&gt;Try to imagine that this class exists in some project at XYZ company who you work for. Khaled is a photographer at XYZ and uses the open photo function to open photos. Ali is another employee who is addicted to music and uses the play music function.&lt;/p&gt;

&lt;p&gt;If Khaled decides to change the way photos are opened you are going to open MusicPlayer class and change the openPhoto function. Similarly, if Ali, a totally different employee, and maybe from a different department decided so. You will do the same but with the playMusic function.&lt;/p&gt;

&lt;p&gt;We gave the class two different reasons to change and they are not related to each other. One for music and the other for photos. Besides, what is the relationship between MusicPlayer class and openPhoto function! I know you’ve thought openPhoto shouldn’t be there.&lt;/p&gt;

&lt;p&gt;The class looks like a student who works at two totally different jobs. In software engineering, this is a bad practice. The more a class can do things the more it is susceptible to change, hence more bugs and problems.&lt;/p&gt;

&lt;p&gt;Not only that, try to imagine if we decided to remove some lines of code from playMusic to find that openPhoto crashes. Different things in one place may affect each other. Bugs everywhere my friend, bugs everywhere!&lt;/p&gt;

&lt;p&gt;So let’s separate the responsibilities and give a class one reason to change.&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;MusicPlayer&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;playMusic&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
      &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Let the fun begins!"&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;class&lt;/span&gt; &lt;span class="nc"&gt;PhotoViewer&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;openPhoto&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
      &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"What about now should I open it?"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we decided to change the photo functionality at any time we won’t touch the music functionality and vice versa. Besides the functions are know logically related to the names of their classes.&lt;/p&gt;

&lt;p&gt;In practice, it’s very hard to give the class only one reason to change with many functions exist, but what you can do is to keep its functions related as much as you can. This is generally known as cohesion in software engineering. Classes with high cohesion are an indication of good design.&lt;/p&gt;

&lt;p&gt;You can apply a single responsibility principle also on functions. A function has to do one specific task no more no less.&lt;/p&gt;

&lt;p&gt;Always ask yourself when you write a function what does it do? if you say it does something &lt;strong&gt;“and”&lt;/strong&gt; something. &lt;strong&gt;And&lt;/strong&gt; here means you’re doing it wrong.&lt;/p&gt;

&lt;p&gt;This is the end of our journey for now.&lt;/p&gt;

&lt;p&gt;Soon we’ll start another journey with the open-closed principle. Just wait for it!&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>programming</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
