<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Dylan Maccora</title>
    <description>The latest articles on Forem by Dylan Maccora (@maccoda).</description>
    <link>https://forem.com/maccoda</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%2F354438%2Faaaf44ad-b205-4a4b-ad0f-241188983a54.jpeg</url>
      <title>Forem: Dylan Maccora</title>
      <link>https://forem.com/maccoda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/maccoda"/>
    <language>en</language>
    <item>
      <title>Valuable functional programming basics every developer should know</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Sun, 05 Sep 2021 15:44:38 +0000</pubDate>
      <link>https://forem.com/maccoda/valuable-functional-programming-basics-every-developer-should-know-2a3e</link>
      <guid>https://forem.com/maccoda/valuable-functional-programming-basics-every-developer-should-know-2a3e</guid>
      <description>&lt;p&gt;For a long time there was an idealistic divide between functional and object oriented programming and this was manifested in the languages that came through. In the current landscape however you are spoilt for choice with languages and this divide is closing with a lot of the most popular languages available taking the best aspects of both paradigms. Kotlin is a great example of this where it is initially based on the JVM and follows OO principles with classes, etc but also introduces a lot of functional principles such as first class functions and the scary monad (I am no expert in this but there are a lot of easy to understand properties that you can make use of). Another example is Rust where it cannot easily be classified as either paradigm but uses functional elements such as using the &lt;code&gt;Result&lt;/code&gt; type to promote functions always return a result, as well as object oriented notions such as the dot notation of functions on a type.&lt;/p&gt;

&lt;h2&gt;
  
  
  Monad Concepts
&lt;/h2&gt;

&lt;p&gt;Monad is a term used a lot in functional programming and especially in the vast theory behind it. I did not wish to delve into such theory here but rather the useful functions that become available because we can treat some objects as a monad. Of particular interest is working with collections or streams of elements. The main reason I personally find these incredibly powerful is that they provide a common language for which we can describe the intention of common functions that can be combined together to achieve our end result. A lot of these functions you can use in place of a &lt;code&gt;for&lt;/code&gt; loop so next time you are looking at iterating over a collection with a &lt;code&gt;for&lt;/code&gt; loop consider if one of the following constructs are what you need.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;map&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;map&lt;/code&gt; function is a transformation of each element from type X to type Y. Depending on the language you are using this may modify the elements in place or create a new collection of the mapped values (in true functional style).&lt;/p&gt;

&lt;p&gt;An easy example is say I have a collection of blog posts and I want to obtain the titles of all these, we can achieve this by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;blogPosts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Collection&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"post 1"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                                             &lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"post 2"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;blogPostTitles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Collection&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blogPosts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// ["post 1", "post 2"]&lt;/span&gt;
&lt;span class="c1"&gt;// Without the syntactic sugar&lt;/span&gt;
&lt;span class="c1"&gt;// val blogPostTitles: Collection&amp;lt;String&amp;gt; = blogPosts.map { post -&amp;gt; post.title }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break this example down a bit to what is happening here. The &lt;code&gt;map&lt;/code&gt;&lt;br&gt;
function takes a function as a parameter which we will call our &lt;strong&gt;transform&lt;br&gt;
function&lt;/strong&gt;. This function maps the input type to some other type (this can the&lt;br&gt;
same as the input type). The &lt;code&gt;map&lt;/code&gt; function then iterates through the input list&lt;br&gt;
and creates a new list applying this function to each element.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;flatMap&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;flatMap&lt;/code&gt; function is actually a combination of the above &lt;code&gt;map&lt;/code&gt; function and the &lt;code&gt;flatten&lt;/code&gt; function (not covered in this post). The &lt;code&gt;flatten&lt;/code&gt; function takes a container of containers type and &lt;strong&gt;flattens&lt;/strong&gt; it to just a container type.&lt;br&gt;
This is quite abstract so lets take a simple example. Say you have a &lt;code&gt;List&amp;lt;List&amp;lt;String&amp;gt;&amp;gt;&lt;/code&gt; and you want a &lt;code&gt;List&amp;lt;String&amp;gt;&lt;/code&gt;, this in the broad sense what the &lt;code&gt;flatten&lt;/code&gt; function does. In this example the container is the &lt;code&gt;List&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;Knowing this it is quite simple to describe the &lt;code&gt;flatMap&lt;/code&gt; function although it may still be difficult to wrap your head around in actual applications. The &lt;code&gt;flatMap&lt;/code&gt; function will apply the transformation specified to the element in the&lt;br&gt;
container and then flatten it. This is typically required when the transformation function returns the same type as the initial type.&lt;/p&gt;

&lt;p&gt;As an example say we have a type that represents the books a customer has read and we want to print all of the books a group of customers have read (we are not concerned about duplicates). Such data may be present as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;BookCustomer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;booksRead&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;bookCustomers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BookCustomer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The end type we are wanting is a &lt;code&gt;List&amp;lt;String&amp;gt;&lt;/code&gt; containing all the book titles. Let us first try this without &lt;code&gt;flatMap&lt;/code&gt;  to aide in seeing when to consider using it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;bookCustomers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BookCustomer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;booksRead&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;MutableList&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;mutableListOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BookCustomer&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;bookCustomers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;booksTitles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;booksRead&lt;/span&gt;
  &lt;span class="n"&gt;booksRead&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;booksTitles&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;The key thing that using these functional paradigms provides is built in immutability. In the above example we are needing to create a mutable list to get the result. We will see in the below version this is not required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;bookCustomers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;BookCustomer&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;bookRead&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bookCustomers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flatMap&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;booksRead&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The transformation we are performing is mapping the &lt;code&gt;BookCustomer&lt;/code&gt; to the list of book titles they have read, then the flatten part of &lt;code&gt;flatMap&lt;/code&gt; handles to reduction of the list of lists into a single list. This leads us into the next section on &lt;code&gt;reduce&lt;/code&gt; which is the general pattern of &lt;code&gt;flatten&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;reduce&lt;/code&gt; or &lt;code&gt;fold&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;reduce&lt;/code&gt; and &lt;code&gt;fold&lt;/code&gt; functions are more general functions to reduce a container, typically a collection, to a singular value. A simple example we can explore is reducing a collection of integers to their sum.&lt;/p&gt;

&lt;p&gt;Firstly, the difference between the two functions is primarily in the arguments that the functions take. The function signatures are below to make it simple to reference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Iterable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;fold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Iterable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;
&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;R&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The primary difference between the two functions is that one takes the &lt;code&gt;initial&lt;/code&gt; value of the resulting type and one does not. It instead uses the first value as the initial value. This subtle difference results in the &lt;code&gt;reduce&lt;/code&gt; function requiring a non-empty collection to work on whereas &lt;code&gt;fold&lt;/code&gt; can work on an empty list and just return the &lt;code&gt;initial&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;A simple example to show how these functions work is by implementing a summation function that operates on a collection of integers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sumReduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Iterable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sumFold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Iterable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;cur&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;The interesting element of these is the closure passed of the form &lt;code&gt;(acc: R, cur: T) -&amp;gt; R&lt;/code&gt;. This function is essentially and accumulation function that combines the elements of the collection to the final result. It takes two arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;acc&lt;/code&gt; which is the current accumulated result up to the current element&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cur&lt;/code&gt; which is the current element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting this in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;sumFold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The accumulation function is applied to each element and then the accumulated value is updated and passed along. Stepping through the above example would look like this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note this is for fold, if using reduce the first row would be omitted as there is no initial value&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;acc&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;cur&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;Accumulation function result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h4&gt;
  
  
  More use cases
&lt;/h4&gt;

&lt;p&gt;The power of these functions is how flexible they are, any accumulator function can be provided allowing you to work with any type. If you were inclined you could spend more time studying functional programming and see how this is the basis of a lot of incredibly power combinator functions. For  example we can create &lt;code&gt;flatMap&lt;/code&gt; using &lt;code&gt;reduce&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt;. Since this is typically handled in the standard library we will look at a more likely scenario to find in a project.&lt;/p&gt;

&lt;p&gt;The example we will go through is that of some validation framework which leverages the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Validation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;ValidationResult&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ValidationResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;VALID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;INVALID&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Imagine we have a collection of validators and we wish to implement some functionality that requires that all validators to run on the &lt;code&gt;Input&lt;/code&gt; object and it returns &lt;code&gt;VALID&lt;/code&gt; if &lt;strong&gt;all&lt;/strong&gt; validators return &lt;code&gt;VALID&lt;/code&gt; otherwise if even one returns &lt;code&gt;INVALID&lt;/code&gt; it returns &lt;code&gt;INVALID&lt;/code&gt;. The &lt;code&gt;reduce&lt;/code&gt; function can do exactly this as we can evaluate all the validators and then &lt;em&gt;reduce&lt;/em&gt; them to a single result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;validators&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Validation&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Input&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ValidationResult&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;validators&lt;/span&gt;
                                  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;ValidationResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;VALID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;ValidationResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;INVALID&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;code&gt;filter&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;filter&lt;/code&gt; function is another helpful function to work with collections which filters the collection based on a provided predicate. If the term predicate is new, it is simply a function that takes an input object and returns a boolean.&lt;/p&gt;

&lt;p&gt;Using another book example let's assume we have the below data type for a book and we want to obtain the list of all books we read, this can be done as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;read&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;books&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;booksRead&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Not quite must knows
&lt;/h2&gt;

&lt;p&gt;The above functions and types are extremely valuable in most modern languages. The below types are useful to know but depending on your language choice you may not actually use it but understanding the concept is valuable regardless as it can still have a positive impact on the way you write your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option/Maybe Type
&lt;/h3&gt;

&lt;p&gt;It is common to have to represent the possibility of some data not being present. Commonly in languages this is represented by some null value but in pure functional programming this is actually represented by a type possibly&lt;br&gt;
called &lt;code&gt;Option&lt;/code&gt; (such as in Rust) or &lt;code&gt;Maybe&lt;/code&gt; (such as in Haskell). The core of this type is to be able to reflect the absence of a value in the type system. This can also be achieved with languages still supporting null such as Kotlin as it surfaces the nullability to the type level.&lt;/p&gt;

&lt;p&gt;Having a container type to represent the absence of a value allows the client of the data to act on it safely without needing to check whether the data is present or not, which is something unable to be achieved with a basic null value alone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;no_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;None&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result_some&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Some(5)&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;result_none&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;no_data&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Result/Either Type
&lt;/h3&gt;

&lt;p&gt;In an OO landscape it is typical to handle errors through exception handling. Pure functional languages require this to be represented in the type system, which is quite a powerful model. This is again named differently depending on the language but it is a container type that has two possible representations. If it is called &lt;code&gt;Either&lt;/code&gt; then this will have a &lt;code&gt;Left&lt;/code&gt; and &lt;code&gt;Right&lt;/code&gt; type, this is more generic than the &lt;code&gt;Result&lt;/code&gt; type as you are able to represent any arbitrary&lt;br&gt;
type that is a union of two possible types. A more focused point is the &lt;code&gt;Result&lt;/code&gt; type used in Rust to represent explicitly an error as it does not have exceptions. The two types of this union are &lt;code&gt;Ok&lt;/code&gt; and &lt;code&gt;Err&lt;/code&gt; (for error).&lt;/p&gt;

&lt;p&gt;Again this notion of a type may not be largely applicable in the OO domain but it does promote you to think of handling errors and state in a different manner and representing this in the type system. This concept can easily be implemented&lt;br&gt;
in Kotlin using sealed classes and does not need to just be used for error handling but can be used to create any container class that a client can act on. Again the goal here is having the container class allows the client to work with&lt;br&gt;
the data irrespective of the underlying state.&lt;/p&gt;

</description>
      <category>functional</category>
    </item>
    <item>
      <title>Deploying your first Micronaut application to Heroku</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Sat, 11 Jul 2020 21:27:55 +0000</pubDate>
      <link>https://forem.com/maccoda/deploying-your-first-micronaut-application-to-heroku-12pm</link>
      <guid>https://forem.com/maccoda/deploying-your-first-micronaut-application-to-heroku-12pm</guid>
      <description>&lt;p&gt;&lt;a href="http://micronaut.io/"&gt;Micronaut&lt;/a&gt; is a great JVM framework I was recently introduced to. It has been designed with the intention to fit into the modern day micro-service and serverless architecture with its big selling point being compile time dependency injection. If you have ever used something like Spring Boot you will know what a difference this will make to start up times. However this post is about deploying these apps to Heroku. Once I have had some more experience in the framework I will write another post about making an application with it. In lieu of that, I would really recommend giving it a try and reach out if you have any blockers.&lt;/p&gt;

&lt;p&gt;For this post I am assuming you are familiar with &lt;a href="https://www.heroku.com/"&gt;Heroku&lt;/a&gt; as I won't go in depth about setting it up. However that is the beauty of this service is they make it really simple to get started which is great for hobby projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the Application
&lt;/h2&gt;

&lt;p&gt;Creating the app is very simple as with many frameworks. Micronaut provide a CLI tool and a &lt;a href="https://guides.micronaut.io/index.html"&gt;guide&lt;/a&gt; for creating your first application using this. Again I won't go more in depth into this but it is very important to follow this &lt;a href="https://guides.micronaut.io/index.html"&gt;guide&lt;/a&gt; otherwise your experience may differ and likely be more difficult.&lt;/p&gt;

&lt;p&gt;The important part of using the CLI tool is that is generates the base project and along with that is provides the &lt;code&gt;Dockerfile&lt;/code&gt; that can be used to build a deployed image. It is a very simple &lt;code&gt;Dockerfile&lt;/code&gt; but just highlights the focus on simplicity in the modern environments provided by Micronaut.&lt;/p&gt;

&lt;h2&gt;
  
  
  First Attempt to deploy to Heroku
&lt;/h2&gt;

&lt;p&gt;My first attempt as the naming of this sections forebodes was not all that successful and led to a few weird issues. Let's start at the beginning.&lt;/p&gt;

&lt;p&gt;Choosing Heroku as the service to manage my deployments was primarily driven by simplicity. To deploy my first app was as simple as &lt;code&gt;git push heroku master&lt;/code&gt; (after some minimal set up of course). This application should be no different I thought, it is a Gradle application and Heroku natively supports with this a Gradle build pack. Unfortunately not quite so, they know how to support Spring applications but if none is detected you are in charge of telling the build pack how to build your application. Which is a good choice and again is made simple by enforcing a convention.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If Heroku does not how to build your application you define a &lt;code&gt;stage&lt;/code&gt; Gradle task&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Of course I am not the first person to deploy a non-Spring application to Heroku so there is a simple guide. For a Micronaut application the only required part however is defining the &lt;code&gt;stage&lt;/code&gt; task and its dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="nf"&gt;stage&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;dependsOn:&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'build'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'clean'&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;build&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mustRunAfter&lt;/span&gt; &lt;span class="n"&gt;clean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was pretty simple!&lt;/p&gt;

&lt;p&gt;Now if you were anything like me and just want to get your application out there you would try to deploy this shortly after. In doing this I started to find where most of my issues lay. After the deployment I attempted to check the health endpoint and was receiving a fat nothing, my first thoughts were, 'Does it know how to run my application?'&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining the Procfile
&lt;/h3&gt;

&lt;p&gt;For those unfamiliar with Heroku, they have a notion of a &lt;code&gt;Procfile&lt;/code&gt; which defines which commands to run to start your application(s). Akin to that you would write for a build pipeline.&lt;/p&gt;

&lt;p&gt;First thing I thought here is I know I was able to run my application through Gradle using the &lt;code&gt;run&lt;/code&gt; command so let's first try that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./gradlew run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Immediately I deployed again and tested the health endpoint. Same result...&lt;/p&gt;

&lt;h3&gt;
  
  
  Finding the Error
&lt;/h3&gt;

&lt;p&gt;It turned out the error was staring me in the face the whole time in the deployment logs that I clearly do not pay the required attention to. When you perform the git deployment you don't actually get all the logs of the container you will need to use &lt;code&gt;heroku logs --tail&lt;/code&gt; to get these, and they are incredibly useful!&lt;/p&gt;

&lt;p&gt;Littered throughout the logs I had occurrences of &lt;code&gt;Error R14 (Memory quota exceeded)&lt;/code&gt;. Not only that, it did not only appear during the execution of the application but actually when trying to build the application!&lt;/p&gt;

&lt;p&gt;Now of course I could purchase the plan providing me with more memory but this was a hobby project. Further that is almost always the cheat's way out as there is probably something better you can do before just upping allocated resources.&lt;/p&gt;

&lt;p&gt;The reason for this issue I can only assume is because of the manner in which Micronaut builds your application. A lot of the heavy lifting is done during compile time giving you super fast start up times. However I was now pushing all this load onto the Heroku containers for which I had limited resources for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting the Application Deployed
&lt;/h2&gt;

&lt;p&gt;After realising I would need to perform the staging on my local machine I started looking at different methods of deploying to Heroku. As it turns out they provide you access to a container registry for which you can push images and then release them for your application.&lt;/p&gt;

&lt;p&gt;The process to deploy a container is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define your deployable image&lt;/li&gt;
&lt;li&gt;Build the artifacts needed for the image and consequently build the deployable image&lt;/li&gt;
&lt;li&gt;Push the deployable image to the registry&lt;/li&gt;
&lt;li&gt;Release the image to the deployed environment&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This process is made extremely simple by Micronaut and Heroku! I was able to script it in 3 lines!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew stage
heroku container:push web
heroku container:release web
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These define the last 3 steps of the process above but where is our image definition? It is the &lt;code&gt;Dockerfile&lt;/code&gt; that Micronaut generated for us on the first build!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; openjdk:8u171-alpine3.7&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;apk &lt;span class="nt"&gt;--no-cache&lt;/span&gt; add curl
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; build/libs/*-all.jar myapp-kt.jar&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; java ${JAVA_OPTS} -jar myapp-kt.jar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All this &lt;code&gt;Dockerfile&lt;/code&gt; does is simply copy the built artifact from the &lt;code&gt;stage&lt;/code&gt; task (which is combined into a single JAR ending in &lt;code&gt;-all&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Finally we just need to update the &lt;code&gt;Procfile&lt;/code&gt; as we do not need to build anything anymore, but rather just need to simply execute the JAR. Of course be aware there is a version appended.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;java -jar build/libs/myapp-kt-0.1-all.jar&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have updated the &lt;code&gt;Procfile&lt;/code&gt;, deploy to Heroku again and you should find your application up and running!&lt;/p&gt;

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

&lt;p&gt;To recap what we managed to achieve. We built our Micronaut application following the provided guide. Then utilizing what was provided from the template built a deployable Docker image which could then deploy to Heroku!&lt;/p&gt;

</description>
      <category>heroku</category>
      <category>micronaut</category>
      <category>projects</category>
    </item>
    <item>
      <title>Speed up your Gitlab pipelines to Heroku</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Sat, 11 Jul 2020 21:24:25 +0000</pubDate>
      <link>https://forem.com/maccoda/speed-up-your-gitlab-pipelines-to-heroku-563i</link>
      <guid>https://forem.com/maccoda/speed-up-your-gitlab-pipelines-to-heroku-563i</guid>
      <description>&lt;p&gt;Arguably one of the best things one can have for their project is a robust continuous delivery&lt;br&gt;
pipeline. Being able to know that once you commit and push your code there is infrastructure in&lt;br&gt;
place to ensure your change is correct and subsequently deployed is one the things I personally&lt;br&gt;
truly enjoy in software. So when I had some spare time I started tinkering with one of my side&lt;br&gt;
projects in getting it set up and deployed to Heroku. My project is hosted at &lt;a href="//www.gitlab.com"&gt;Gitlab&lt;/a&gt; which have&lt;br&gt;
their own integrated CI service and is deployed to &lt;a href="//www.heroku.com"&gt;Heroku&lt;/a&gt; a platform as a service (PaaS) provider&lt;br&gt;
which manages a lot of the deployment infrastructure for me, perfect for a side project.&lt;/p&gt;

&lt;p&gt;For this project I had set up the following stages:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://mermaid-js.github.io/mermaid-live-editor/#/edit/eyJjb2RlIjoiZ3JhcGggTFJcbiAgdFtUZXN0XVxuICBwcFtEZXBsb3kgUHJlLVByb2RdXG4gIGl0W0ludGVncmF0aW9uIFRlc3RzXVxuICBwW0RlcGxveSBQcm9kXVxuICB0IC0tPiBwcFxuICBwcCAtLT4gaXRcbiAgaXQgLS0-IHAiLCJtZXJtYWlkIjp7InRoZW1lIjoiZGVmYXVsdCJ9fQ"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wwzAeiBb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mermaid.ink/img/eyJjb2RlIjoiZ3JhcGggTFJcbiAgdFtUZXN0XVxuICBwcFtEZXBsb3kgUHJlLVByb2RdXG4gIGl0W0ludGVncmF0aW9uIFRlc3RzXVxuICBwW0RlcGxveSBQcm9kXVxuICB0IC0tPiBwcFxuICBwcCAtLT4gaXRcbiAgaXQgLS0-IHAiLCJtZXJtYWlkIjp7InRoZW1lIjoiZGVmYXVsdCJ9fQ" alt="" width="599" height="54"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Definitely a little more involved than most of my side projects but hopefully it actually closer&lt;br&gt;
represents would you would typically encounter for production services in the industry.&lt;/p&gt;

&lt;p&gt;The application I had was a &lt;a href="//www.kotlinlang.org"&gt;Kotlin&lt;/a&gt; web server application built with &lt;a href="//www.gradle.org"&gt;Gradle&lt;/a&gt; and deployed to&lt;br&gt;
Heroku using a very versatile deployment tool &lt;a href="https://github.com/travis-ci/dpl"&gt;dpl&lt;/a&gt;. All of this definitely worked and did what it&lt;br&gt;
needed to however it took around 17 minutes and there are always things we can improve!&lt;/p&gt;
&lt;h2&gt;
  
  
  Using a cache
&lt;/h2&gt;

&lt;p&gt;Gitlab CI runners are declaratively defined and based on &lt;a href="https://www.docker.com/"&gt;Docker&lt;/a&gt; containers. Each job executed in&lt;br&gt;
the pipeline you will get an entirely fresh environment which is great to provide a reproducible&lt;br&gt;
environment but this does require the environment to be recreated every time. In the case of Gradle&lt;br&gt;
and the Gradle wrapper this means that on each stage of the pipeline that was using a Gradle task it&lt;br&gt;
would need to download the distribution.&lt;/p&gt;

&lt;p&gt;&lt;a href="/images/gradle-download.png" class="article-body-image-wrapper"&gt;&lt;img src="/images/gradle-download.png" alt="gradle download"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To avoid needing to download this at each job we can simply add in a &lt;a href="https://docs.gitlab.com/ee/ci/caching/"&gt;cache&lt;/a&gt; in our pipeline. This&lt;br&gt;
cache is saved at the end of each job and restored at the start, so it does come with its own cost&lt;br&gt;
but compared to downloading Gradle and all project dependencies it is minor. To achieve this for my&lt;br&gt;
Gradle project I simply changed the directory for where Gradle saved the dependencies and told&lt;br&gt;
Gitlab CI to cache it, with a lot of help from &lt;a href="https://stackoverflow.com/questions/34162120/gitlab-ci-gradle-dependency-cache"&gt;this StackOverflow question&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;before_script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;export GRADLE_USER_HOME=`pwd`/.gradle&lt;/span&gt;

&lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.gradle/wrapper&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.gradle/caches&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do note that with this configuration your cache will be persisted and restored across all jobs&lt;br&gt;
(assuming you have not placed this in a single job already) so if not all stages require this cache&lt;br&gt;
we could adjust this to shave off some extra time but for now it will do just fine.&lt;/p&gt;
&lt;h2&gt;
  
  
  Deploying with Docker to Heroku directly
&lt;/h2&gt;

&lt;p&gt;The change that gave me the biggest improvement was by converting my deployment from using the &lt;a href="https://github.com/travis-ci/dpl"&gt;dpl&lt;/a&gt;&lt;br&gt;
tool to using the &lt;a href="https://devcenter.heroku.com/articles/container-registry-and-runtime"&gt;container registry&lt;/a&gt; Heroku provides.&lt;/p&gt;

&lt;p&gt;The speed of deployment is less attributed to the dpl tool and more towards how Heroku defaults its&lt;br&gt;
deployments. If you look across all of the Heroku website it shows how simple it is to perform&lt;br&gt;
deployments with git. This is definitely a positive for simplicity of deployment but in doing so it&lt;br&gt;
means that each deployment needs to be built from source each time. Using the registry we are able&lt;br&gt;
to create an immutable, deployable Docker image that we can reuse at each stage of deployment.&lt;/p&gt;

&lt;p&gt;First thing you will require is that your application builds to a Docker image, which I will not&lt;br&gt;
delve into here (maybe another post). My application was actually already being deployed through&lt;br&gt;
Docker with dpl by using the &lt;a href="https://devcenter.heroku.com/articles/build-docker-images-heroku-yml"&gt;heroku.yml&lt;/a&gt; file, so my Docker image was ready to go.&lt;/p&gt;

&lt;p&gt;With a deployable Docker image ready the next step is to add a build and publish step to our&lt;br&gt;
pipeline. For those not familiar with publishing Docker images, you can consider Docker images akin&lt;br&gt;
to a release of a library/dependency. These are published to some central registry with a specific&lt;br&gt;
identifier and version, the same is done for Docker images. We can publish a specific application&lt;br&gt;
and version to a registry, here it is hosted by Heroku others include &lt;a href="https://aws.amazon.com/ecr/"&gt;ECR (AWS)&lt;/a&gt; or of course&lt;br&gt;
&lt;a href="https://hub.docker.com/"&gt;DockerHub&lt;/a&gt;, which is then fetched and deployed. Coming back now let's add this build and publish&lt;br&gt;
step to our pipeline, firstly lets define some variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;CONTAINER_IMAGE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG&lt;/span&gt;
  &lt;span class="na"&gt;PREPROD_APP_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-pre-prod-app&lt;/span&gt;
  &lt;span class="na"&gt;PROD_APP_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-prod-app&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These variables will simply make it easy to reference the identifier given to our container image&lt;br&gt;
using some &lt;a href="https://docs.gitlab.com/ee/ci/variables/predefined_variables.html"&gt;variables&lt;/a&gt; that Gitlab will populate for us as well as the names of&lt;br&gt;
our Heroku applications. With these in place we can now create a build step, I have added this to my&lt;br&gt;
test stage so the tests and build can run in parallel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docker:19.03.1&lt;/span&gt;
  &lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker:19.03.1-dind&lt;/span&gt;
  &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;test&lt;/span&gt;
  &lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;HEROKU_PREPROD_IMAGE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;registry.heroku.com/${PREPROD_APP_NAME}/web&lt;/span&gt;
    &lt;span class="na"&gt;HEROKU_PROD_IMAGE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;registry.heroku.com/${PROD_APP_NAME}/web&lt;/span&gt;
  &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker login --username=_ --password=$HEROKU_API_KEY registry.heroku.com&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker build -t $CONTAINER_TEST_IMAGE .&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker tag $CONTAINER_TEST_IMAGE $HEROKU_PREPROD_IMAGE&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker push $HEROKU_PREPROD_IMAGE&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker tag $CONTAINER_TEST_IMAGE $HEROKU_PROD_IMAGE&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker push $HEROKU_PROD_IMAGE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above follows from the example provided in the &lt;a href="https://docs.gitlab.com/ee/user/packages/container_registry/#container-registry-examples-with-gitlab-cicd"&gt;Gitlab documentation&lt;/a&gt; with&lt;br&gt;
some tweaks to target the Heroku container registry instead of the Gitlab one. In Heroku each&lt;br&gt;
application has its own container registry, since each stage is its own application we need to push&lt;br&gt;
the built image to both registries.&lt;/p&gt;

&lt;p&gt;The one new variable here is the &lt;code&gt;HEROKU_API_KEY&lt;/code&gt; which I have injected into the pipeline. This is&lt;br&gt;
the API token that is generated by Heroku to allow access to the API, this can be accessed in your&lt;br&gt;
Heroku account settings.&lt;/p&gt;

&lt;p&gt;With the image built and pushed to the Heroku container repository we can now move onto the final&lt;br&gt;
stage which is the actual deployment to Heroku.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;preprod&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;preprod&lt;/span&gt;
  &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;apt-get update -qy&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;apt-get install -y curl bash&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;curl https://cli-assets.heroku.com/install.sh | sh&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;heroku container:release -a ${PREPROD_APP_NAME} web&lt;/span&gt;
  &lt;span class="na"&gt;only&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;master&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only way to perform the deployment is through the Heroku CLI therefore the first step here&lt;br&gt;
(after obtaining dependencies) is to download the CLI. We can then simply perform the&lt;br&gt;
&lt;a href="https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-container-release"&gt;container:release&lt;/a&gt; operation which will deploy the most recent version of our built application&lt;br&gt;
from the container registry. The same step can be done for the production stage by simply changing&lt;br&gt;
the app name variable.&lt;/p&gt;

&lt;p&gt;Using this technique over &lt;code&gt;dpl&lt;/code&gt; the deployment time went down from around 5 minutes to around 1.5&lt;br&gt;
minutes for each deployment! Reducing this deployment time is extremely valuable to allow for the&lt;br&gt;
ability to roll forward with continuous deployment pipelines, as well as a great excuse to learn&lt;br&gt;
something new!&lt;/p&gt;
&lt;h2&gt;
  
  
  Completed file
&lt;/h2&gt;

&lt;p&gt;The completed pipeline file looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;stages&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;test&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;preprod&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;integration_test&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;prod&lt;/span&gt;

&lt;span class="na"&gt;before_script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;export GRADLE_USER_HOME=`pwd`/.gradle&lt;/span&gt;

&lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.gradle/wrapper&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.gradle/caches&lt;/span&gt;

&lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;CONTAINER_IMAGE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG&lt;/span&gt;
  &lt;span class="na"&gt;PREPROD_APP_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-pre-prod-app&lt;/span&gt;
  &lt;span class="na"&gt;PROD_APP_NAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my-prod-app&lt;/span&gt;

&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docker:19.03.1&lt;/span&gt;
  &lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker:19.03.1-dind&lt;/span&gt;
  &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;test&lt;/span&gt;
  &lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;HEROKU_PREPROD_IMAGE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;registry.heroku.com/${PREPROD_APP_NAME}/web&lt;/span&gt;
    &lt;span class="na"&gt;HEROKU_PROD_IMAGE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;registry.heroku.com/${PROD_APP_NAME}/web&lt;/span&gt;
  &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker login --username=_ --password=$HEROKU_API_KEY registry.heroku.com&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker build -t $CONTAINER_TEST_IMAGE .&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker tag $CONTAINER_TEST_IMAGE $HEROKU_PREPROD_IMAGE&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker push $HEROKU_PREPROD_IMAGE&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker tag $CONTAINER_TEST_IMAGE $HEROKU_PROD_IMAGE&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;docker push $HEROKU_PROD_IMAGE&lt;/span&gt;

&lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;test&lt;/span&gt;
  &lt;span class="c1"&gt;# Test step...&lt;/span&gt;

&lt;span class="na"&gt;preprod&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;preprod&lt;/span&gt;
  &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;apt-get update -qy&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;apt-get install -y curl bash&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;curl https://cli-assets.heroku.com/install.sh | sh&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;heroku container:release -a ${PREPROD_APP_NAME} web&lt;/span&gt;
  &lt;span class="na"&gt;only&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;master&lt;/span&gt;

&lt;span class="na"&gt;integration&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;integration_test"&lt;/span&gt;
  &lt;span class="c1"&gt;# Integration step...&lt;/span&gt;

&lt;span class="na"&gt;production&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;stage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;prod&lt;/span&gt;
  &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;apt-get update -qy&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;apt-get install -y curl bash&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;curl https://cli-assets.heroku.com/install.sh | sh&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;heroku container:release -a ${PROD_APP_NAME} web&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>gitlab</category>
      <category>heroku</category>
      <category>continuousdelivery</category>
      <category>gradle</category>
    </item>
    <item>
      <title>How was I so wrong about CI and what I learnt about CD</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Sat, 11 Jul 2020 20:56:35 +0000</pubDate>
      <link>https://forem.com/maccoda/how-was-i-so-wrong-about-ci-and-what-i-learnt-about-cd-1and</link>
      <guid>https://forem.com/maccoda/how-was-i-so-wrong-about-ci-and-what-i-learnt-about-cd-1and</guid>
      <description>&lt;p&gt;My knowledge of Continuous Integration (CI) and Continuous Delivery (CD) had so far only been developed from discussions with colleges and my own assumptions on the topic. Only recently had I decided that I should actually put this knowledge to the test and started doing some reading. To my surprise I was well off...&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuous Integration
&lt;/h2&gt;

&lt;p&gt;First off I started looking into CI, you know the build machines and stuff yeah? Nope. Continuous Integration is exactly as its name describes, continuously integrating your code. For some unknown reason to me I had understood CI as the build pipeline getting kicked off every time someone makes a commit to the repository. In a way this isn't ridiculously off as this pipeline provides the means for CI but is not it itself. After watching a &lt;a href="https://www.youtube.com/watch?v=aoMfbgF2D_4"&gt;talk&lt;/a&gt; by Martin Fowler he described CI in such a raw, upfront manner that it has really stuck with me. I paraphrase but he states something along the lines of;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you can answer yes to the following 3 questions, then indeed you **are&lt;/em&gt;* practicing continuous integration:*&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;em&gt;Are all engineers pushing to trunk/master on a daily basis?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Does every commit run a solid suite of tests giving confidence the commit works?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;When the build is broken, is it typically fixed in under 10 minutes?&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is pretty far from a build pipeline tool right?&lt;/p&gt;

&lt;p&gt;So what we are saying here is that CI is the practice of continually integrating developer's code in the repository, &lt;strong&gt;daily&lt;/strong&gt;. That is, there is no never ending feature branches and no 'sub-master' branch that we all merge into before we go to our actual master, nope. Everything needs to be integrated and on the master branch. Period. I have definitely witnessed teams not practicing CI and seeing how things can gradually diverge over time and then when finally ready, bringing them all back together becomes feature work in of itself!&lt;/p&gt;

&lt;p&gt;Next up, we are saying, cool you are all integrating but does the software still do what it is meant to? The easiest way to get peace of mind for this is a solid suite of tests. Now we are all well familiar with unit tests but through my readings I was introduced to so many different types of tests, all of which play a different role in building our confidence in our software. If you are interested in looking further into it have a read of &lt;a href="https://www.atlassian.com/continuous-delivery/different-types-of-software-testing"&gt;this article&lt;/a&gt; by Atlassian. So what we really want here is a suite of tests, not only unit tests, that is kicked off every time some one delivers and definitely every time some on commits to the master branch.&lt;/p&gt;

&lt;p&gt;The final question is directed more at the culture developed, than the actual practice. I feel that it is depicting the importance of the master branch. The notion of, if it fails, you drop everything and get that baby back to green! To achieve this there has to be a culture shift, as well as confidence in your build pipeline. The last thing you want is developers not thinking twice about a failed build because 'everyone knows this test is flaky'. If it is flaky and you can do something about it, fix that up, regain some confidence in your pipeline and get that pipeline to a strong green.&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuous Delivery vs Continuous Deployment
&lt;/h2&gt;

&lt;p&gt;The two CD's were a lot newer to my vocabulary than the wrongly understood CI, so I approached these with less of a tainted mind. Again Martin Fowler covered these topics in his &lt;a href="https://www.youtube.com/watch?v=aoMfbgF2D_4"&gt;talk&lt;/a&gt;. There is so much to talk about regarding these topics and it is really fascinating if you get the chance to read further into them but here I just wanted to look at &lt;em&gt;what exactly is the difference?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Firstly I want to say that I am by no means an expert in regards to these but just want to share what I thought was a really simple explanation on differentiating the two. Both of these notions revolve around very similar concepts and both have similar end goals. They are looking at being able to get quality software out the door in an automated, reproducible manner, and of course, quickly. One of the biggest pre-requisites is to have an automated pipeline, script where you can, define your pipeline in source code, get everything moving with as little human interaction as possible! Once this is done we can look into getting the software into environments of closer parity to the production environment than our local development machine, again ideally in an automated manner. This is all so we can gain confidence that the release will work as well as be certain that it is done in the same manner every time.&lt;/p&gt;

&lt;p&gt;So at this point we haven't really said much about what is the actual difference between delivery and deployment. Well that is because up until now there kind of isn't much of a difference. However when we get to the final stage delivering the software into production that is where it differs. &lt;strong&gt;Continuous Delivery&lt;/strong&gt; is having the ability to deliver the current master at any given moment, &lt;strong&gt;Continuous Deployment&lt;/strong&gt; is actually doing just that for every commit. You can see how the two can be easily confused, further how continuous delivery is sort of a requirement for continuous deployment.&lt;/p&gt;

&lt;p&gt;Actually implementing these practices can have some unexpected side effects as I have seen. Applying only continuous delivery, teams had no problem practicing continuous integration as they had their master branch raring and ready, but only deployed when it was required. Whereas those practicing continuous deployment actually gravitated away from the continuous integration definition given earlier as they started having these sub-master branches so that they could be confident in their software before it was delivered to production. However I am sure this all depends on the scenario and what your team has in place, I am certainly not saying continuous deployment is bad as I am sure the big tech companies have definitely made this work well for them. Give it a go and see what works best for your team.&lt;/p&gt;

</description>
      <category>ci</category>
      <category>cd</category>
      <category>devops</category>
    </item>
    <item>
      <title>Why you no longer need feature branches</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Sat, 04 Jul 2020 21:50:28 +0000</pubDate>
      <link>https://forem.com/maccoda/why-you-no-longer-need-feature-branches-5bjk</link>
      <guid>https://forem.com/maccoda/why-you-no-longer-need-feature-branches-5bjk</guid>
      <description>&lt;p&gt;The core of what software engineers do is add new and improved features to a service or application. We add a feature the customer requested or address some technical debt. Some of these features can take hours to complete whilst others weeks. All the while you are part of a whole team working on numerous features at once, so the question comes how do merge all these features together once they are ready? &lt;/p&gt;

&lt;h2&gt;
  
  
  Feature Branches
&lt;/h2&gt;

&lt;p&gt;One such option is to use feature branches, of the popular workflow &lt;a href="https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow"&gt;Gitflow&lt;/a&gt;, where a new branch is forked off the main branch and all feature development completed on this branch. The branch is then merged back on when the feature is completed. The benefit here is the isolation from the main branch so it is not disturbed with a half complete feature. It also means that as each feature is completed the merge request only needs to focus on the feature being merged and is independent from any other features that are being worked on in parallel.&lt;/p&gt;

&lt;p&gt;There are some points of concern here. If you are the lucky or unlucky one, your choice, working on the larger feature then you are constantly needing to merge changes from the main branch as each new smaller feature, bug fix, or any work is committed. This adds a fair bit of churn to this workflow as the developer on this larger feature will be going back and forth between solving merge conflicts and implementing the feature.&lt;/p&gt;

&lt;p&gt;This then extends the time it takes for the feature to be completed, leading to the second concern that we end up having long lived feature branches and we start to lose continuous integration. The longer a feature branch lives the more it diverges and the greater the risk of the merge. One way we can definitely improve this is the feature definition process to make these as small as possible but that is separate from the point of this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter Feature Toggles
&lt;/h2&gt;

&lt;p&gt;As continuous delivery has become a more prevalent concept other solutions have developed working on getting these features out as quickly as possible. What this has resulted in is the notion of feature toggles.&lt;/p&gt;

&lt;p&gt;Feature toggles at its simplest can be thought of as a switch. It is either off or on. This switch controls whether or not the feature is off or on in our application. Feature toggles have greater functionality than this but let's explore the benefits before we delve into those.&lt;/p&gt;

&lt;p&gt;A major benefit with feature toggles is its enabling of main branch development, continuous integration just got a whole lot easier. How do we do this? When we start work on our new feature first thing we do is create a feature toggle and wrap our changes in a conditional on the state of the toggle.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (featureToggleService.isLaunched("NEW_HEADER") {
    addHeader(document)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this it means that we can implement the feature over several commits but since the feature toggle is off in production our new feature code isn't activate in production. This also means that the changes become smaller meaning those merge conflicts should be less and less.&lt;/p&gt;

&lt;p&gt;The biggest benefit is the separation of feature work completion and feature launch. With the feature branch model it wouldn't be unheard of to block the deployment to production or block the merge to the main branch until business, product, or testing team are ready for the launch. This can lead to several problematic circumstances, we have a big bang deployment to production containing a lot of changes so if something does go wrong it is harder to pinpoint the exact issue, or in the case the branch isn't merged the developers have to keep this feature branch up to date well after the feature work has been completed. Feature toggles on the other hand allow us to push our code out to production even when it is incomplete. Once the work is completed we can then hand control over to the interested team who is able to be responsible for launching that feature by simply flicking the switch. Decoupling the code completion to the feature launch entirely.&lt;/p&gt;

&lt;p&gt;Another element is the safety lever feature toggles provide. In the feature branch model we need to revert all commits and push it through the pipeline if an issues was found. For the feature toggle it is yet again a simple flick of the switch. Not only is there less time and risk in this "roll back" but it can also be controlled by anyone responsible for the feature and not just the engineering team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other functionalities of feature toggles
&lt;/h2&gt;

&lt;p&gt;Hopefully I have been able to show the benefit of feature toggles however the above is not the only benefits we get.&lt;/p&gt;

&lt;p&gt;Feature toggles can typically be dialed up progressively, meaning we are able to test it on a smaller audience to see if all goes well prior to everyone getting the change. An example being we could give the new feature to 10% of the customer base, then after an hour move to 50%, etc. Giving even more confidence when launching these features.&lt;/p&gt;

&lt;p&gt;We can also usually override the controls for a feature toggle. That is if the testing team want to test the change for a certain test customer or the UX team want to test how actual customers interact with the change this can be done without affecting all customers.&lt;/p&gt;

&lt;p&gt;Depending on which feature toggle service you use there may be even more options available, or you might find your company already has one you can improve.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Feature Toggle Services
&lt;/h2&gt;

&lt;p&gt;Below are a few of the feature toggle services I have heard of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.split.io/"&gt;Split.io&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://launchdarkly.com/"&gt;Launch Darkly&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are plenty more just have a search for "feature toggle service", it is also sometimes called "feature flag"&lt;/p&gt;

</description>
      <category>featuretoggle</category>
      <category>cd</category>
      <category>ci</category>
    </item>
    <item>
      <title>Object construction validation in Kotlin</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Thu, 02 Jul 2020 03:01:10 +0000</pubDate>
      <link>https://forem.com/maccoda/kotlin-class-validation-in-initialization-12cp</link>
      <guid>https://forem.com/maccoda/kotlin-class-validation-in-initialization-12cp</guid>
      <description>&lt;p&gt;Recently I was working with a bit of code where we had written validation logic in the constructor. This was simply validating that the input received from an API call matched what we required. We are currently migrating some of the code across from Java to Kotlin and whilst most of the time this is a straightforward process this validation one wasn't so smooth.&lt;/p&gt;

&lt;p&gt;Let's say you have a &lt;code&gt;FullName&lt;/code&gt; class that requires both a first and last name that cannot be empty. We might have a class something like 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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FullName&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;FullName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;StringUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isBlank&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nc"&gt;StringUtils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isBlank&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;InvalidDataException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Names must not be blank"&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;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&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;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// getters...&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to move this across to a new &lt;code&gt;FullName&lt;/code&gt; Kotlin class instead, below are some of the options we managed create. Each have different styles and benefits so it is up to you which you prefer.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For these examples I am assuming that &lt;code&gt;firstName&lt;/code&gt; and &lt;code&gt;lastName&lt;/code&gt; didn't have to add a &lt;code&gt;null&lt;/code&gt; check. If this is not the case for you simply change the type to a nullable type and perform the check using &lt;a href="https://kotlinlang.org/docs/reference/null-safety.html#safe-calls"&gt;safe calls&lt;/a&gt; or similar. Do this so that you do not get an exception throw from the standard library which is essentially a null pointer exception.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using an &lt;code&gt;init&lt;/code&gt; block
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
    &lt;span class="nf"&gt;init&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;RuntimeException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name cannot be blank"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstName&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lastName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastName&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;&lt;strong&gt;Try this &lt;a href="https://pl.kotl.in/0dQNj7Fb2"&gt;in the playground&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;This has the same interface as the previous Java class so all calling code should fit nicely&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We haven't been able to convert to a &lt;code&gt;data class&lt;/code&gt; where it may have been similar to one in Java. This is because a &lt;code&gt;data class&lt;/code&gt; requires a public field being present in the primary constructor. This can easily be overcome by using your IDE to generate the &lt;code&gt;equals/hashcode&lt;/code&gt; and &lt;code&gt;toString&lt;/code&gt; functions you may have wanted.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Use a static constructor
&lt;/h3&gt;

&lt;p&gt;Using static constructors is a recommended practice as per Effective Java, so one alternative is to put the validation logic into such a function so the class becomes simple. In our above I believe it was done in the way above to match the Spring MVC convention.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;FullName&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;RuntimeException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name cannot be blank"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&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;p&gt;&lt;strong&gt;Try this &lt;a href="https://pl.kotl.in/QfFA6uqJt"&gt;in the playground&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Pros
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We are now able to use the &lt;code&gt;data class&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The class itself doesn't have a 'smart' constructor&lt;/li&gt;
&lt;li&gt;We are able to make all construction through the static method using &lt;code&gt;private constructor&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;We have now changed the signature of the class for the callers of the existing class&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;Invoke&lt;/code&gt; to get both
&lt;/h3&gt;

&lt;p&gt;I have not been able to test how this works with Java interoperability but this option will give you the best of the above two solutions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;FullName&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;companion&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;FullName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;RuntimeException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Name cannot be blank"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;FullName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastName&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;p&gt;&lt;strong&gt;Try this &lt;a href="https://pl.kotl.in/GIfzAHuXR"&gt;in the playground&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
    </item>
    <item>
      <title>Exploring how to use the Law of Demeter</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Sun, 28 Jun 2020 23:21:42 +0000</pubDate>
      <link>https://forem.com/maccoda/exploring-how-to-use-the-law-of-demeter-23je</link>
      <guid>https://forem.com/maccoda/exploring-how-to-use-the-law-of-demeter-23je</guid>
      <description>&lt;p&gt;First and foremost I should define exactly what the &lt;a href="https://en.wikipedia.org/wiki/Law_of_Demeter"&gt;Law of Demeter&lt;/a&gt; is. The Law of Demeter is a design principle of object orientated software, which is also known as the &lt;strong&gt;principle of least knowledge&lt;/strong&gt;. The essence of the principle is that your code should only know about the classes it is directly dealing with and should not be overreaching into classes that those classes know. As with all design principles this is quite intellectual and is difficult to translate to the code we write day to day. Personally I try and find concrete patterns for these that I can use to check if I am either infringing or obeying the principle. The Law of Demeter has a pretty easy check if you are disobeying it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you see a chain of calls (several dots) on a single line you are most likely violating the Law of Demeter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A concrete example of this is something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;isGmailAccount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProfile&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEmail&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"gmail.com"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see here our code has access to some &lt;code&gt;Account&lt;/code&gt; class but this class also knows the internals of the &lt;code&gt;Account&lt;/code&gt; class that is used to determine if it is a Gmail account. Checking against our rule you can easily see it violates the rule as there are &lt;strong&gt;3 dots&lt;/strong&gt; for chained calls and 2 of those are accessing different classes.&lt;/p&gt;

&lt;p&gt;I want to make a quick note here that as per every rule there are exemptions. The key part of the above code is the call chain is accessing different underlying classes. This means that our code goes from only knowing about the &lt;code&gt;Account&lt;/code&gt; class to now also knowing about the &lt;code&gt;Profile&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt; class. Said differently it means that our code is now coupled to the &lt;code&gt;Profile&lt;/code&gt; and &lt;code&gt;Email&lt;/code&gt; class and even their implementation. An example where there is a call chain but no extended class access is easily seen when using combinators on &lt;code&gt;Stream&lt;/code&gt;s in Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MovieTitle&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;movieTitles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;movies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                     &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;Objects:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;notNull&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                     &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;Movie:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;getTitle&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                     &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;collect&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;toList&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;I am going to do a quick example where we work for a bank and we want to send an email to a customer about our new deal if that customer has a checking account with the bank.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jpKwDvRA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mermaid.ink/img/eyJjb2RlIjoiY2xhc3NEaWFncmFtXG4gIEN1c3RvbWVyIG8tLSBcIjEuLipcIiBBY2NvdW50XG4gIEFjY291bnQgLS0gQWNjb3VudFR5cGVcbiAgY2xhc3MgQWNjb3VudFR5cGV7XG4gICAgQ0hFQ0tJTkdcbiAgICBTQVZJTkdTXG4gIH0iLCJtZXJtYWlkIjp7InRoZW1lIjoiZGVmYXVsdCJ9fQ" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jpKwDvRA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mermaid.ink/img/eyJjb2RlIjoiY2xhc3NEaWFncmFtXG4gIEN1c3RvbWVyIG8tLSBcIjEuLipcIiBBY2NvdW50XG4gIEFjY291bnQgLS0gQWNjb3VudFR5cGVcbiAgY2xhc3MgQWNjb3VudFR5cGV7XG4gICAgQ0hFQ0tJTkdcbiAgICBTQVZJTkdTXG4gIH0iLCJtZXJtYWlkIjp7InRoZW1lIjoiZGVmYXVsdCJ9fQ" alt="Class diagram" width="113" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This diagram is intentionally limited to not distract from the key elements.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;These class all seem like we can represent these as plain data objects, they contain some values and we just create a whole bunch of getters. This was exactly my typical approach as I thought "I don't need my data classes to be very smart". So then to implement our requirement above we may need to write some code like this:&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sendPromoEmail&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;shouldSendPromoEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAccounts&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getType&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;AccountType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CHECKING&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findFirst&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isPresent&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;shouldSendPromoEmail&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Build promo message ...&lt;/span&gt;
    &lt;span class="n"&gt;emailService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sendEmail&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEmail&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;promoMessage&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;This definitely does seem to do the job and we meet the requirements but if you look carefully this function doesn't quite satisfy the Law of Demeter. This function only knows about (or is coupled with) the &lt;code&gt;Customer&lt;/code&gt; class. However because we choose to model our data with simple getters we need our function to know what is inside a &lt;code&gt;Customer&lt;/code&gt;. This function is now coupled with how the &lt;code&gt;Customer&lt;/code&gt; models the &lt;code&gt;Account&lt;/code&gt;s it has and is also coupled with the representation of the &lt;code&gt;AccountType&lt;/code&gt; of &lt;code&gt;Account&lt;/code&gt;. This coupling is quite subtle and will typically go unnoticed, I am not even needing to import the &lt;code&gt;Account&lt;/code&gt; class for this to work!&lt;/p&gt;

&lt;p&gt;The root of the problem here is that we chose to use &lt;strong&gt;dumb data classes&lt;/strong&gt; that simply store data. I am not saying these don't work in all cases but I would urge you to consider if by doing so you end up in the same situation we are in here.&lt;/p&gt;

&lt;p&gt;To remove this unneeded coupling we will need the data classes to now tell us more about themselves rather than us ask about it.&lt;/p&gt;

&lt;p&gt;First thing we could do is change the &lt;code&gt;Account&lt;/code&gt; class to tell us if it is a checking account.&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;Account&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;AccountType&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isCheckingAccount&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;AccountType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CHECKING&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;Then our function becomes:&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sendPromoEmail&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;shouldSendPromoEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getAccounts&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;Account:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;isCheckingAccount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findFirst&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                                         &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isPresent&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;shouldSendPromoEmail&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Build promo message ...&lt;/span&gt;
    &lt;span class="n"&gt;emailService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sendEmail&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEmail&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;promoMessage&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;Trust me I know this looks odd and what about all the other account types, does that mean we need to expand the &lt;code&gt;Account&lt;/code&gt; interface for all of this? We may, but only if we ever use them of course. This encapsulation is a trade off as everything is. By creating this method the &lt;code&gt;AccountType&lt;/code&gt; can be encapsulated withing the &lt;code&gt;Account&lt;/code&gt; class, which means if we were ever change the representation or interface it would now only be the &lt;code&gt;Account&lt;/code&gt; class we need to update. We don't need to go back to our &lt;code&gt;sendPromoEmail&lt;/code&gt; function or any others that may be doing the same!&lt;/p&gt;

&lt;p&gt;We have now removed the coupling with the &lt;code&gt;AccountType&lt;/code&gt; and the next to remove is the &lt;code&gt;Account&lt;/code&gt;. To do this we will follow the same pattern but one up the chain. We now want the &lt;code&gt;Customer&lt;/code&gt; class to tell us if it has a checking account rather than us ask for its internals.&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;Customer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;hasCheckingAccount&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                   &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;Account:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;isCheckingAccount&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                   &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;findFirst&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                   &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isPresent&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;Then our function becomes:&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;sendPromoEmail&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Customer&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;shouldSendPromoEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hasCheckingAccount&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;shouldSendPromoEmail&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Build promo message ...&lt;/span&gt;
    &lt;span class="n"&gt;emailService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sendEmail&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getEmail&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;promoMessage&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;There we have it, we have removed the &lt;code&gt;Account&lt;/code&gt; coupling in our function and it only knows about the &lt;code&gt;Customer&lt;/code&gt; class and its interface. Now any changes to the &lt;code&gt;Account&lt;/code&gt; class never reach this method and can ideally all be handled in the &lt;code&gt;Customer&lt;/code&gt; class.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Intro to TDD Basics</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Sun, 28 Jun 2020 22:36:11 +0000</pubDate>
      <link>https://forem.com/maccoda/intro-to-tdd-basics-5782</link>
      <guid>https://forem.com/maccoda/intro-to-tdd-basics-5782</guid>
      <description>&lt;p&gt;TDD is definitely quite the polarizing topic in my experience, whilst not everyone may agree with it I believe it is always valuable to understand it and develop your own opinion on it.&lt;/p&gt;

&lt;p&gt;TDD stands for Test Driven Development and was made made popular by Kent Beck. It is an option of development process that we as software engineers can consider. A wispy way to describe it that instead of the typical practice of writing tests after production code your write it up front. I don't know about you but when I was first introduced to this I thought it was pretty crazy. Let's explore it a little further are there is more to it than simply writing your tests first.&lt;/p&gt;

&lt;h2&gt;
  
  
  The TDD Cycle
&lt;/h2&gt;

&lt;p&gt;A core part of TDD is the TDD cycle. This is a fairly simple cycle and it dictates the exact development cycle one should take when implementing TDD.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Write test - Write code - Refactor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The scope of this cycle is to whatever you define your unit of test. In the object orientated world this will typically match to your class and your unit tests for said class. Let's delve into each of these steps before heading into an example.&lt;/p&gt;

&lt;p&gt;This cycle is also known as the &lt;em&gt;Red-Green-Green&lt;/em&gt; cycle representing the state of the tests at each stage.&lt;/p&gt;

&lt;h3&gt;
  
  
  Write Test
&lt;/h3&gt;

&lt;p&gt;First and foremost, you write a test for the behavior that you expect of the class. Most importantly you expect this to be a &lt;strong&gt;failing&lt;/strong&gt; test. Here of course remember that a compilation error is also a failing test. This test just needs to describe one piece of functionality and fail so that we can implement it and make it pass in the next stage of the cycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Write Code
&lt;/h3&gt;

&lt;p&gt;Now that we have a failing test that describes a piece of functionality we implement that functionality. The interesting part here, and where most people struggle to adopt TDD is that your implementation should be as minimal as possible. That is, your implementation should only make your tests pass and nothing more. The purpose of this is to drive implementation details to be as simple as possible, attempting to remove as much unintentional complexity as possible. It is very common at the end of this step to be looking at your code and not being very impressed, but stick with it because we will get there.&lt;/p&gt;

&lt;p&gt;This stage is complete once we have an implementation that makes all existing, and new tests being written green (or passing).&lt;/p&gt;

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

&lt;p&gt;This is the time where we look at our code and we can make it something that we are proud of. Apply all the refactoring tools from your toolbox but make sure after every refactor you run all your tests and they all stay green. There is not much more to it, there may various different changes to make but it will all depend on the situation.&lt;/p&gt;

&lt;p&gt;Once we have completed this step and all our tests are green now would be a good time to commit (small and often). Then we start the cycle again by adding in a new test and continue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Give It a Try
&lt;/h2&gt;

&lt;p&gt;There is whole lot more that we can delve into for TDD practices and reasons as to why it is good but this post is simply to give an introduction to the topic. We will try give a better understanding with a basic example of a much loved Fizz Buzz program.&lt;/p&gt;

&lt;p&gt;To give a quick recap of the problem so you don't have to search it up, the Fizz Buzz problem is given a number and returns &lt;em&gt;fizz&lt;/em&gt; if it is divisible by 3 and &lt;em&gt;buzz&lt;/em&gt; if it is divisible by 5. If it is both we return &lt;em&gt;fizz buzz&lt;/em&gt;, otherwise return the number.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The example I will show is written in Kotlin for something a little different and we will only look on the function level, just for those looking for the constructor that never was. Also I won't be using any fancy test framework just good old fashioned JUnit.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's write our first test then, when we give our function 1 we expect it to return 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`when&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// Compilation error&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First and foremost this test will have a compilation error which is indeed a failing test because we do not have the function yet written. Whilst this is not a good example, the compilation error step is important because it allows us to consider the API that we want to define. This is because the Fizz Buzz problem has a clear input and output but you can imagine when you create a new class you can use this as a chance to consider API options.&lt;/p&gt;

&lt;p&gt;Now we move into the write code phase. First thing we do is get it compiling and passing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"1"&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 exactly the time when you need to fight the urge to try and over complicate this as this perfectly matches the requirements of the cycle. The tests we have written pass and that is that. Since we don't have much code here, there isn't much to refactor so let's commit and move to our next step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`when&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Moving back to the implementation stage we see it starts get a bit weird but we follow the TDD rules of only implementing enough to satisfy the tests to pass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"1"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s"&gt;"2"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can refactor this a little here, keeping the tests green and then commit again before we start writing our next test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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;Now we encounter a different case, so let's write a test for it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`when&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fizz`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fizz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&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;Back to the implementation step we go!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"fizz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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;Again you can clearly say, hey this won't consider all other cases! Yet again we are only considering making the current tests pass. However you may start to see that this could possibly take a lot longer than the traditional method of implement followed by test, which is definitely a common argument against TDD and in this case it definitely shows.&lt;/p&gt;

&lt;p&gt;In my opinion that is because the speed of TDD here can be improved by defining tests based on expected behavior, rather than for this case just incrementing the input (particularly since not all of our problems will take a simple integer). So let's do exactly that, let's consider the case where it is divisible by 5.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`when&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;buzz`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"buzz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&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;Hopefully you can see this test will obviously be failing so let's make it pass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"fizz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"buzz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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;We are almost there we have a few more behaviors we want to check, these are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A number divisible by 3 but not 3&lt;/li&gt;
&lt;li&gt;A number divisible by 5 but not 5&lt;/li&gt;
&lt;li&gt;A number divisible by both 3 and 5&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let's quickly iterate through these as hopefully by now you have the basic idea. For brevity I will collapse all the tests into a single code block but these would have been added and then implemented one at a time. The implementation blocks I will separate though to make it clear.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`when&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;divisible&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fizz`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fizz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`when&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;divisible&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;buzz`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"buzz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;`when&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;divisible&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="n"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fizzbuzz`&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"fizzbuzz"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&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;Now to the implementation. After testing for divisible by 3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"fizz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"buzz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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;Divisible by 5.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"fizz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"buzz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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;Divisible by both 3 and 5.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fizzBuzz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"fizzbuzz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"fizz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s"&gt;"buzz"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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;Now of course you can then refactor this however best suits your needs (I would prefer braces here, maybe consider using a &lt;code&gt;when&lt;/code&gt; statement, etc). Then there you have it, we have just implemented fizz buzz by TDD.&lt;/p&gt;

&lt;h2&gt;
  
  
  Isn't this a bit much?
&lt;/h2&gt;

&lt;p&gt;As I mentioned before one of the biggest deterrents I have heard around is that this procedure can be so strict and slow down development cycle. Honestly this is definitely true at first but of course you are learning something entirely new, you don't expect to be incredibly effective in a brand new programming language when you first start, so why expect much different when learning a new process?&lt;/p&gt;

&lt;p&gt;TDD is a new mindset as well as process so it definitely does take time. To top it off it does some with some very strict rules making you think just joined the military! This is exactly why for me personally I haven't been able to integrate the explicit procedure in my working process, however I still believe there is a lot of value to TDD.&lt;/p&gt;

&lt;p&gt;For me personally I really like the aspect of having to think of the behavior upfront rather than implementation, which is always a great thing to be focusing on in testing. However sometimes I need to play around with API ideas and test out some possible implementations which may or may not work. This is where I have found TDD a bit harder to utilize.&lt;/p&gt;

&lt;p&gt;Overall there are some great takeaways from TDD and it is up to you how much or little you buy into it and use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping it up
&lt;/h2&gt;

&lt;p&gt;Something I hope you take away from this introduction of TDD is that it is a process with more to it than just writing some tests upfront. There is a nice little cycle (Test-Implement-Refactor) to guide you through the whole process which has some great goals. It allows you to think about the behavior of your system rather than its implementation, making it easier to test and maintain. By only implementing enough to get it to pass it provides a framework to reduce incidental complexity. As well as always providing you a chance to look and consider any refactoring once each cycle, which is much better than having to perform it at a larger scale.&lt;/p&gt;

</description>
      <category>tdd</category>
      <category>testing</category>
    </item>
    <item>
      <title>We don't really need those setters</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Sun, 24 May 2020 20:22:46 +0000</pubDate>
      <link>https://forem.com/maccoda/making-it-immutable-71n</link>
      <guid>https://forem.com/maccoda/making-it-immutable-71n</guid>
      <description>&lt;p&gt;Mutability in terms of software usually will describe the ability to change the internal state of an object once it has been created. Many main design decisions on frameworks and conventions favored having mutable objects and didn't really touch on the concept of immutability. The clearest example of this is the POJO concept in Java, or more specifically the notion of getters and setters.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Notion of Getters and Setters
&lt;/h2&gt;

&lt;p&gt;I will take a short tangent for those not familiar but if you are please skip ahead.&lt;/p&gt;

&lt;p&gt;In a typical Java object you will encapsulate fields by making them &lt;code&gt;private&lt;/code&gt; and unable to be directly accessed. The access to these internal fields are usually protected by getters and setters, which simply get the current value and update the field respectively. A basic example for getting and setting the name of a person can be seen below.&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;Person&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="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&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="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setName&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="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why the Change of Mind?
&lt;/h2&gt;

&lt;p&gt;So if we made all these decisions in languages and frameworks that are now so popular why should I even need to know this? Well this may be a little bleak but in software just because something is popular unfortunately does not mean it is good or right. I would almost argue that it is very hard to find something good in software because designs can be so subjective and things simply move so fast that we can suddenly do things that we couldn't do when our first decisions were made.&lt;/p&gt;

&lt;p&gt;My favorite comparison is comparing older languages to more modern languages. A simple example is Java vs Kotlin. Whilst these both live in a similar realm of the language space I don't believe you can justly compare them as they both came from different times when we knew different things. Java is much older than Kotlin, which was a language specifically designed to bake in all the Java best practices into a language. The fact that we can make an entirely new language with the goal to simply enforce best practices is a clear sign that as an industry we discover new and better things arguably daily.&lt;/p&gt;

&lt;h3&gt;
  
  
  Immutability is now part of the language
&lt;/h3&gt;

&lt;p&gt;In this example one of the things that Kotlin has added that Java didn't have (at least when Kotlin first came out) was the notion of immutability. In Java (and many other languages of the time) the notion of immutability is not as evident. It has the &lt;code&gt;final&lt;/code&gt; keyword to enforce variables are set only once but it is an extra keyword always needed to be added and certainly not what you learn in most beginner courses. Whereas more modern languages have introduced separate keywords for a mutable and immutable variable. This makes the thought about this variable's mutability far more apparent.&lt;/p&gt;

&lt;p&gt;Let us take a really basic example of getting a result back from a method invocation. In Java I can simply make the types line up and we are good to go. I can almost forget about the mutability question because it isn't required at all. In fact this happens a lot for me so I have actions performed on save to add the &lt;code&gt;final&lt;/code&gt; keywords for me.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;invoke&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if I want it immutable I need to remember my &lt;code&gt;final&lt;/code&gt; keyword:&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;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myObject&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;invoke&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whereas in Kotlin, thanks to advances in what we can achieve within the compiler the notion of making the types line up is not as important and the developer's responsibility is to choose the correct keyword to define the mutability of the result.&lt;/p&gt;

&lt;p&gt;If the result is to be immutable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the result is to be mutable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see from this example, the notion of immutability has become more prevalent in the developers decisions. To the point where the compiler can tell you that the mutable variable you made could actually be mutable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mutability can bite
&lt;/h3&gt;

&lt;p&gt;So we have explored how the notion of mutability is now brought closer to front in terms of our development, but still have not answered why? The answer is pretty simple, making everything mutable and not addressing that upfront led to a large cognitive load for developers and with that bugs will follow. The issue is embedded in the fact that it lead to objects having vast amounts of way of changing state. The more possible states an object could be in the harder it is to reason with and that is what happened.&lt;/p&gt;

&lt;p&gt;An immutable object is one that is set at construction and has no way in which it is able to change afterwards. Of course this does come with a few issues you need to keep track of and I will discuss those later. The main point I want to express here is that mutability can cause difficult to manage code and we should be aware of our decision to use mutable or immutable objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Converting to Immutable Objects
&lt;/h2&gt;

&lt;p&gt;So we have hinted at why you might want to try immutability but how can you do it? Every language has its own way to manage immutability and the extent to which you can achieve immutability. We will stick with Java for the time being. Let's return to the person example before but make it a little more interesting. Let's say the person has a name and a phone number and we want to make it a basic immutable object. It would look something like this:&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;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&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;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&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;Person&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="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;phoneNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getPhoneNumber&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;phoneNumber&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key points to note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whilst not exactly data immutable, having the &lt;code&gt;final class&lt;/code&gt; means that the class is immutable and
cannot be extended&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;private final&lt;/code&gt; variables ensure they are set once and only available within the class scope&lt;/li&gt;
&lt;li&gt;There are no setters, only getters&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Not Everything is Perfectly Immutable
&lt;/h3&gt;

&lt;p&gt;Whilst the above example is the closest we can get to perfect immutable in Java there are some things to consider with these, since Java as a language chose mutability as the default.&lt;/p&gt;

&lt;p&gt;One of the biggest confusions is that following those steps above is all you need for it to be immutable but let us consider an extension on the example. Let's say that this person object now needs to cater for multiple phone numbers and my naive implementation is to do this by using a list.&lt;br&gt;
Perhaps it could look something like 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="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&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;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;phoneNumbers&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;Person&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="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;phoneNumbers&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;phoneNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;phoneNumbers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

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

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getPhoneNumbers&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;phoneNumbers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this looks pretty good, all our data is only set once and we only have getters. The problem here is that a &lt;code&gt;List&lt;/code&gt; is not an immutable object whereas it just so happened in our earlier example &lt;code&gt;String&lt;/code&gt; was. Let's delve a little deeper into how immutability breaks here.&lt;/p&gt;

&lt;p&gt;The client code for the above &lt;code&gt;Person&lt;/code&gt; class could look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPhoneNumbers&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"555444333"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Despite the poor choice of example phone number there is also something else concerning here, the client is modifying the list of phone numbers. Now sure enough because the &lt;code&gt;Person&lt;/code&gt; class returns the reference to their internal field this is going to modify the state of the original &lt;code&gt;person&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Let's have a look at what I mean by that (the comments on the right show the value printed)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Frank"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;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;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPhoneNumbers&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;

&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPhoneNumbers&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"555444333"&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;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPhoneNumbers&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;size&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So even without any setters we can see that we have still actually written an interface that allows mutability, but we really want this to be an immutable object so how can we do this? Unfortunately this does involve a little bit of work in Java but certainly does give us the immutability benefits so if it is what you are working for then definitely the cost is worth it.&lt;/p&gt;

&lt;p&gt;The general practice is you need to return the value not the reference. For a list this is by making a copy of the list and returning that.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Warning&lt;/em&gt;: Making a shallow copy may not always be enough, if the contained object is not immutable then a deep copy is necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to be aware of with Immutability
&lt;/h2&gt;

&lt;p&gt;Nothing is a silver bullet and immutability certainly has some caveats to be aware of, here are a few obvious ones:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If the type returned from the getters is not immutable itself, then your class has just become mutable. (Refer to above section)&lt;/li&gt;
&lt;li&gt;Java does support reflection so people can get fancy with this.&lt;/li&gt;
&lt;li&gt;A lot of frameworks have been built based on mutability so can be difficult to integrate&lt;/li&gt;
&lt;li&gt;Can create many copies of objects which can be costly&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last one is particularly important and one of the main reasons immutability wasn't popular earlier. Having pure immutable objects means that you need different copies for every possible value which can consume a lot of your memory resources. Obviously we have a lot more memory we can use nowadays but that does not mean we can disregard this limitation. When making objects immutable consider this, particularly when needing to perform deep copies as these are certainly not free.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Immutable Objects
&lt;/h2&gt;

&lt;p&gt;Finally, why am I writing about this, why tell people to aim for immutability where possible? Quite simply it makes me think less. In our profession as counter intuitive as that may sound I find it is a big influencer because there will always be other difficult things wanting your attention.&lt;/p&gt;

&lt;p&gt;If you have immutable objects you do not need to worry about shared state and who is modifying what. This particularly becomes useful in concurrency. You can share as many immutable objects between thread as you want and you won't have to worry about landing in a corrupt state because you cannot modify the state of the object but only create a new one.&lt;/p&gt;

&lt;p&gt;For the simplicity it has added to my development I would definitely recommend choosing immutable as your standard and then consciously making the decision to make your classes mutable. This has even become a standard for programming languages themselves, Rust is an example of such a language.&lt;/p&gt;

&lt;p&gt;Regardless of if you agree or not on the value of immutability, I urge you to at least consider the mutability facet of a class when you are designing and not let it be an after thought because the language you develop in doesn't accentuate it.&lt;/p&gt;

</description>
      <category>immutable</category>
    </item>
    <item>
      <title>Why would I even use dependency injection?</title>
      <dc:creator>Dylan Maccora</dc:creator>
      <pubDate>Mon, 18 May 2020 02:54:17 +0000</pubDate>
      <link>https://forem.com/maccoda/why-would-i-even-use-dependency-injection-oeh</link>
      <guid>https://forem.com/maccoda/why-would-i-even-use-dependency-injection-oeh</guid>
      <description>&lt;p&gt;I was inspired to write this post and hopefully a little series on some of these key concepts of&lt;br&gt;
software as I was asked "Why would I need to use dependency injection?". This brought me back to&lt;br&gt;
when I was first learning these industry staples that you never get taught learning computer&lt;br&gt;
science. Due to this I was hoping to write a little series around my learning experiences and&lt;br&gt;
understanding of these concepts because I have always found the more perspectives you get the easier&lt;br&gt;
it is to form your own understanding. This is the first one for the series so let's see how far we&lt;br&gt;
can go.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Dependency Injection?
&lt;/h2&gt;

&lt;p&gt;Alright now that the little preamble is over with lets start out like any good problem solver and&lt;br&gt;
define the &lt;em&gt;What&lt;/em&gt; of the problem. According to our good friends at Wikipedia we define dependency&lt;br&gt;
injection as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In software engineering, dependency injection is a technique whereby one object supplies the&lt;br&gt;
dependencies of another object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Dependency_injection"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So this is pretty good and correct, assuming you understand the concept to begin with. So let's try&lt;br&gt;
break it down a bit.&lt;/p&gt;

&lt;p&gt;I first learnt this with Java, so one simple trick to think of dependency injection is moving all of&lt;br&gt;
the &lt;code&gt;new&lt;/code&gt; keywords out of your classes. Now this is a pretty dumb thing to say because something&lt;br&gt;
obviously has to instantiate your classes and that is entirely correct but the point of dependency&lt;br&gt;
injection is to create that separation between where your classes and their dependencies are&lt;br&gt;
instantiated and where you write the fun problem solving logic. So the concept of dependency&lt;br&gt;
injection is being able to &lt;strong&gt;give&lt;/strong&gt; your classes their dependencies rather than you class&lt;br&gt;
instantiating its own dependencies. A term you will hear a lot around this topic is Inversion of&lt;br&gt;
Control (IOC). You are now giving control of what your classes dependencies are to a class higher up&lt;br&gt;
the chain.&lt;/p&gt;

&lt;p&gt;Don't worry there is a lot of writing here and my description is arguably confusing in itself but I&lt;br&gt;
will clarify this with some examples soon! So stick with me!&lt;/p&gt;
&lt;h2&gt;
  
  
  How do I use Dependency Injection?
&lt;/h2&gt;

&lt;p&gt;There are a lot of fancy frameworks that you can use for dependency injection, no matter what&lt;br&gt;
language you develop in. However I have no intention of showing how to use those frameworks because&lt;br&gt;
if you want to learn you should practice it on the bare metal. I always enjoy getting my hands dirty&lt;br&gt;
with this (as much as they can typing on a keyboard) because I find that to be the best way to&lt;br&gt;
understand, making the mistakes and finding the answers.&lt;/p&gt;

&lt;p&gt;Saying that let's get onto our example. Since I am not feeling incredibly creative today I am going&lt;br&gt;
to take the classic shopping cart example. Our task is to make a shopping cart class that will allow&lt;br&gt;
our customers to buy some items. The code below is one possible way of doing this (although please&lt;br&gt;
don't use this to implement an actual shopping cart).&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;ShoppingCart&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;CreditCard&lt;/span&gt; &lt;span class="n"&gt;creditCard&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;LineItems&lt;/span&gt; &lt;span class="n"&gt;items&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;ShoppingCart&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;creditCardNumber&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;creditCard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CreditCard&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;creditCardNumber&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LineItems&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;creditCard&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;total&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;So as you can see this shopping cart implementation needs to have a &lt;code&gt;CreditCard&lt;/code&gt; to charge and some&lt;br&gt;
&lt;code&gt;LineItems&lt;/code&gt; to define how much to charge. Therefore it is pretty easy to see that the dependencies of&lt;br&gt;
&lt;code&gt;ShoppingCart&lt;/code&gt; are &lt;code&gt;CreditCard&lt;/code&gt; and &lt;code&gt;LineItems&lt;/code&gt;. A pretty easy way to see this in Java is they will&lt;br&gt;
be fields of the class, you really only define a field if you need to use it perform some tasks.&lt;/p&gt;

&lt;p&gt;However there are &lt;code&gt;new&lt;/code&gt; keywords here so you can see the dependencies are not injected into&lt;br&gt;
&lt;code&gt;ShoppingCart&lt;/code&gt;, rather &lt;code&gt;ShoppingCart&lt;/code&gt; knows exactly how to create its dependencies. It knows it&lt;br&gt;
needs a &lt;strong&gt;credit card number&lt;/strong&gt; and a &lt;strong&gt;list of products&lt;/strong&gt;. In fact this is exactly why dependency&lt;br&gt;
injection is important because it means your class does not need to know how to create its&lt;br&gt;
dependencies but only how to &lt;strong&gt;use&lt;/strong&gt; its dependencies. Instead of providing the parameters to&lt;br&gt;
construct the dependencies we should have instead provided the dependencies themselves and&lt;br&gt;
constructed those elsewhere, perhaps like this:&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;ShoppingCart&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;CreditCard&lt;/span&gt; &lt;span class="n"&gt;creditCard&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;LineItems&lt;/span&gt; &lt;span class="n"&gt;items&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;ShoppingCart&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CreditCard&lt;/span&gt; &lt;span class="n"&gt;creditCard&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;LineItems&lt;/span&gt; &lt;span class="n"&gt;items&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;creditCard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;creditCard&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;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;creditCard&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;charge&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;total&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;So we can now create it elsewhere&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;Factory&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ShoppingCart&lt;/span&gt; &lt;span class="nf"&gt;shoppingCart&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;creditCardNumber&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ShoppingCart&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;CreditCard&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;creditCardNumber&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;LineItems&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have separated our creation code from our domain logic code. In doing this it has provided us&lt;br&gt;
dependency injection as you can see our &lt;code&gt;ShoppingCart&lt;/code&gt; no longer has any &lt;code&gt;new&lt;/code&gt; keywords!&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Dependency Injection?
&lt;/h2&gt;

&lt;p&gt;The most obvious question now is, "Why would I do that? That looks like more code!". This is indeed&lt;br&gt;
correct we do have more code but the number of lines you have written is never a sole indicator of&lt;br&gt;
the quality of the code. Instead what you should be looking at is "Has this made it easier for me to&lt;br&gt;
change the code as requirements change?" or "Is this code easily testable?". Making our code use&lt;br&gt;
dependency injection has allowed us to say yes on both of those fronts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Changing with Requirements or Design
&lt;/h3&gt;

&lt;p&gt;The biggest achievement we have made is we are now able to develop to an interface. That is,&lt;br&gt;
&lt;code&gt;ShoppingCart&lt;/code&gt; doesn't need to know anything about how &lt;code&gt;CreditCard&lt;/code&gt; or &lt;code&gt;LineItems&lt;/code&gt; work under the&lt;br&gt;
hood, or if they are even concrete classes. All it needs to know is that it can call &lt;code&gt;charge&lt;/code&gt; and&lt;br&gt;
&lt;code&gt;total&lt;/code&gt; on them respectively. Therefore if we only supported one type of credit card and we needed&lt;br&gt;
to add another one, so long as it implements &lt;code&gt;charge&lt;/code&gt; for the &lt;code&gt;CreditCard&lt;/code&gt; interface our&lt;br&gt;
&lt;code&gt;ShoppingCart&lt;/code&gt; need not change.&lt;/p&gt;

&lt;p&gt;In a different direction, if the design of &lt;code&gt;LineItems&lt;/code&gt; were to change and it needed something&lt;br&gt;
different to construct itself, our &lt;code&gt;ShoppingCart&lt;/code&gt; is now unaffected. The only place it needs to&lt;br&gt;
change is where we create it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testability
&lt;/h3&gt;

&lt;p&gt;Something else that dependency injection has aided with is making tests easier to write. If we had&lt;br&gt;
of kept it the old way, testing &lt;code&gt;ShoppingCart&lt;/code&gt; would be near impossible without needing to charge an&lt;br&gt;
actual credit card. To avoid this we would need to use reflection to inject a mock and capture all&lt;br&gt;
the interactions, doable but more complicated than it need be. Now we can simply make a test double&lt;br&gt;
that looks like a &lt;code&gt;CreditCard&lt;/code&gt; and then capture the interactions on that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing Up
&lt;/h2&gt;

&lt;p&gt;Hopefully through this very basic example you can see how you can use dependency injection in your&lt;br&gt;
current work as well as how it is helpful. As with a lot of these practices it is hard to see the&lt;br&gt;
benefit in the small scale but once your system grows and the number of moving parts increases its&lt;br&gt;
value becomes apparent.&lt;/p&gt;

</description>
      <category>di</category>
      <category>bestpractices</category>
    </item>
  </channel>
</rss>
