<?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: Elle Hallal 👩🏽‍💻</title>
    <description>The latest articles on DEV Community by Elle Hallal 👩🏽‍💻 (@ellehallal).</description>
    <link>https://dev.to/ellehallal</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%2F233764%2Fda7e40d7-942e-4324-99a2-3f6a488b4d1a.jpeg</url>
      <title>DEV Community: Elle Hallal 👩🏽‍💻</title>
      <link>https://dev.to/ellehallal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ellehallal"/>
    <language>en</language>
    <item>
      <title>What Are Interfaces, Abstract and Concrete Classes?</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Wed, 29 May 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/what-are-interfaces-abstract-and-concrete-classes-ahn</link>
      <guid>https://dev.to/ellehallal/what-are-interfaces-abstract-and-concrete-classes-ahn</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;


&lt;h2&gt;
  
  
  What Are Interfaces?
&lt;/h2&gt;

&lt;p&gt;Interfaces are used to standardise the way a particular set of classes are used. An interface specifies common behaviour, which needs to be implemented by the classes. It can be described as a blueprint of a class.&lt;/p&gt;

&lt;p&gt;An interface decouples &lt;em&gt;what&lt;/em&gt; needs to be implemented from &lt;em&gt;how&lt;/em&gt; it is implemented. It is not concerned with how the behaviour is implemented.&lt;/p&gt;

&lt;p&gt;Every method declared in an interface will need to be included in a class which implements it. The body of methods in an interface are empty, and therefore classes implementing it need to override the methods to add a method body.&lt;/p&gt;

&lt;p&gt;An interface contains methods and constants, which are automatically public andabstract. As a result, including these keywords is optional.&lt;/p&gt;

&lt;p&gt;For example, here’s the &lt;code&gt;play&lt;/code&gt; method in the &lt;code&gt;Pet&lt;/code&gt; interface. It has an empty method body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Pet&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;play&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;Interfaces have an &lt;em&gt;is-a&lt;/em&gt; relationship. In the example below where &lt;code&gt;Cat&lt;/code&gt; is implementing &lt;code&gt;Pet&lt;/code&gt;, Cat &lt;em&gt;is-a&lt;/em&gt; &lt;code&gt;Pet&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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Pet&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&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;Cat&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;name&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="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;play&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="n"&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;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is playing with human"&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;Cat&lt;/code&gt; has overridden the &lt;code&gt;play&lt;/code&gt; method in the &lt;code&gt;Pet&lt;/code&gt; interface, with its own &lt;code&gt;play&lt;/code&gt;method.&lt;/p&gt;

&lt;p&gt;Below is another example, using an interface called &lt;code&gt;MediaPlayer&lt;/code&gt;. The classes &lt;code&gt;MusicPlayer&lt;/code&gt; and &lt;code&gt;VideoPlayer&lt;/code&gt; implement the interface.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interface - MediaPlayer&lt;/strong&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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;MediaPlayer&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;play&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;stop&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;currentlyPlaying&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;strong&gt;Class - MusicPlayer&lt;/strong&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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MusicPlayer&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;MediaPlayer&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;songName&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;boolean&lt;/span&gt; &lt;span class="n"&gt;isPlaying&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MusicPlayer&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;songName&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;songName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;songName&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;play&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;isPlaying&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="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;stop&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;isPlaying&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="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;currentlyPlaying&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;isPlaying&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="n"&gt;songName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is currently playing."&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;"Nothing is playing."&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;&lt;strong&gt;Class - VideoPlayer&lt;/strong&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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;VideoPlayer&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;MediaPlayer&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;videoName&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;boolean&lt;/span&gt; &lt;span class="n"&gt;isPlaying&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;VideoPlayer&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;videoName&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;videoName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;videoName&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;play&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;"Playing..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;isPlaying&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="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;stop&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;"Stopped"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;isPlaying&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="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;currentlyPlaying&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;isPlaying&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="n"&gt;videoName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" is currently playing."&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="n"&gt;videoName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;"is not playing. Press play"&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;&lt;code&gt;MusicPlayer&lt;/code&gt; and &lt;code&gt;VideoPlayer&lt;/code&gt; have implemented the &lt;code&gt;play&lt;/code&gt;, &lt;code&gt;stop&lt;/code&gt;, and &lt;code&gt;currentlyPlaying&lt;/code&gt; methods declared in &lt;code&gt;MediaPlayer&lt;/code&gt;, by overriding them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other key points
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An interface cannot be instantiated&lt;/li&gt;
&lt;li&gt;A single class can implement more than one interface&lt;/li&gt;
&lt;li&gt;Multiple classes can implement the same interface&lt;/li&gt;
&lt;li&gt;An interface can extend another interface&lt;/li&gt;
&lt;li&gt;Another example of an interface is &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/List.html"&gt;List&lt;/a&gt; in Java.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What Are Abstract Classes?
&lt;/h2&gt;

&lt;p&gt;The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.&lt;/p&gt;

&lt;p&gt;It should be used when a class contains some implementation code, which all subclasses can use, such as a method with a body. Like interfaces, an abstract class can also be referred to as a blueprint.&lt;/p&gt;

&lt;p&gt;An abstract class contains at least one abstract method. An abstract method is a method with an empty body, just like the methods in an interface.&lt;/p&gt;

&lt;p&gt;The difference is in an abstract class, the abstract keyword needs to be used when declaring the class and abstract methods. Here’s an 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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;speak&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;In addition, methods within this class type can have any visibility, where as in an interface, methods are public only.&lt;/p&gt;

&lt;p&gt;Abstract classes can also contain non-abstract methods, meaning the method body is defined. For example, here’s the abstract class, &lt;code&gt;Animal&lt;/code&gt; , with the non-abstract method age:&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;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;abstract&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;speak&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;age&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;age&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 am "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;" years old"&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;Abstract classes cannot be instantiated, and classes which extend from it need to override the abstract methods to provide implementation. Below is an example of a class which extends from the &lt;code&gt;Animal&lt;/code&gt; class, overriding the abstract method speak:&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;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Animal&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;speak&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="n"&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;"Woof!"&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;An abstract class can implement an interface. In addition, a subclass of an abstract class usually provides implementations of all abstract methods from the parent class. If not, the subclass must also be declared as abstract.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Are Concrete Classes?
&lt;/h2&gt;

&lt;p&gt;A concrete class implements all of its inherited methods and state from an interface and/or an abstract class. Unlike an interface or abstract class, a concrete class can be instantiated.&lt;/p&gt;

&lt;p&gt;It demonstrates the implementation of a blueprint. Any abstract methods are overridden, to include a method body.&lt;/p&gt;

&lt;p&gt;A concrete class can implement multiple interfaces, but can only inherit from one parent class.&lt;/p&gt;

&lt;p&gt;In the example below, the &lt;code&gt;Dog&lt;/code&gt; class inherits its methods from the interface, &lt;code&gt;Pet&lt;/code&gt;, and the abstract class, &lt;code&gt;Animal&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oMCWSbou--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/9w7cCGl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oMCWSbou--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/9w7cCGl.png" alt="" width="720" height="502"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another example of a concrete class is an ArrayList in Java. It implements the methods in the &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html"&gt;List&lt;/a&gt; interface. It also extends from the &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/AbstractList.html"&gt;AbstractList&lt;/a&gt; class. In addition, it implements other interfaces, such as Iterable and Cloneable.&lt;/p&gt;

</description>
      <category>oop</category>
      <category>interfaces</category>
      <category>classes</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>What Is The Liskov Substitution Principle?</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Fri, 17 May 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/what-is-the-liskov-substitution-principle-31bj</link>
      <guid>https://dev.to/ellehallal/what-is-the-liskov-substitution-principle-31bj</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;


&lt;h2&gt;
  
  
  The Principle Definition
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Let φ(x) be a property provable about objects x of type T. Then φ(y) should be true for objects y of type S where S is a subtype of T.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s great, but what does it mean 🤷🏽‍♀️? In simpler terms, a child class should be able to substitute a parent class, without any unexpected behaviour. It ensures inheritance is being used correctly.&lt;/p&gt;


&lt;h2&gt;
  
  
  An example of adhering to the Liskov Substitution Principle
&lt;/h2&gt;

&lt;p&gt;Below is the class &lt;code&gt;Animal&lt;/code&gt;. When the &lt;code&gt;speak&lt;/code&gt; method is called, a string is expected to be returned. The method does not have any parameters.&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;Animal&lt;/span&gt;
    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;speak&lt;/span&gt;
    &lt;span class="s"&gt;""&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below are subclasses &lt;code&gt;Cat&lt;/code&gt; and &lt;code&gt;Dog&lt;/code&gt;. Both inherit from &lt;code&gt;Animal&lt;/code&gt; and have a &lt;code&gt;speak&lt;/code&gt; method.&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;Cat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;
    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;speak&lt;/span&gt;
    &lt;span class="s"&gt;"Meow!"&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;
    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;speak&lt;/span&gt;
    &lt;span class="s"&gt;"Woof!"&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;


&lt;span class="n"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;

&lt;span class="n"&gt;cat&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;speak&lt;/span&gt;  &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="s"&gt;"Meow!"&lt;/span&gt;
&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;speak&lt;/span&gt;  &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="s"&gt;"Woof!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although calling the speak method on &lt;code&gt;Cat&lt;/code&gt; returns 'Meow!' and 'Woof!' for &lt;code&gt;Dog&lt;/code&gt;, a string is returned in both cases. In addition, no arguments are required. As a result, instances of these subclasses can be substituted where an instance of &lt;code&gt;Animal&lt;/code&gt; is used.&lt;/p&gt;




&lt;h2&gt;
  
  
  Violation of the Liskov Substitution Principle
&lt;/h2&gt;

&lt;p&gt;Below is the class &lt;code&gt;Jellyfish&lt;/code&gt;, which is a subclass of &lt;code&gt;Animal&lt;/code&gt;. Its speak method has a parameter name and returns a string.&lt;/p&gt;

&lt;p&gt;Although this method returns a string, it needs a name as an argument when called. As the parent class’ &lt;code&gt;speak&lt;/code&gt; method doesn’t require a name argument, an instance of the &lt;code&gt;Animal&lt;/code&gt; class cannot be substituted with an instance of the &lt;code&gt;Jellyfish&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;class&lt;/span&gt; &lt;span class="nc"&gt;Jellyfish&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;
    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;speak&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="s"&gt;"#{name} cannot speak"&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;jellyfish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Jellyfish&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;jellyfish&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;speak&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jelly"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="s"&gt;"Jelly cannot speak"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition, if the speak method of a subclass returned anything other than a string, this would also violate the principle.&lt;/p&gt;




&lt;h2&gt;
  
  
  In conclusion
&lt;/h2&gt;

&lt;p&gt;To adhere to the Liskov Substitute Principle, a child class should be able to substitute a parent class, without any unexpected behaviour. This is to ensure inheritance is being used correctly.&lt;/p&gt;




&lt;h3&gt;
  
  
  Helpful resources
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Liskov_substitution_principle"&gt;Liskov substitution principle - Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://thoughtbot.com/blog/back-to-basics-polymorphism-and-ruby"&gt;Back to Basics: Polymorphism and Ruby&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>oop</category>
      <category>solid</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>What Is The Dependency Inversion Principle?</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Tue, 30 Apr 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/what-is-the-dependency-inversion-principle-2mjb</link>
      <guid>https://dev.to/ellehallal/what-is-the-dependency-inversion-principle-2mjb</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;



&lt;p&gt;This is a quick blog on my understanding of the dependency inversion principle. There are still elements I am unsure about, so please feel free to leave feedback.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is the Dependency Inversion Principle?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;High-level modules should not depend on low-level modules. Both should depend on abstractions.Abstractions should not depend on details. Details should depend on abstractions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;— Robert C. Martin&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;That’s great, but what does it mean? 🤷🏽‍♀️ I’ll demonstrate my understanding by using a class in my &lt;a href="https://github.com/ellehallal/ruby-tic-tac-toe"&gt;Tic Tac Toe application&lt;/a&gt; as an example.&lt;/p&gt;
&lt;h2&gt;
  
  
  Dependency Inversion in Tic Tac Toe
&lt;/h2&gt;

&lt;p&gt;In the application, there’s a class called &lt;code&gt;GameFactory&lt;/code&gt;. The purpose of &lt;code&gt;GameFactory&lt;/code&gt; is to create an instance of the &lt;code&gt;Game&lt;/code&gt; class with the specified players and a board.&lt;/p&gt;

&lt;p&gt;Here’s a condensed version of the 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;class&lt;/span&gt; &lt;span class="nc"&gt;GameFactory&lt;/span&gt;
    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player_factory&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@player_factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;player_factory&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;new_game&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;player1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@player_factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create_player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'x'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;player2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@player_factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create_player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'o'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;Game&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;player1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;player2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;new_game&lt;/code&gt; method, new instances of the &lt;code&gt;Board&lt;/code&gt; and &lt;code&gt;Game&lt;/code&gt; classes are created within it. However, this violates the Dependency Inversion Principle.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s wrong with it?
&lt;/h3&gt;

&lt;p&gt;The high-level class &lt;code&gt;GameFactory&lt;/code&gt; is dependent on the low level classes &lt;code&gt;Board&lt;/code&gt; and &lt;code&gt;Game&lt;/code&gt;. As a result, they are tightly coupled. A change in a low-level class will affect the high-level class.&lt;/p&gt;

&lt;p&gt;If the name of the &lt;code&gt;Board&lt;/code&gt; or &lt;code&gt;Game&lt;/code&gt; class was changed, the &lt;code&gt;new_game&lt;/code&gt; method within &lt;code&gt;GameFactory&lt;/code&gt; wouldn’t work. As a result, it would need to be amended to accommodate the renamed classes.&lt;/p&gt;

&lt;p&gt;If sub classes of &lt;code&gt;Board&lt;/code&gt; and &lt;code&gt;Game&lt;/code&gt; were to be used to create a new game, (for example, &lt;code&gt;BestBoard&lt;/code&gt; and &lt;code&gt;FunGame&lt;/code&gt;) the &lt;code&gt;new_game&lt;/code&gt; method would need to be changed again to accommodate this.&lt;/p&gt;

&lt;p&gt;A method to resolve the above issues is to pass the classes into &lt;code&gt;GameFactory&lt;/code&gt;'s constructor:&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;GameFactory&lt;/span&gt;
    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;player_factory&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;game&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@player_factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;player_factory&lt;/span&gt;
    &lt;span class="nd"&gt;@board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;
    &lt;span class="nd"&gt;@game&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;game&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;

    &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;new_game&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;player1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@player_factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create_player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'x'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;player2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@player_factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;create_player&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'o'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@game&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;player1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;player2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whatever is passed in as board and game during initialisation, becomes &lt;code&gt;@board&lt;/code&gt; and &lt;code&gt;@game&lt;/code&gt; within &lt;code&gt;GameFactory.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the names of the &lt;code&gt;Board&lt;/code&gt; and &lt;code&gt;Game&lt;/code&gt; classes were to change, initialising &lt;code&gt;GameFactory&lt;/code&gt; with the renamed classes would not affect &lt;code&gt;GameFactory&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If subclasses of &lt;code&gt;Board&lt;/code&gt; and &lt;code&gt;Game&lt;/code&gt; (for example, &lt;code&gt;BestBoard&lt;/code&gt; and &lt;code&gt;FunGame&lt;/code&gt;) were used to initialise an instance of &lt;code&gt;GameFactory&lt;/code&gt;, this would not affect how &lt;code&gt;new_game&lt;/code&gt; functions.&lt;/p&gt;

&lt;p&gt;In conclusion, my understanding is initialising a specific class, or classes within another results in tight coupling. Being able to inject the classes via the constructor, helps to make the classes loosely coupled.&lt;/p&gt;




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

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://www.getlaura.com/dependency-inversion-principle-in-ruby/"&gt;Dependency Inversion Principle in Ruby&lt;/a&gt;&lt;a href="https://www.netguru.com/codestories/solid-5-dip"&gt;SOLID Principles #5 - Dependency Inversion Principle&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>oop</category>
      <category>solid</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>Creating An Unbeatable Computer Player Using Minimax</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Fri, 26 Apr 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/creating-an-unbeatable-computer-player-using-minimax-14e1</link>
      <guid>https://dev.to/ellehallal/creating-an-unbeatable-computer-player-using-minimax-14e1</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;



&lt;p&gt;This week, I have been working on adding an unbeatable computer (UC) player to my &lt;a href="https://github.com/ellehallal/ruby-tic-tac-toe"&gt;Ruby Tic Tac Toe game&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this blog, I will share my understanding of recursion and minimax, and my first attempt at using it to create an unbeatable computer player. In order to implement a UC Player I needed to implement the MiniMax algorithm, which also meant I needed to use recursion.&lt;/p&gt;


&lt;h2&gt;
  
  
  The computer player
&lt;/h2&gt;
&lt;h3&gt;
  
  
  The beatable computer player
&lt;/h3&gt;

&lt;p&gt;Before working towards making it unbeatable, the computer player selected a move without a strategy. It simply picked an available space on the Tic Tac Toe board at random.&lt;/p&gt;
&lt;h3&gt;
  
  
  The unbeatable computer (UC) player
&lt;/h3&gt;

&lt;p&gt;The UC player should know the available squares on the board, assess all of the possible outcomes and as a result, choose the best move.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is considered to be the best move?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The UC player’s aim is to win. If this is not possible, the next best outcome for the UC is for the game to be a tie. The opponent winning the game is not an option. A move should be selected to prevent an opponent from winning, subsequently resulting in a win or a tie for the UC player.&lt;/p&gt;


&lt;h2&gt;
  
  
  Recursive functions and Minimax
&lt;/h2&gt;
&lt;h3&gt;
  
  
  What is a recursive function?
&lt;/h3&gt;

&lt;p&gt;My understanding of a recursive function is a function which calls itself, until a certain condition is met. It consists of a base case, and a recursive case.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Base case: when triggered, this stops the recursive function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recursive case: The recursive function calls itself again, with a slight variation from the previous function call. The recursive case occurs when the base case is not triggered.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  What is the minimax algorithm?
&lt;/h3&gt;

&lt;p&gt;My understanding is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;it’s an algorithm which uses recursion&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the aim is to maximise the outcome for active player and conversely minimise the outcome for the other player&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;it is used to get the optimum move for a player in a game&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;it plays out all of the possible outcomes, using the current player and opponent mark&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;each outcome of the game is given a score, depending on whether the current player wins, an opponent wins, or the game is a tie.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the selected move should minimise the points the opponent can obtain.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In the context of a Tic Tac Toe game:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The maximising player is the UC player, and the minimising player is the opponent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The base case is triggered when either player has a winning line or the board is full (a tie).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the base case is not triggered, the recursive function will continue where the UC player’s mark and the opponent’s mark will keep being added to the available squares on a board&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a mark is added to the board, this means the recursive function has been called again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a player has won, or the game is a tie, the initial square that was played is given a score.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The UC player selects the best move where the opponent’s score is minimised.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  How is an outcome scored?
&lt;/h2&gt;

&lt;p&gt;If the game is a tie, the outcome is scored as 0. The maximum points a winning player can obtain is 10 (or -10 in the case of an opponent).&lt;/p&gt;

&lt;p&gt;As a mentioned above, when a mark is added to the board, the recursive function is being called again. My understanding is by keeping track of the depth (how many times the recursive function has been called), this helps to assess the outcome of the game. The aim is to win in the fewest moves. Therefore a win in 1 move should have a higher score than a win in 3 moves&lt;/p&gt;

&lt;p&gt;As a mark is added to the board (another recursive function call, increase in depth by 1), the depth is subtracted from the player’s maximum score.&lt;/p&gt;

&lt;p&gt;For example, here’s a scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The UC player is looking for the best move and tries out position 3&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the recursive function was called 3 times (&lt;code&gt;depth = 3&lt;/code&gt;), the UC obtained a winning line&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  maximum_score = 10
  depth = 3
  score = maximum_score - depth
  #score = 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  A Visual Example Of The UC Player Using Minimax
&lt;/h2&gt;

&lt;p&gt;Expanding on the above, the example below presents a scenario where the UC player has three available squares it can choose from (3, 4, and 9). It demonstrates all of possible outcomes being played.&lt;/p&gt;

&lt;p&gt;The UC player (x) is the maximising player, and the opponent (o) is the minimising player. I’ll explain all of the possible outcomes in more detail below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7QYdot-u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/LL3kSXe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7QYdot-u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/LL3kSXe.png" alt="" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  The UC playing position 3
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BqaL1u3D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ddpfdSw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BqaL1u3D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ddpfdSw.png" alt="" width="289" height="797"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The image above demonstrates the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;UC plays position 3 (depth 1)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Opponent plays position 4 (depth 2)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UC plays position 9 (depth 3)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The outcome is a &lt;strong&gt;winning line&lt;/strong&gt; for the UC, after a total of 3 positions have been played.&lt;/p&gt;

&lt;p&gt;The UC has won, so the &lt;strong&gt;maximum score is 7&lt;/strong&gt;. Remember the maximum points the UC can get is 10, and the depth is subtracted from this.&lt;/p&gt;

&lt;p&gt;To the right of the image:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;UC plays position 3 (depth 1)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Opponent plays position 9 (depth 2)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;UC plays position 4 (depth 3)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The outcome is tie, neither the UC or the opponent has a winning line. The &lt;strong&gt;minimum score is 0&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In conclusion, for position 3:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Maximum score = 7 (10–3)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimum score = 0 (tie)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  The UC playing position 4
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zJYSKpGv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/thNxHjJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zJYSKpGv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/thNxHjJ.png" alt="" width="300" height="792"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above diagram, the UC plays position 4. When doing this, both outcomes result in the UC not winning, and the opponent wins in one.&lt;/p&gt;

&lt;p&gt;In conclusion, for position 4:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Maximum score = 0 (tie)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimum score = -8 ( -(10–2))&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  The UC playing position 9
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Axoh9xy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/sUm9qwQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Axoh9xy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/sUm9qwQ.png" alt="" width="289" height="793"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;maximum score is 7&lt;/strong&gt; , as the UC can win in one of the outcomes at depth 3.&lt;/p&gt;

&lt;p&gt;However, the &lt;strong&gt;minimum score is -8&lt;/strong&gt; , because the opponent wins in the other outcome at depth 2.&lt;/p&gt;

&lt;p&gt;Although the UC can win by choosing position 9, the opponent can win on the next move.&lt;/p&gt;

&lt;p&gt;In conclusion, for position 9:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Maximum score = 7 (10–3)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimum score = -8 ( -(10–2))&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Using the scores to select a best move
&lt;/h2&gt;

&lt;p&gt;My understanding is the UC player’s aim is to ensure the opponent obtains the minimum amount of points possible. The higher the opponent’s points, the higher its chance of winning.&lt;/p&gt;

&lt;p&gt;Therefore, &lt;strong&gt;the best move is square 3 🎉&lt;/strong&gt; with the maximum score of 0 for the opponent (minimising player). This position results in either a win or tie for the UC player. The opponent has no chance of winning.&lt;/p&gt;


&lt;h2&gt;
  
  
  Making a beatable computer player unbeatable
&lt;/h2&gt;

&lt;p&gt;In theory, the above sounds great but how can it be implemented to create an unbeatable computer player? In all honesty, I found translating the above into code extremely difficult.&lt;/p&gt;

&lt;p&gt;There are many examples, blogs and videos demonstrating numerous ways to create an unbeatable computer player using minimax. You can find the resources I found helpful at the bottom of this blog post. In this explanation, I’ll try to explain each part of the function.&lt;/p&gt;

&lt;p&gt;Before I begin, &lt;a href="https://gist.github.com/ellehallal/8f8388306502f6a0d314a20f88422eec"&gt;here is the Minimax class&lt;/a&gt; I created. It contains the &lt;code&gt;find_best_move&lt;/code&gt; recursive function, which the unbeatable player will use.&lt;/p&gt;
&lt;h3&gt;
  
  
  The reason for choose_move
&lt;/h3&gt;

&lt;p&gt;To ensure the instance of the board object being used within the game isn’t modified, a new instance of the board is created using the &lt;code&gt;copy_board&lt;/code&gt; method. You can see the Board class &lt;a href="https://github.com/ellehallal/ruby-tic-tac-toe/blob/master/lib/board.rb"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  The purpose of find_best_move’s parameters
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_best_move&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_player&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opponent&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;board&lt;/code&gt; is needed in order to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;get all of the squares, consisting of unavailable (taken) and available squares on the board&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;check if a player has a winning line on the board, or if the board is full (tie)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;reset the square was modified, with a player’s mark&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;depth&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to keep a track of how many times the recursive function was called&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;current_player&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the mark of the player whose turn it is to play a move&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;opponent&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the current_player's opponent&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  best_score and available_squares
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;best_score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;available_squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;available_squares&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;best_score&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;to keep track of the scores for positions&lt;/li&gt;
&lt;li&gt;When all of the available squares have been evaluated, best_score will contain the scores, with the available square as the key. For example: &lt;code&gt;{3=&amp;gt;-8, 9=&amp;gt;0}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;available_squares&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;contains the available squares on the current board as an array&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  The base case for find_best_move
&lt;/h3&gt;

&lt;p&gt;As mentioned previously, in a recursive function, there should be a base case, which will stop the function if it is triggered. Here it is:&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;score_move&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_player&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opponent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;
  &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stop_playing&lt;/span&gt;&lt;span class="o"&gt;?(&lt;/span&gt;&lt;span class="n"&gt;current_player&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opponent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the game is a tie, or either player has won, the game is over. As a result, the recursion should stop. &lt;code&gt;board.stop_playing?&lt;/code&gt; returns true if this is the case.&lt;/p&gt;

&lt;p&gt;The outcome of playing the initial square should then be scored and returned, to be added to the &lt;code&gt;best_score&lt;/code&gt; hash.&lt;/p&gt;

&lt;p&gt;A separate method &lt;code&gt;score_move&lt;/code&gt; returns the score depending on the outcome.&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="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;score_move&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_player&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opponent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;winning_line&lt;/span&gt;&lt;span class="o"&gt;?(&lt;/span&gt;&lt;span class="n"&gt;current_player&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;depth&lt;/span&gt;
  &lt;span class="n"&gt;elsif&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;winning_line&lt;/span&gt;&lt;span class="o"&gt;?(&lt;/span&gt;&lt;span class="n"&gt;opponent&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;depth&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
  &lt;span class="n"&gt;elsif&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;complete&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Recursive case: Iterating through the available squares
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Iterate through the available squares on the board&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Play the &lt;code&gt;current_player&lt;/code&gt;’s mark in the selected square&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;code&gt;best_score&lt;/code&gt; hash, set the key as the square, and the value to the outcome score.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is where the recursive function is called again, with slight variation. &lt;code&gt;board&lt;/code&gt; now has a mark in a previously available space, &lt;code&gt;depth&lt;/code&gt; has increased by 1. The &lt;code&gt;current_player&lt;/code&gt; and &lt;code&gt;opponent&lt;/code&gt; switch.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;board.reset_square&lt;/code&gt; removes the mark from the available square&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why is the return value of the recursive function multiplied by-1?&lt;/strong&gt; 🤷🏽‍♀️&lt;/p&gt;

&lt;p&gt;This is something I’m yet to grasp, but here’s a quote from a &lt;a href="http://www.shei.io/recursion-minimax-algorithm/"&gt;blog post&lt;/a&gt;, explaining the reason:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Multiplying the return value by -1 will ensure you get either [a positive or negative score] depending on who is last to move.If the game is won on any odd move (1,3,5,7,9), the result will be [positive], which means the current player has won the game.If the game is won on any even move (2,4,6,8), the score will be [negative], which means the opponent would have won the game.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Recursive case: Evaluating the move
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;evaluate_move&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;best_score&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last part of the &lt;code&gt;find_best_move&lt;/code&gt; function is to evaluate the move. My understanding is if the depth is not zero, meaning the recursive function has not returned to the first function call, then return the score.&lt;/p&gt;

&lt;p&gt;If the depth is zero, the best move is to be returned as the UC’s player’s move.&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="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;max_best_move&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max_by&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;}[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;max_best_score&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;max_by&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;}[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;evaluate_move&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;zero&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;max_best_move&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;max_best_score&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, position 9 was played and the score for the outcome was 0 (a tie). If &lt;code&gt;depth = 1&lt;/code&gt; the score needs to be returned. Remember the &lt;code&gt;best_score&lt;/code&gt; hash that’s in the initial recursive function at &lt;code&gt;depth 0?&lt;/code&gt; Currently it looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;best_score = {3=-8, 9=&amp;gt; }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It needs the value (0) to match the key (9), to complete the hash. As a result of calling &lt;code&gt;evaluate_move&lt;/code&gt;, the score will be returned, and the &lt;code&gt;best_move&lt;/code&gt; hash becomes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;best_score = {3=-8, 9=&amp;gt;0}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;depth = 0&lt;/code&gt;, the values (scores) of each key (available square) in the hash can be evaluated with &lt;code&gt;evaluate_move&lt;/code&gt;. The key with the highest value is returned. In this example, the best move is 9.&lt;/p&gt;

&lt;p&gt;In conclusion, this is my understanding of recursion, and using minimax to date. There are still some elements I am unsure about. As my understanding of minimax improves, the way the unbeatable player has been created is likely to change.&lt;/p&gt;

&lt;p&gt;You can find the repository for my version of Tic Tac Toe, in Ruby here: &lt;a href="https://github.com/ellehallal/ruby-tic-tac-toe"&gt;https://github.com/ellehallal/ruby-tic-tac-toe&lt;/a&gt;&lt;/p&gt;




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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.neverstopbuilding.com/blog/minimax"&gt;Tic Tac Toe: Understanding the Minimax Algorithm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.shei.io/recursion-minimax-algorithm/"&gt;Recursion 201: Minimax, Tic Tac Toe, &amp;amp; an unbeatable AI&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.freecodecamp.org/how-to-make-your-tic-tac-toe-game-unbeatable-by-using-the-minimax-algorithm-9d690bad4b37"&gt;How to make your Tic Tac Toe game unbeatable by using the minimax algorithm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.neverstopbuilding.com/blog/minimax"&gt;Tic Tac Toe: Understanding the Minimax Algorithm&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>minimax</category>
      <category>algorithms</category>
      <category>ai</category>
      <category>oop</category>
    </item>
    <item>
      <title>Testing Output With RSpec</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Tue, 09 Apr 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/testing-output-with-rspec-134e</link>
      <guid>https://dev.to/ellehallal/testing-output-with-rspec-134e</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;



&lt;p&gt;This is a quick blog on testing output with RSpec. Currently, I am working on a &lt;a href="https://github.com/ellehallal/ruby-tic-tac-toe"&gt;Tic Tac Toe game in Ruby&lt;/a&gt;, where a user can play against a computer.&lt;/p&gt;


&lt;h2&gt;
  
  
  Previous test attempts
&lt;/h2&gt;

&lt;p&gt;I’ve experienced issues testing a loop in the game, where it keeps requesting moves from players until the board is full or a player has won.&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="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;main_game&lt;/span&gt;
  &lt;span class="n"&gt;play_move&lt;/span&gt; &lt;span class="n"&gt;until&lt;/span&gt; &lt;span class="nd"&gt;@game&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;over&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;end_of_game&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One attempt at testing this was to spy on the &lt;code&gt;play_move&lt;/code&gt; and &lt;code&gt;end_of_game&lt;/code&gt; methods. This worked fine if a full board was provided.&lt;/p&gt;

&lt;p&gt;However, this was proving difficult to test when an incomplete board was present. Despite simulating user input, the test would hang at this stage. The same issue occurred when using &lt;a href="https://relishapp.com/rspec/rspec-mocks/v/3-8/docs/verifying-doubles/using-an-object-double"&gt;object doubles&lt;/a&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Testing output
&lt;/h2&gt;

&lt;p&gt;One of my mentors suggested testing the output of the &lt;code&gt;end_of_game&lt;/code&gt; method. The &lt;code&gt;end_of_game&lt;/code&gt; method prints the Tic Tac Toe board and the outcome of the game.&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="n"&gt;it&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;plays&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;game&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;ends&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;winning&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;

  &lt;span class="n"&gt;expect&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;main_game&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;to&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"""

  1 | x | x
----------------
  o | o | x
----------------
  x | o | o
The current player is x
Choose a position from 1-9:

  x | x | x
----------------
  o | o | x
----------------
  x | o | o
x is the winner!"""&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;to_stdout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The multi-lines and alignment was also proving difficult to get right for the expected output. The key element that needed to be tested was the outcome of the game, which was the last line of the output: 'x is the winner!'&lt;/p&gt;




&lt;h2&gt;
  
  
  StringIO
&lt;/h2&gt;

&lt;p&gt;StringIO is a ‘&lt;a href="https://stackoverflow.com/questions/37526074/what-is-stringio-in-the-context-of-rspec-testing-ruby-on-rails"&gt;string-based replacement for an IO object. It acts same as a file, but it’s kept in memory as a String&lt;/a&gt;’.&lt;/p&gt;

&lt;p&gt;The idea is to catch the stream of output and redirect it. To check the last line of the output was correct, my mentor suggested trying the following:&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="n"&gt;it&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;plays&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;game&lt;/span&gt; &lt;span class="n"&gt;that&lt;/span&gt; &lt;span class="n"&gt;ends&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;winning&lt;/span&gt; &lt;span class="n"&gt;player&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;controller&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;controller_setup&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'x'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'x'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'o'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'x'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'x'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;     &lt;span class="sc"&gt;'o'&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;allow&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;$stdin&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;to&lt;/span&gt; &lt;span class="n"&gt;receive&lt;/span&gt;&lt;span class="o"&gt;(:&lt;/span&gt;&lt;span class="n"&gt;gets&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;and_return&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'8'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'4'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'1'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;$stdout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;StringIO&lt;/span&gt;&lt;span class="o"&gt;*.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;

  &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;main_game&lt;/span&gt;
  &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;$stdout&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;string&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"\n"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;last&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A new instance of the &lt;code&gt;controller&lt;/code&gt; class is created, with an incomplete board&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The input is simulated to play the three remaining positions on the board&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The standard output &lt;code&gt;$stdin&lt;/code&gt; is redirected to a new instance of &lt;code&gt;StringIO&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;main_game&lt;/code&gt; method is called to play and complete the game&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The output is then saved to the variable &lt;code&gt;output&lt;/code&gt;, converted to a string, and split at the points where there is a new line to create an array of strings&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The last item in the array is expected to be 'x is the winner!'&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I’ll continue to use this method when testing large blocks of text with multiple lines. It’s significantly simpler than trying to align multi-line outputs in a test.&lt;/p&gt;




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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://thoughtbot.com/blog/io-in-ruby#stringio"&gt;IO in Ruby&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ruby-doc.org/core-2.3.1/IO.html"&gt;Class: IO (Ruby 2.3.1)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/14987362/how-can-i-capture-stdout-to-a-string/22777806"&gt;How can I capture STDOUT to a string?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>tdd</category>
      <category>testing</category>
      <category>rspec</category>
      <category>stringio</category>
    </item>
    <item>
      <title>What Is The Red Green Refactor Cycle</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Fri, 29 Mar 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/what-is-the-red-green-refactor-cycle-2n9i</link>
      <guid>https://dev.to/ellehallal/what-is-the-red-green-refactor-cycle-2n9i</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;



&lt;p&gt;This week, I have continued creating a human vs. human &lt;a href="https://github.com/ellehallal/ruby-tic-tac-toe"&gt;Tic Tac Toe game in Ruby&lt;/a&gt;, using Test Driven Development. I have also been trying to get familiar with a number of principles and rules, such as SOLID principles and the Four Rules of Simple Design.&lt;/p&gt;

&lt;p&gt;When creating Tic Tac Toe, Test Driven Development is helping me to break down the game into small chunks. I still have a way to go, but the red, green refactor cycle is helping me to write cleaner code.&lt;/p&gt;


&lt;h2&gt;
  
  
  Red, Green, Refactor cycle
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Red
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;red&lt;/em&gt; stage means that a test should be written and it should fail. This means not writing any code related to the test yet. In addition, it’s important to run the test to ensure it fails. Here’s an example of a test in my Tic Tac Toe game:&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="n"&gt;it&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;there&lt;/span&gt; &lt;span class="n"&gt;are&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="n"&gt;spaces&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'x'&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="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;complete&lt;/span&gt;&lt;span class="o"&gt;?).&lt;/span&gt;&lt;span class="na"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Green
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;green&lt;/em&gt; stage means code should be written to pass the test. At this stage, the code doesn’t need to be the cleanest, just enough to make the test pass.&lt;/p&gt;

&lt;p&gt;The simplest way of passing the above test would be to create a method namedcomplete?, which simply returns false. Here’s my initial attempt, which passed the test:&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="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;complete&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;available_squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;@squares&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;count&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;is_a&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;available_squares&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Refactor
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;refactor&lt;/em&gt; stage refers to writing a cleaner version of the code and still pass the test. In Sandi Metz’s presentation on &lt;a href="https://www.youtube.com/watch?v=v-2yFMzxqwU"&gt;SOLID Object-Oriented Design&lt;/a&gt;, there are a number of factors to consider when refactoring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Is it DRY?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Does it have a single responsibility?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Does everything in it change at the same rate?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Does it depend on things that change less often that it does?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Looking at the method above, it appears to be DRY. However, it does have more than one responsibility.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Counting the amount of available squares&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Returning true or false depending on the amount of available squares&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My understanding is there should be two separate methods. With this in mind, another test was created to test a method counting the amount of available squares. The related code in complete? was extracted and added to the new method:&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="c1"&gt;//test&lt;/span&gt;
&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="n"&gt;squares&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="sc"&gt;'x'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'o'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
  &lt;span class="n"&gt;expect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;available_squares&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;to&lt;/span&gt; &lt;span class="n"&gt;eq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;//method&lt;/span&gt;
&lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;available_squares&lt;/span&gt;
  &lt;span class="nd"&gt;@squares&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;count&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;is_a&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The refactored &lt;code&gt;complete?&lt;/code&gt; method is now doing one thing: returning true or false depending on the amount of available squares:&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="n"&gt;def&lt;/span&gt; &lt;span class="n"&gt;complete&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
  &lt;span class="n"&gt;available_squares&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;zero&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I still have a way to go to adhere to the Single Responsibility Principle throughout the Tic Tac Toe game. Following the red, green, refactor cycle and the points on refactoring are helping to apply this principle.&lt;/p&gt;




&lt;h2&gt;
  
  
  Helpful resources:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://youtu.be/v-2yFMzxqwU"&gt;Sandi Metz — SOLID Object-Oriented Design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://martinfowler.com/bliki/BeckDesignRules.html"&gt;Beck Design Rules&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.cleancoder.com/uncle-bob/2014/12/17/TheCyclesOfTDD.html"&gt;The Cycles Of TDD&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>refactoring</category>
      <category>tdd</category>
      <category>testing</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>What Is The Single Responsibility Principle?</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Thu, 21 Mar 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/what-is-the-single-responsibility-principle-3259</link>
      <guid>https://dev.to/ellehallal/what-is-the-single-responsibility-principle-3259</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;



&lt;p&gt;The Single Responsibility Principle(SRP) is one of the five SOLID principles of object-oriented programming. The intention is to “&lt;a href="https://en.wikipedia.org/wiki/SOLID"&gt;make software designs more understandable, flexible and maintainable&lt;/a&gt;”.&lt;/p&gt;

&lt;p&gt;SRP is the concept that a class should have &lt;strong&gt;one&lt;/strong&gt; responsibility, and therefore only one reason to change.&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementing The Principle
&lt;/h2&gt;

&lt;p&gt;I’m currently working on a Tic Tac Toe game in Ruby, where I have been provided with a number of user stories. The first user story is: &lt;em&gt;as a user, I can see the initial board&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Breaking this down, my understanding is there needs to be a Tic Tac Toe board, &lt;strong&gt;and&lt;/strong&gt; the user needs to be able to see it.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating The Board
&lt;/h3&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;Board&lt;/span&gt;
  &lt;span class="n"&gt;attr_reader&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;

  &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;
  &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Above, the Board class takes a board as an argument when initialised. For a Tic Tac Toe game, an array containing numbers 1–9 will be passed when an instance of Board is initialised.&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="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Board&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;new&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="mi"&gt;6&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Displaying The Board
&lt;/h3&gt;

&lt;p&gt;To show the Tic Tac Toe board, a method is needed to show the output to the user. 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="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;display_board&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt; &lt;span class="s"&gt;"""
    #{board[0]} | #{board[1]} | #{board[2]}
    -----------
    #{board[3]} | #{board[4]} | #{board[5]}
    -----------
    #{board[6]} | #{board[7]} | #{board[8]}\n"""&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It would be natural to assume this method should be within the &lt;code&gt;Board&lt;/code&gt; class, simply because that is where the Tic Tac Toe board lives.&lt;/p&gt;

&lt;p&gt;However, what if changes were to occur in the future? For example, if the application was to become web-based, rather than command line based, &lt;code&gt;display_board&lt;/code&gt; would be a redundant method. As a result, it is likely that any methods within Board containing output would need to be changed.&lt;/p&gt;

&lt;p&gt;What would happen if all output was contained within a separate class? In the case above, the &lt;code&gt;Board&lt;/code&gt; class wouldn’t need to be amended because it doesn’t contain methods that are printing to the command line.&lt;/p&gt;

&lt;p&gt;With this in mind, a new class called &lt;code&gt;Display&lt;/code&gt;, was created with the &lt;code&gt;display_board&lt;/code&gt; method added:&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;Display&lt;/span&gt;
  &lt;span class="n"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;display_board&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
  &lt;span class="n"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For other user stories, where printing a message to the command line is needed, such as prompting the user for a move and displaying the current player, it will be contained within this class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By implementing the Single Responsibility principle, the &lt;code&gt;Board&lt;/code&gt; and &lt;code&gt;Display&lt;/code&gt; classes have one responsibility each and only one reason to change.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Board&lt;/code&gt; class manages the state of the Tic Tac Toe board. In relation to the user story, it creates a Tic Tac Toe board.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Display&lt;/code&gt; class manages printing output to the command line for the user to see. In relation to the user story, the user can see the initial board.&lt;/p&gt;

</description>
      <category>oop</category>
      <category>softwaredesign</category>
      <category>solid</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>How To Deploy A Web App To Heroku</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Wed, 30 Jan 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/how-to-deploy-a-web-app-to-heroku-2f4l</link>
      <guid>https://dev.to/ellehallal/how-to-deploy-a-web-app-to-heroku-2f4l</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---Vo5jvu5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/oVoqC2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---Vo5jvu5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/oVoqC2t.png" alt="" width="880" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a quick blog on how I deployed my weather manager app to Heroku.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/ellehallal/js-weather-manager"&gt;Github repository&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://peaceful-ridge-32032.herokuapp.com/"&gt;Weather Manager on Heroku&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;Deploying took longer than anticipated, due to an issue I was experiencing with dotenv-webpack and dotenv in a production environment. The following error kept popping up when deploying to Heroku:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;failed to load ./.env

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

&lt;/div&gt;



&lt;p&gt;Thanks to one of my mentors, Dan, for helping me to figure out what was going on! As this issue has been resolved, this blog will outline the steps in an order that should not cause errors when deploying.&lt;/p&gt;

&lt;p&gt;For reference, here’s how my weather manager files are organised. There are hidden files:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nAirRoJu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Xls1A5f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nAirRoJu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Xls1A5f.png" alt="" width="281" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;./dist&lt;/code&gt; contains &lt;code&gt;main.js&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;.env&lt;/code&gt; (which contains my API key) is in the root directory&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 1: Express.js — web app framework
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;code&gt;server.js&lt;/code&gt; in the root directory, and add the following code:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;static&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;npm install express&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key points
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;__dirname&lt;/code&gt; is the directory where &lt;code&gt;server.js&lt;/code&gt; is&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;__dirname + ‘/dist'&lt;/code&gt; is the current directory from where &lt;code&gt;main.js&lt;/code&gt; is running&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 2: Create webpack.prod.js
&lt;/h2&gt;

&lt;p&gt;This step is important if you have &lt;code&gt;dotenv-webpack&lt;/code&gt; installed. If installed then in &lt;code&gt;webpack.config.js&lt;/code&gt;, &lt;code&gt;dotenv-webpack&lt;/code&gt; is required:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Dotenv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dotenv-webpack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./src/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;main.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;empty&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Dotenv&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 fine for development, but doesn’t seem to work well for production. Therefore, a similar file is needed for production only, which doesn’t contain references to &lt;code&gt;dotenv-webpack&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a copy of &lt;code&gt;webpack.config.js&lt;/code&gt; in your root directory and name it &lt;code&gt;webpack.prod.js&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In &lt;code&gt;webpack.prod.js&lt;/code&gt;, remove references to &lt;code&gt;dotenv-webpack&lt;/code&gt;, and replace it with the following:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./src/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;production&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;main.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;node&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;empty&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DefinePlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;process.env&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{},&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Under scripts in &lt;code&gt;package.json&lt;/code&gt;, add:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node server.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;heroku-postbuild&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack --config webpack.prod.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a result, Heroku will use the &lt;code&gt;webpack.prod.js&lt;/code&gt; file, rather than the &lt;code&gt;webpack.config.js&lt;/code&gt; file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set the version of npm and Node.js by adding the below to &lt;code&gt;package.json:&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;engines&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;node&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;11.6.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;npm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;6.5.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 3: Only require dotenv when NODE_ENV is set to development
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Assuming dotenv is also installed, add the following to &lt;code&gt;server.js&lt;/code&gt;, just under &lt;code&gt;const app = express()&lt;/code&gt;;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dotenv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;silent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 4: Set dotenv-webpack and dotenv as devDependencies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;dotenv-webpack&lt;/code&gt; and &lt;code&gt;dotenv&lt;/code&gt; to be required during development only, run the following:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install dotenv --save-dev
npm install dotenv-webpack --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 5: Deploying to Heroku
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://signup.heroku.com/"&gt;Sign up to Heroku&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;Install Heroku CLI&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Log into Heroku via the terminal with &lt;code&gt;heroku login&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;heroku create&lt;/code&gt; to create your app on Heroku. An app name will be generated&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reinitialise the project by running &lt;code&gt;git init&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set a Heroku remote branch by &lt;code&gt;heroku git:remote --app [your-heroku-app-name]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set your environment variables — or config vars as they’re referred to in Heroku. Here’s how I set my API key for openweathermap: &lt;code&gt;heroku config:set API_KEY=myapikey3902e92e802e8&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Git add and commit&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Push to Heroku with &lt;code&gt;git push heroku master&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that’s it (hopefully)!&lt;/p&gt;




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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://webpack.js.org/guides/production/"&gt;Webpack.js documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/gdi2290/angular-starter/issues/386"&gt;Stack Overflow: Best way to pass variables through Webpack?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://codeburst.io/deploy-your-webpack-apps-to-heroku-in-3-simple-steps-4ae072af93a8"&gt;Deploy your Webpack apps to Heroku in 3 simple steps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://devcenter.heroku.com/articles/config-vars"&gt;Configuration and Config Vars | Heroku Dev Center&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>heroku</category>
      <category>javascript</category>
      <category>node</category>
      <category>webpack</category>
    </item>
    <item>
      <title>What Is Dependency Injection?</title>
      <dc:creator>Elle Hallal 👩🏽‍💻</dc:creator>
      <pubDate>Fri, 16 Nov 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/ellehallal/what-is-dependency-injection-1gdc</link>
      <guid>https://dev.to/ellehallal/what-is-dependency-injection-1gdc</guid>
      <description>&lt;center&gt;&lt;h3&gt;Originally posted at &lt;a href="https://ellehallal.dev"&gt;ellehallal.dev&lt;/a&gt;👩🏽‍💻&lt;/h3&gt;&lt;/center&gt;



&lt;p&gt;This is a quick blog on how I've used dependency injection to improve the design of my &lt;a href="https://github.com/ellehallal/js-tic-tac-toe/"&gt;JavaScript Tic Tac Toe game&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the example below, when the &lt;code&gt;Game&lt;/code&gt; class is initialised, instances of the &lt;code&gt;Board&lt;/code&gt; and &lt;code&gt;Player&lt;/code&gt; classes are also created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Game&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Board&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;player1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="no"&gt;HumanPlayer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;player2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="no"&gt;HumanPlayer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we wanted to add a new type of player (&lt;code&gt;ComputerPlayer&lt;/code&gt;) as an option in our game, the above code would make it difficult to do so. This is because the player types are "hardcoded" in the &lt;code&gt;Game&lt;/code&gt;'s constructor. This is where dependency injection comes in.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Is It?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;In software engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. &lt;br&gt;
— Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Let's apply this to our example. Instead of "hard coding" the classes to be initialised inside of &lt;code&gt;Game&lt;/code&gt;'s constructor, we can pass them in.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Game&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;player1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;player2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;player1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;player1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;player2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;player2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;When we initialise a new instance of &lt;code&gt;Game&lt;/code&gt;, we can pass in our arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;board&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Board&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;humanPlayer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="no"&gt;HumanPlayer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;computerPlayer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="no"&gt;ComputerPlayer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;const&lt;/span&gt; &lt;span class="n"&gt;game&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="no"&gt;Game&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;board&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;humanPlayer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;computerPlayer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a result, this improves the flexibility of which dependents we can use with &lt;code&gt;Game&lt;/code&gt;. For example, now we can initialise the class with two human players, or two computer players, or one of each!&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/ellehallal/js-tic-tac-toe"&gt;JavaScript Tic Tac Toe Repo on Github&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://this-tic-tac-toe.herokuapp.com/"&gt;Tic Tac Toe on Heroku&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>oop</category>
      <category>dependencyinjection</category>
      <category>softwaredesign</category>
      <category>cleancode</category>
    </item>
  </channel>
</rss>
