<?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: Martin Gaston</title>
    <description>The latest articles on Forem by Martin Gaston (@martingaston).</description>
    <link>https://forem.com/martingaston</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%2F164256%2F0776fbed-13e2-4a63-8338-e7985c565c98.jpeg</url>
      <title>Forem: Martin Gaston</title>
      <link>https://forem.com/martingaston</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/martingaston"/>
    <language>en</language>
    <item>
      <title>On avoiding getting distracted by squirrels</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Sun, 19 Jan 2020 19:49:03 +0000</pubDate>
      <link>https://forem.com/martingaston/on-avoiding-getting-distracted-by-squirrels-1g4c</link>
      <guid>https://forem.com/martingaston/on-avoiding-getting-distracted-by-squirrels-1g4c</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;My master made me this collar. He is a good and smart master and he made me this collar so that I may tal--SQUIRREL! - &lt;em&gt;Dug&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At the start of 2019 I wrote a list of self-learning goals which I then proceeded to largely ignore and abandon for the year because other stuff got in the way. It's not that I wasn't spending time &lt;em&gt;working&lt;/em&gt; on learning some skills, it's more that I was getting distracted by little bits all of the time.&lt;/p&gt;

&lt;p&gt;To contextualise this, consider Redux; you've got to do a bit of Redux work on a codebase. Perhaps start with Dan Abramov's quintessential &lt;a href="https://egghead.io/courses/getting-started-with-redux"&gt;Egghead course&lt;/a&gt;, but then you've got to move on to all the middleware because the internet says so. So you've gone through Redux Sagas, or Thunks, or a weird mix of both of them in the case of one codebase I was working last year. Then you ship some shabby Redux, so then you've just &lt;strong&gt;got&lt;/strong&gt; to read up on design patterns and architecture for peace of mind. Someone will mention Elm, which is worth looking into. And then someone who is trying to be helpful will tell you not to bother because of React Hooks, which is great advice other than the fact you really do have to use Redux anyway because &lt;em&gt;the codebase you are already working on is using it&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This is all well and good, but what if learning all of that context &lt;em&gt;around&lt;/em&gt; Redux doesn't actually progress you any further to the goals you're looking to pursue? It all feels a bit, well, unstructured. While I'm sure there's plenty of benefit from doing such things, in the short-term maybe you've ended up over-investing in areas you don't particularly need right now. These are all &lt;em&gt;squirrels&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A relatively common bit of advice I've heard and read is that you should focus your learning time on starting and finishing actual projects, and I think there's some sound logic to that. Outside of the other benefits of actually getting something done, which might also lead to something down the line, it also gives you an educational tether. Once you've invested a couple of months in building an awesome Django project it's perhaps more likely that you'll continue forward with it, rather than dipping your toes into any number of web frameworks - which I often find quite tempting. I am really good at &lt;em&gt;starting&lt;/em&gt; a bunch of projects and &lt;em&gt;much less&lt;/em&gt; accomplished at finishing them. &lt;/p&gt;

&lt;p&gt;The internet is a magnificent tool and there is an embarrassment of riches when it comes to affordable courses, tutorials and guides, but actually working out how to navigate a path through all of it can be massively challenging. &lt;/p&gt;

&lt;p&gt;I'd love to learn more about understand and map out a full pedagogic journey, but that's a problem for another day - right now I actually need to do some deeper (rather than wider) learning.&lt;/p&gt;

&lt;p&gt;With that in mind, these are the resources I'm looking to go through in 2020: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.obeythetestinggoat.com/"&gt;Test-Driven Development With Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.amazon.co.uk/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052"&gt;Working Effectively With Legacy Code&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://cs50.harvard.edu/games/"&gt;CS50's Introduction To Game Development&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.amazon.co.uk/Math-Overboard-Basic-Adults-Part/dp/1457514818/ref=sr_1_3?keywords=math%20overboard&amp;amp;qid=1579461369&amp;amp;sr=8-3"&gt;Math Overboard&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bigmachine.io/products/a-curious-moon/"&gt;A Curious Moon&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.amazon.co.uk/Structure-Interpretation-Computer-Electrical-Engineering/dp/0262510871"&gt;Structure and Interpretation of Computer Programs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Objection! Let's play with primitives in JavaScript</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Fri, 02 Aug 2019 14:53:22 +0000</pubDate>
      <link>https://forem.com/martingaston/objection-let-s-play-with-primitives-in-javascript-4jj7</link>
      <guid>https://forem.com/martingaston/objection-let-s-play-with-primitives-in-javascript-4jj7</guid>
      <description>&lt;p&gt;I like to spend lazy weekends eating my body weight in Pringles, playing videogames and also really digging into some of the fundamentals of programming languages to get an appreciation for what's going on. I've been fascinated by methods and primitives in JavaScript lately, spurred on as I look to branch out a deeper understanding of standard data types in both object-oriented and functional patterns. &lt;/p&gt;

&lt;p&gt;While you certainly don't need to worry about a lot of this - it's really something the language largely abstracts away from you behind its individual implementation - it's nice to get a sense of what's going on. &lt;/p&gt;

&lt;p&gt;I'm going to be using JavaScript for most of this, but also with little cameos from Ruby and Elixir, though you certainly don't need to know either of those languages.&lt;/p&gt;

&lt;p&gt;Let's play with some JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;foo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;
&lt;span class="c1"&gt;// 'string'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Consider the humble &lt;code&gt;string&lt;/code&gt;, a proper salt of the earth, workhorse of data type. Strings are model citizens. They work hard all day and don't ask questions, and as long as they've got enough cash left over to take the family on a couple of holidays a year they're pretty contented. &lt;/p&gt;

&lt;p&gt;Over on the fields of JavaScript, a &lt;code&gt;string&lt;/code&gt; is one of its six primitives, which are kind of like the language's Infinity Stones - &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;symbol&lt;/code&gt;. Primitives in JavaScript are immutable, although can be replaced by other primitives with assignment. Primitives are lightweight, like the delicious &lt;a href="https://uk.fage/yoghurts/fage-total"&gt;Total 0%&lt;/a&gt; of data types, and don't have methods or properties - or the extra processing needs that come with them. &lt;/p&gt;

&lt;p&gt;Wait a second, woah woah woah &lt;em&gt;thank you very much&lt;/em&gt;, how come you can do &lt;em&gt;this&lt;/em&gt; then?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// "FOO"&lt;/span&gt;
&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, with the exception of &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;null&lt;/code&gt;, each primitive also has a complementary &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript"&gt;primitive wrapper object&lt;/a&gt; that, perhaps unsurprisingly, wraps around it - so that's&lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Number&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt; and &lt;code&gt;Symbol&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When you're as smart as Kyle Simpson (❤️) you can &lt;a href="https://github.com/getify/You-Dont-Know-JS/blob/master/types%20&amp;amp;%20grammar/ch3.md"&gt;actually dig deep into what's going on under the hood&lt;/a&gt;, but essentially the &lt;code&gt;String&lt;/code&gt; object (which still has its own constructor) will create an object - so &lt;em&gt;not&lt;/em&gt; a primitive - around  a given &lt;code&gt;string&lt;/code&gt; primitive, which is still accessible with the &lt;code&gt;valueOf&lt;/code&gt; method. This, I believe, is also loosely analogous to the implementation of primitives in Java, but I'm certainly no expert there: the most I know about Java is the &lt;a href="https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition"&gt;FizzBuzz Enterprise Edition&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="c1"&gt;// 'object'&lt;/span&gt;
&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// 'string'&lt;/span&gt;
&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, JavaScript will also handle this wrapping (also commonly called &lt;em&gt;boxing&lt;/em&gt; or &lt;em&gt;autoboxing&lt;/em&gt;) automatically when you attempt to access a method or property on a primitive, and then immediately following its evaluation it will automatically fling this new, temporary, single-use object off to the garbage collector.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// variable bound to string primitive&lt;/span&gt;
&lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;!!!!!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// create a String wrapper object and add a shout method, which is evaluated immediately and is then sent to the garbage collector&lt;/span&gt;
&lt;span class="c1"&gt;// "HELLO!!!!!"&lt;/span&gt;
&lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shout&lt;/span&gt; &lt;span class="c1"&gt;// creates a NEW String wrapper object, where shout doesn't exist, so returns undefined &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Alas, poor &lt;code&gt;String&lt;/code&gt;! I knew him. &lt;/p&gt;

&lt;p&gt;You should also never &lt;em&gt;ever&lt;/em&gt; &lt;strong&gt;EVER&lt;/strong&gt; (probably) use these constructors in real-life, as even though it might make sense that creating these objects manually - especially for repetitive loops and the like - would be more efficient, the &lt;strong&gt;reverse&lt;/strong&gt; is actually true. Implementations of JavaScript have worked out how to use the single-use wrapped objects way more effectively, which is a peculiar mishmash of timesaving and functionality that makes JavaScript such a hot and cold pleasure to deal with.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;performance&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;perf_hooks&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// only needed for Node&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;test_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;t0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10000000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
    &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;performance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; took &lt;/span&gt;&lt;span class="p"&gt;${(&lt;/span&gt;&lt;span class="nx"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; seconds`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;test_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wrapped string primitive&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;test_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Instantiated String object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Wrapped string primitive took 0.011956994999200105 seconds&lt;/span&gt;
&lt;span class="c1"&gt;// Instantiated String object took 1.9173262799978257 seconds&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's put JavaScript to one side and take a look at some other implementations. How's our little &lt;code&gt;foo&lt;/code&gt; doing over in the ancient zen gardens of Ruby?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'foo'&lt;/span&gt;
&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="c1"&gt;# String&lt;/span&gt;
&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt; &lt;span class="c1"&gt;# [String, Comparable, Object, Kernel, BasicObject]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In Ruby, all data is represented by objects. This helps Ruby become an elegant object orientated language, and Ruby coders to easily unwind in the evening without having to decompress out these particular day terrors. "Ruby follows the influence of the Smalltalk language by giving methods and instance variables to all of its types", &lt;a href="https://www.ruby-lang.org/en/about/"&gt;says the language itself&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;By tracing the &lt;code&gt;String&lt;/code&gt; object through its ancestors, we see that it - and every object in Ruby - inherits from &lt;code&gt;BasicObject&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;magic_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;magic_number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ancestors&lt;/span&gt; &lt;span class="c1"&gt;# [Integer, Numeric, Comparable, Object, Kernel, BasicObject]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, all data types in Ruby are objects, and there's no wrapping/autoboxing going on behind the scenes, and even the JavaScript-style concept of primitives is non-existent. &lt;/p&gt;

&lt;p&gt;Because everything starts life as an object, this means you can reopen a class and add your own methods, which I am &lt;strong&gt;legally required&lt;/strong&gt; to tell you can be pretty dangerous: you should always &lt;em&gt;always&lt;/em&gt; &lt;strong&gt;always&lt;/strong&gt; make sure you ensure your code is maintainable and sticking your snout into the standard library is a great way to cause a total meltdown in real life. With that said, it's still &lt;em&gt;pretty cool&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upcase&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s2"&gt;"!!!!!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt;
&lt;span class="c1"&gt;# "HELLO!!!!!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Elsewhere, in more functional languages, the concepts of objects and classes is usually a distant memory - so accessing methods directly on your data types is a no-no. If we follow our venerable &lt;code&gt;foo&lt;/code&gt; over to Elixir, we can see that instead of accessing properties and methods we're now passing our data types as parameters to functions in the &lt;code&gt;String&lt;/code&gt; module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight elixir"&gt;&lt;code&gt;&lt;span class="n"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"foo"&lt;/span&gt;
&lt;span class="n"&gt;is_bitstring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# true&lt;/span&gt;
&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upcase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# "FOO"&lt;/span&gt;
&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is just a brief stop in Elixir, but it's great to see that our &lt;code&gt;foo&lt;/code&gt; is doing well, even though we can barely recognise them from those early JavaScript days - they just grow up so fast!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Understanding the Java Classpath: Building a Project Manually</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Sat, 27 Jul 2019 10:37:40 +0000</pubDate>
      <link>https://forem.com/martingaston/understanding-the-java-classpath-building-a-project-manually-3c3l</link>
      <guid>https://forem.com/martingaston/understanding-the-java-classpath-building-a-project-manually-3c3l</guid>
      <description>&lt;p&gt;This is the second post in a series looking to understand Java projects. Earlier, &lt;a href="https://dev.to/martingaston/getting-started-with-a-java-environment-on-macos-541c"&gt;we looked at how Java actually gets installed onto a macOS system&lt;/a&gt;, and now we're going to build a basic app. Then we'll move on to incorporating the same app with help from Gradle, a popular build tool, and we'll finish by incorporating our project into the IntelliJ IDE.&lt;/p&gt;

&lt;h3&gt;
  
  
  Our directory structure
&lt;/h3&gt;

&lt;p&gt;We're going to be compiling a totally jazzed up version of Hello World, and our overblown example will make use of packages, libraries and tests because it's a more indicative of a project we'd spot in the wild. This project is intended for people who have worked a little with Java and isn't going to dwell much on syntax outside of tooling.&lt;/p&gt;

&lt;p&gt;We'll be loosely following the conventions of the Maven &lt;a href="https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html"&gt;standard directory layout&lt;/a&gt;, which is admittedly a little overkill for our tiny app - but we're also going to charge ahead with it because it's indicative of the conventions we'll commonly run into when working with Java. Here's how that looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── bin
├── lib
│   ├── jfiglet-0.0.8.jar
│   └── junit-platform-console-standalone-1.4.2.jar
└── src
    ├── main
    │   └── java
    │       └── space
    │           └── gaston
    │               ├── greeter
    │               │   └── Greeter.java
    │               ├── helloWorld
    │               │   └── HelloWorld.java
    │               └── textEffects
    │                   └── Bubble.java
    └── test
        └── java
            └── space
                └── gaston
                    ├── greeter
                    │   └── GreeterTest.java
                    └── textEffects
                        └── BubbleTest.java
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's unpack what we've got here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;bin&lt;/code&gt; will contain our compiled &lt;code&gt;.class&lt;/code&gt;  files&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lib&lt;/code&gt; will hold our third-party libraries. In this case we're using JFiglet and the JUnit test runner, which we'll get back to later.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src&lt;/code&gt; will house our &lt;code&gt;.java&lt;/code&gt; source code. Within &lt;code&gt;src&lt;/code&gt; you have subdirectories &lt;code&gt;main&lt;/code&gt; and &lt;code&gt;test&lt;/code&gt; , which both have a &lt;code&gt;java&lt;/code&gt; subdirectory to denote the language, and then the &lt;a href="https://docs.oracle.com/javase/tutorial/java/package/packages.html"&gt;standard Java package hierarchy&lt;/a&gt;. Eventually, after months of digging, you finally stumble upon our actual &lt;code&gt;.java&lt;/code&gt; files. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Just a quick caveat: I haven't checked the &lt;code&gt;lib&lt;/code&gt; folder into Git so you'll need to grab the &lt;a href="https://search.maven.org/artifact/com.github.lalyos/jfiglet/0.0.8/jar"&gt;JFiglet&lt;/a&gt; and &lt;a href="https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.4.2/junit-platform-console-standalone-1.4.2.jar"&gt;JUnit&lt;/a&gt; &lt;code&gt;.jar&lt;/code&gt; files from Maven if you're looking to build this project yourself. This is, partially, to expose how fiddly it is to manage dependencies manually and help us understand how, later on, how awesome it is that other tools can come to our rescue.&lt;/em&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  .java, .class and .jar
&lt;/h3&gt;

&lt;p&gt;Okay, so I threw around quite a few filetypes just then. I think it's entirely common to be hidden from a lot of these specifics by our IDE, so here's a quick summary of what each of those types does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;.java&lt;/code&gt; file is a plain-text file of human-readable Java source code - though admittedly that 'readable' designation is, ahem, rather debatable when it comes to certain aspects of Java syntax. Basically, &lt;code&gt;.java&lt;/code&gt; files are the files we squishy human flesh monsters sling our code in. &lt;/li&gt;
&lt;li&gt;A &lt;code&gt;.class&lt;/code&gt; file is compiled Java bytecode that can be executed by the Java Virtual Machine, an admittedly snazzy tool which runs our Java programs. These cold, mechanical files are the ones the computer likes read. &lt;/li&gt;
&lt;li&gt;A &lt;code&gt;.jar&lt;/code&gt; file is an archive of &lt;code&gt;.class&lt;/code&gt; files (and other necessary resources) all neatly zipped up for convenience. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  javac and java
&lt;/h3&gt;

&lt;p&gt;Two of our JDK binaries are responsible for compiling and running our code: &lt;code&gt;javac&lt;/code&gt; and &lt;code&gt;java&lt;/code&gt;. In short, &lt;code&gt;javac&lt;/code&gt; is responsible for turning our &lt;code&gt;.java&lt;/code&gt; files into &lt;code&gt;.class&lt;/code&gt; files that &lt;code&gt;java&lt;/code&gt; can run. &lt;/p&gt;

&lt;p&gt;If we put together a totally barebones &lt;code&gt;HelloWorld.java&lt;/code&gt;, we can then feed it as an argument to &lt;code&gt;javac&lt;/code&gt; and run it with &lt;code&gt;java&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;HelloWorld&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Oh no, not another Hello World example..."&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;Without multiple &lt;code&gt;.java&lt;/code&gt; files, external libraries or output directories to worry about, compiling and running this is as straightforward as sending the file path to &lt;code&gt;javac&lt;/code&gt; and then running the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;javac HelloWorld.java
&lt;span class="nv"&gt;$ &lt;/span&gt;java HelloWorld
Oh no, not another Hello World example...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice how the &lt;code&gt;.class&lt;/code&gt; file will be currently be compiled into the same directory  as its companion source code, and that we call the &lt;code&gt;HelloWorld&lt;/code&gt; class directly with &lt;code&gt;java&lt;/code&gt; with no &lt;code&gt;.class&lt;/code&gt; extension - the latter would trigger a dreaded &lt;code&gt;java.lang.ClassNotFoundException&lt;/code&gt; error, but that's a story for another day. It turns out we use &lt;code&gt;java&lt;/code&gt; to run Java classes, not Java &lt;code&gt;.class&lt;/code&gt; files.&lt;/p&gt;

&lt;h3&gt;
  
  
  Classpath
&lt;/h3&gt;

&lt;p&gt;Another potential error we might see right about now is a &lt;code&gt;java.lang.NoClassDefFoundError&lt;/code&gt;, which usually comes about because there's a &lt;code&gt;.class&lt;/code&gt; file that was created at compile time (with &lt;code&gt;javac&lt;/code&gt;) but has got lost somewhere when we're trying to run it with &lt;code&gt;java&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Be warned to those of a fragile constitution, the next word is enough to give you the vapours. This frightful concept has dashed the hopes and dreams of event the strongest minds, adventurer. Abandon all hope all ye who enter... the &lt;em&gt;classpath&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Let's zoom in our our main &lt;code&gt;.java&lt;/code&gt; files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── greeter
│   └── Greeter.java
├── helloWorld
│   └── HelloWorld.java
└── textEffects
    └── Bubble.java
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;Greeter.java&lt;/code&gt;, &lt;code&gt;HelloWorld.java&lt;/code&gt; and &lt;code&gt;Bubble.java&lt;/code&gt; are all being stored in different packages (which, in Java, also means different directories) and have requirements of their own. &lt;code&gt;HelloWorld&lt;/code&gt; includes both a &lt;code&gt;Greeter&lt;/code&gt; and a &lt;code&gt;Bubble&lt;/code&gt;, with &lt;code&gt;Bubble&lt;/code&gt; itself an adapter to the third-party &lt;code&gt;FigletFont&lt;/code&gt; class from the JFiglet &lt;code&gt;.jar&lt;/code&gt; in our &lt;code&gt;lib&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Over in our &lt;code&gt;test&lt;/code&gt; folder we've got tests for &lt;code&gt;Greeter&lt;/code&gt; and &lt;code&gt;Bubble&lt;/code&gt;, which includes classes from JUnit in &lt;code&gt;lib&lt;/code&gt; as well as requiring the actual &lt;code&gt;Greeter&lt;/code&gt; and &lt;code&gt;Bubble&lt;/code&gt; classes to test. &lt;/p&gt;

&lt;p&gt;Welcome, friends, to dependencies. &lt;/p&gt;

&lt;p&gt;Java, while pretty smart, needs to know where to go sniffing for all these requirements - hence the classpath. We can get the scoop right from the &lt;a href="https://docs.oracle.com/javase/tutorial/essential/environment/paths.html"&gt;horse's mouth&lt;/a&gt;: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the &lt;code&gt;-cp&lt;/code&gt; command line switch overrides this value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Setting the &lt;code&gt;CLASSPATH&lt;/code&gt; can be tricky and should be performed with care.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which is just charming. Essentially, our classpath needs to contain the path to our &lt;code&gt;.jar&lt;/code&gt; files to the top of our package hierarchies. It can be set either via environment variable, which you shouldn't do, or with the much better option of the &lt;code&gt;-cp&lt;/code&gt; flag. &lt;/p&gt;

&lt;p&gt;It also helps explain a common Java gotcha, where you try and run &lt;code&gt;java&lt;/code&gt; without &lt;code&gt;cd&lt;/code&gt;'ing into the directory first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;java bin.space.gaston.helloworld.HelloWorld
Error: Could not find or load main class bin.space.gaston.helloworld.HelloWorld
Caused by: java.lang.NoClassDefFoundError: space/gaston/helloworld/HelloWorld &lt;span class="o"&gt;(&lt;/span&gt;wrong name: bin/space/gaston/helloworld/HelloWorld&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;bin&lt;/code&gt;, you see, isn't part of the Java package hierarchy, but does need to be on the classpath if you're outside of the folder. So both of the below would work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;bin
&lt;span class="nv"&gt;$ &lt;/span&gt;java space.gaston.helloworld.HelloWorld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;java &lt;span class="nt"&gt;-cp&lt;/span&gt; bin space.gaston.helloworld.HelloWorld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Bringing it all together
&lt;/h3&gt;

&lt;p&gt;With all that in mind, we can start to compile our files into the &lt;code&gt;bin&lt;/code&gt; directory. From the top of our project directory, we can get to work. &lt;/p&gt;

&lt;h4&gt;
  
  
  1. Compiling our &lt;code&gt;main&lt;/code&gt; folder:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;javac &lt;span class="nt"&gt;-d&lt;/span&gt; bin &lt;span class="nt"&gt;-cp&lt;/span&gt; lib/jfiglet-0.0.8.jar src/main/java/space/gaston/greeter/Greeter.java src/main/java/space/gaston/helloWorld/HelloWorld.java src/main/java/space/gaston/textEffects/Bubble.java
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We're now using the &lt;code&gt;-d&lt;/code&gt; flag to specify where our compiled files should end up. We're manually feeding each of our &lt;code&gt;.java&lt;/code&gt; files to &lt;code&gt;javac&lt;/code&gt;, so we don't need to add them to the classpath, but we do need to add our JFiglet &lt;code&gt;.jar&lt;/code&gt; file so that &lt;code&gt;Bubble&lt;/code&gt; can compile. &lt;/p&gt;

&lt;h4&gt;
  
  
  2. Compiling our &lt;code&gt;test&lt;/code&gt; folder:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;javac &lt;span class="nt"&gt;-d&lt;/span&gt; bin &lt;span class="nt"&gt;-cp&lt;/span&gt; lib/junit-platform-console-standalone-1.4.2.jar:lib/jfiglet-0.0.8.jar src/main/java/space/gaston/textEffects/Bubble.java src/test/java/space/gaston/textEffects/BubbleTest.java src/main/java/space/gaston/greeter/Greeter.java src/test/java/space/gaston/greeter/GreeterTest.java
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We need to add both the JFiglet and JUnit &lt;code&gt;.jar&lt;/code&gt; files to our classpath, and now we've also got to feed in each test file and the file its testing to the compiler. We could effectively consolidate steps 1 and 2, but it's good to break them up for demonstration purposes here as I think it helps illustrate what's going on.  &lt;/p&gt;

&lt;p&gt;Our bin file will now look like this - notice that the directory structure of our &lt;code&gt;.class&lt;/code&gt; files must maintain the same package hierarchy as the &lt;code&gt;.java&lt;/code&gt; source files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── BubbleTests.class
├── GreeterTests.class
└── space
    └── gaston
        ├── greeter
        │   └── Greeter.class
        ├── helloworld
        │   └── HelloWorld.class
        └── textEffects
            └── Bubble.class
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Running our tests:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ java -jar lib/junit-platform-console-standalone-1.4.2.jar -cp bin:lib/jfiglet-0.0.8.jar --scan-class-path
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;╷
├─ JUnit Jupiter ✔
│  ├─ BubbleTests ✔
│  │  └─ helloReturnsAsciiHello&lt;span class="o"&gt;()&lt;/span&gt; ✔
│  └─ GreeterTests ✔
│     ├─ greetWithArgumentSteveReturnsHelloSteve&lt;span class="o"&gt;()&lt;/span&gt; ✔
│     └─ greetWithNoArgsReturnsHelloWorld&lt;span class="o"&gt;()&lt;/span&gt; ✔
└─ JUnit Vintage ✔
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our version of JUnit can be run on the command line. We're passing in the &lt;code&gt;--scan-class-path&lt;/code&gt; flag to automatically have JUnit look for all tests on our classpath, so this requires adding the &lt;code&gt;bin&lt;/code&gt; folder to the classpath (because we're at the top of our project folder) as well as JFiglet, which is still required by &lt;code&gt;Bubble&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Also, yay, the tests pass. &lt;/p&gt;

&lt;h4&gt;
  
  
  4. Running our main app:
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ java -cp bin:lib/jfiglet-0.0.8.jar space.gaston.helloworld.HelloWorld
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  _   _          _   _             ____    _
 | | | |   ___  | | | |   ___     / ___|  | |_    ___  __   __   ___
 | |_| |  / _ \ | | | |  / _ \    \___ \  | __|  / _ \ \ \ / /  / _ \
 |  _  | |  __/ | | | | | (_) |    ___) | | |_  |  __/  \ V /  |  __/
 |_| |_|  \___| |_| |_|  \___/    |____/   \__|  \___|   \_/    \___|
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;No, I have no idea either why we tasked it to output 'Hello Steve'. &lt;/p&gt;

&lt;p&gt;So, great. We've got that working. But, gosh, wasn't that a bit much for even a simple, totally contrived application? Can you imagine that literally every time you make a change to the app and need to recompile? I don't know about you, but if I had to work like that for over a week I'd be permanently stuck looking like I was cosplaying Edvard Munch's The Scream. &lt;/p&gt;

&lt;p&gt;In the next post, the cavalry will march in to bail us out of a lifetime of perpetual build horror. &lt;/p&gt;




&lt;p&gt;Has this post been useful for you? I'd really appreciate any comments and feedback on whether there's anything that could be made clearer or explained better. I'd also massively appreciate any corrections! &lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Draw the Rest of the F**king Owl: Approaching Software Design as a Beginner</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Fri, 19 Jul 2019 15:31:29 +0000</pubDate>
      <link>https://forem.com/martingaston/draw-the-rest-of-the-f-king-owl-approaching-software-design-as-a-beginner-35c9</link>
      <guid>https://forem.com/martingaston/draw-the-rest-of-the-f-king-owl-approaching-software-design-as-a-beginner-35c9</guid>
      <description>&lt;p&gt;I've always found the discussions around software design some of the most intimidating aspects of programming. With the best of intentions, I still flinch a little when I hear the phrase &lt;em&gt;design pattern&lt;/em&gt;. I think one of the reasons for all the fervent dogma around this aspect of software design is how one size doesn't fit all, and even with all these so-called best practices at the end of the day it's a human problem. Albeit one we try to act like we can definitely solve with methodologies. The way you structure a project in your mind, while potentially being enormously successful in your own practice, might not be anywhere near the most productive way for somebody else. &lt;/p&gt;

&lt;p&gt;Yay for individualism! But this can be anathema in an office, where projects are precisely timed, judged and turned into lots of whizzy graphs to show off at various and endless important meetings. We also want our efforts to be reproducible, and the overall processes to be largely understood so we can effectively estimate against it. Stop crushing my free spirit, The Man! Boo!&lt;/p&gt;

&lt;p&gt;But wait, it's not all doom and one-day agile development workshop coaches. We do need a common shared context of information for communicating with other developers. One of the trickiest parts of getting started in any specialised field, in my opinion, is growing the knowledge and confidence to articulate your thoughts. When I started with pair programming at a coding bootcamp, I found frustration easily crept in between the gaps of knowledge and communication: many of us knew &lt;em&gt;where&lt;/em&gt; to start solving a problem, but found it extremely difficult with &lt;em&gt;how&lt;/em&gt; to discuss it. I still regularly find myself resorting to muttering about 'that thingy' or doing 'some stuff' with the 'whatsit'.&lt;/p&gt;

&lt;p&gt;Developing this internal monologue - in my mind, reader, I am Jane Austen and my code is a novel - is as useful for your own personal code development as it is for working in a team. For instance, I recently realised my first few steps in design as a software developer could be neatly summed up by this meme: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fraowr6zqaxsy53j8a72w.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fraowr6zqaxsy53j8a72w.jpg"&gt;&lt;/a&gt;&lt;br&gt;
I knew where I was (two circles) and I knew where I wanted to end up (the owl), but I'd be royally danged if I could think of literally &lt;em&gt;anything&lt;/em&gt; that could help get me to move between steps 1 and 2. So I'd just sit there, trapped in my own analysis paralysis until someone else came along and helped push me along the path. I was often mentally blocked in a world where everything was either a &lt;em&gt;thingy&lt;/em&gt;, a &lt;em&gt;whatsit&lt;/em&gt; or a finished owl. &lt;/p&gt;

&lt;p&gt;TDD - wait! come back! I'm not going to try and sell you an eBook or offer any agile coaching services! - is often prescribed as a tidy cure to this ailment. As a fan of the classics, I've recently started reading an endearingly dog-eared copy of Kent Beck's &lt;em&gt;Test Driven Development by Example&lt;/em&gt; and it's done a really good job of helping me actually break through this barrier from time to time.&lt;/p&gt;

&lt;p&gt;There's plenty of sage advice within the book (I'd say it's definitely worth picking up a copy), but the key benefit I've been able to extract from it, with the way my my works, is in making my feedback loops shorter - reaching for the &lt;em&gt;little&lt;/em&gt; change and starting to build the mountain of my application from many, many molehills.&lt;/p&gt;

&lt;p&gt;Beck frequently reminds us not to jump ahead too far - I think the book helps demonstrate a framework for &lt;em&gt;thinking&lt;/em&gt; more than anything else, and frequent reminders to maintain an active todo list is a useful guides for practising pacing and scope. He also reminds us there's no point in being too dogmatic about everything: if there's an obvious implementation, then just write it. Keeping a todo list also encourages me to doodle more, which occasionally helps me figure out my next steps. And that's super cool!&lt;/p&gt;

&lt;p&gt;Here's the exact moment I realised, on a slightly grim-coloured, off-brand Post-it® note, how HTTP server responses are kind of numbered in the order you might process the request, which blew my mind:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxau2opnx12lq4nl0ueyl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxau2opnx12lq4nl0ueyl.png" alt="I know, I misspelt Flowtatoes"&gt;&lt;/a&gt;&lt;br&gt;
Once we start with a failing test, and then make it run, Beck introduces three strategies to start progressing towards making our code work the way we want it to: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Fake it&lt;/strong&gt;: Return a constant and gradually replace constants with variables until you have a fully-baked implementation.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Use Obvious Implementation&lt;/strong&gt;: Just type in the real implementation.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Triangulation&lt;/strong&gt;: The most conservative strategy, where we only start to generalise our code when there are two or more examples. This helps to eke towards the right path if you're getting lost on your design, which I frequently am. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Right now, I like to consciously think about which one of these strategies I'm employing and really actively think about it. &lt;/p&gt;

&lt;p&gt;Something else I really liked about the book is that it helped me think more about the rhythm of development - speeding up and slowing down as appropriate. I've always been accidentally partial to a bit of &lt;a href="https://youtu.be/m11VH-dUPEk" rel="noopener noreferrer"&gt;speed archaeology&lt;/a&gt;, which helps me blaze ahead in the first few hours but quickly creates more problems than it solves. &lt;/p&gt;

&lt;p&gt;The other thing I like about working like this - short, intentional steps - is how it's an incremental approach. Trying to memorise two dozen complicated design patterns and the exact right time of when to use them is both exhausting and overly intimidating. So most of the time I try and guide myself with a far simpler set of strategies, and then I can reach for a design pattern when I get a sense that my code is missing a certain... something. &lt;/p&gt;

&lt;p&gt;Finally, one of the biggest challenges about your approach to design is that it doesn't sit still. These things ebb and flow regardless of matter how many Gang of Four patterns you've had tattooed across your back. When I started developing in React I couldn't even get half-way through Googling my problem before someone was telling me about a presentational or container component, and now Dan Abramov &lt;a href="https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0" rel="noopener noreferrer"&gt;has updated his blog post on the pattern&lt;/a&gt; to include the following caveat: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I wrote this article a long time ago and my views have since evolved. In particular, I don’t suggest splitting your components like this anymore. If you find it natural in your codebase, this pattern can be handy. But I’ve seen it enforced without any necessity and with almost dogmatic fervor far too many times. The main reason I found it useful was because it let me separate complex stateful logic from other aspects of the component. Hooks let me do the same thing without an arbitrary division. This text is left intact for historical reasons but don’t take it too seriously.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Partially related: &lt;a href="https://overreacted.io/" rel="noopener noreferrer"&gt;reading Dan Abramov's blog&lt;/a&gt; is a wonderfully humbling experience which brings frequent joy into my life.&lt;/p&gt;

&lt;p&gt;Trying to separate out my sense of intimidation from design with some real-life approaches is what worked for me, but the frustrating beauty of it all is that it might not work for anyone else. If it feels painfully new to you, like it does with me, I'd recommend just trying a bunch of stuff out. When you start to find those methods which resonate with you, eventually you realise &lt;em&gt;you're actually drawing the owl&lt;/em&gt;. &lt;/p&gt;




&lt;p&gt;Has this post been useful for you? I'd really appreciate any comments and feedback on whether there's anything that could be made clearer or explained better.&lt;/p&gt;

</description>
      <category>design</category>
      <category>beginners</category>
      <category>career</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Just My Type: Rolling a fresh TypeScript project with Jest, ESLint and Prettier in 2019</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Sat, 06 Jul 2019 18:19:03 +0000</pubDate>
      <link>https://forem.com/martingaston/just-my-type-rolling-a-fresh-typescript-project-with-jest-eslint-and-prettier-in-2019-2cfk</link>
      <guid>https://forem.com/martingaston/just-my-type-rolling-a-fresh-typescript-project-with-jest-eslint-and-prettier-in-2019-2cfk</guid>
      <description>&lt;p&gt;TypeScript's motto is that it's &lt;em&gt;JavaScript that scales&lt;/em&gt;, and one area where that's definitely true is in the extra work you have to do in order to spin up a project. Ba-dum! The time investment isn't so bad when you're setting up something you'll be working across for months on end, but when you're looking to just spin up a repo to learn, experiment or solve a few katas it can be a bit of a fiddle to get everything up and running over and over and over again. I just want that beautiful static typing 😭&lt;/p&gt;

&lt;p&gt;If you'd like to just skip all the setup and get stuck in, you &lt;a href="https://github.com/martingaston/typescript-skeleton"&gt;can find this starter template on GitHub&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Install Dependencies
&lt;/h3&gt;

&lt;p&gt;Get ready to install this hearty wedge of dependencies. I'm using &lt;code&gt;npm&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ npm init -y &amp;amp;&amp;amp; npm i -D jest typescript ts-jest @types/jest eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin prettier eslint-plugin-prettier eslint-config-prettier&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/"&gt;TypeScript&lt;/a&gt;'s own getting started requests a global installation, but I like to avoid these whenever possible. We'll throw in a script to get access to the TypeScript compiler, &lt;code&gt;tsc&lt;/code&gt;, elsewhere in our config. &lt;/p&gt;

&lt;h3&gt;
  
  
  Add Configuration
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. &lt;code&gt;package.json&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Outside of the standard &lt;code&gt;npm init -y&lt;/code&gt; template, nip into your &lt;code&gt;package.json&lt;/code&gt; and add a couple of updates to your &lt;code&gt;scripts&lt;/code&gt; to easily get access to our test runner and the TypeScript compiler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"jest"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"tsc"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"tsc"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  2. &lt;code&gt;jest.config.js&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;We're using &lt;code&gt;ts-jest&lt;/code&gt; with our project so we can &lt;a href="https://kulshekhar.github.io/ts-jest/user/babel7-or-ts"&gt;feast on TypeScript's type checking&lt;/a&gt; directly from Jest. A quick little &lt;code&gt;$ npx ts-jest config:init&lt;/code&gt; will produce the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;preset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ts-jest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;testEnvironment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  3. &lt;code&gt;tsconfig.json&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;By default, the TypeScript compiler can produce a hefty configuration file - it's got really informative comments so it helps the output not get too overwhelming. You can build one by running &lt;code&gt;$ npm run tsc -- --init&lt;/code&gt;, and the options below are how I like to have mine setup right now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"compilerOptions"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"target"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ES2015"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;                       
    &lt;/span&gt;&lt;span class="nl"&gt;"module"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"commonjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;                     
    &lt;/span&gt;&lt;span class="nl"&gt;"outDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;                        
    &lt;/span&gt;&lt;span class="nl"&gt;"rootDir"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"strict"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"esModuleInterop"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"exclude"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"node_modules"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="s2"&gt;"test"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  4. &lt;code&gt;.eslintrc.js&lt;/code&gt; and &lt;code&gt;.pretterrc&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;As of the start of this year, the TypeScript team formally adopted ESLint as the linter de rigueur of the TypeScript space.&lt;/p&gt;

&lt;p&gt;If you're interested, the &lt;code&gt;typescript-eslint&lt;/code&gt; team talk about the inherent challenge of parsing TypeScript and ESLint together &lt;a href="https://github.com/typescript-eslint/typescript-eslint#why-does-this-project-exist"&gt;in their repo&lt;/a&gt;. It's a pretty fascinating topic! &lt;/p&gt;

&lt;p&gt;Wedging ESLint together with Prettier - my favourite code formatter - and TypeScript is probably the most involved step. The brief overview is that we configure ESLint to disregard the rules that Prettier is concerned with, then roll Prettier in with our linter so they can run together as a single step.&lt;/p&gt;

&lt;p&gt;We also need to point our ESLint config to our &lt;code&gt;tsconfig.json&lt;/code&gt; file and set up a few other options, such as the &lt;code&gt;sourceType&lt;/code&gt; and &lt;code&gt;ecmaVersion&lt;/code&gt; settings.&lt;/p&gt;

&lt;p&gt;Here's how that looks:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.eslintrc.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@typescript-eslint/parser&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@typescript-eslint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;extends&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;plugin:@typescript-eslint/recommended&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;plugin:prettier/recommended&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prettier/@typescript-eslint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;parserOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tsconfig.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;ecmaVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2018&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;sourceType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Special ESLint rules or overrides go here.&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;Over in &lt;code&gt;.prettierrc&lt;/code&gt; I just like to pop in my preferred options: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;.prettierrc&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;trailingComma&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;es5"&lt;/span&gt;
&lt;span class="na"&gt;tabWidth&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;
&lt;span class="na"&gt;semi&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;span class="na"&gt;singleQuote&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now we've got the one-two combo of ESLint and Prettier helping sure our code looks swell and operates properly. We're ready to actually write some code! &lt;/p&gt;

&lt;h3&gt;
  
  
  Hello World!
&lt;/h3&gt;

&lt;p&gt;I like to separate out code in a &lt;code&gt;src&lt;/code&gt; directory, with tests in &lt;code&gt;test&lt;/code&gt;. So a quick &lt;code&gt;$ mkdir src test&lt;/code&gt; will set that up, and our previous steps will have TypeScript transpile everything in our &lt;code&gt;src&lt;/code&gt; directory to a JavaScript &lt;code&gt;build&lt;/code&gt; directory when we &lt;code&gt;npm run tsc&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But to really make check everything is working, it's time for a quintessential rite of passage: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;test/sayHello.test.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../src/sayHello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sayHello can greet a user&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;TypeScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello TypeScript!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then to make that pass (and to check it's all working) we can throw in some of our fancy TypeScript features: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/sayHello.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;sayHello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$ npm test&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt; PASS  &lt;span class="nb"&gt;test&lt;/span&gt;/sayHello.test.ts
  ✓ sayHello can greet a user &lt;span class="o"&gt;(&lt;/span&gt;4ms&lt;span class="o"&gt;)&lt;/span&gt;

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.206s
Ran all &lt;span class="nb"&gt;test &lt;/span&gt;suites.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Fantastic! We've created a fine little environment for playing with TypeScript. &lt;/p&gt;

&lt;p&gt;And, just to make sure we haven't set up any conflicting rules in our ESLint config, we can run a quick check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./node_modules/.bin/eslint &lt;span class="nt"&gt;--print-config&lt;/span&gt; src/sayHello.ts | ./node_modules/.bin/eslint-config-prettier-check
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; No rules that are unnecessary or conflict with Prettier were found.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;TypeScript is a really fun language to work in, but getting into a place where you can run the config quickly makes it far more pleasurable to get started. Feel free to share your own tips and tricks of configuring your TypeScript environment - I'm still learning the language myself, so would love to know how other people like to setup their repos! &lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus 1: Add a Git Hook
&lt;/h3&gt;

&lt;p&gt;One really neat part about the JavaScript ecosystem is that husky and lint-staged make it incredibly straightforward to run our linters and test suites as we're committing directly to git, wrapping us in another comfy blanket of consistent, tested code. While it's possible to get these up and running without introducing another pair of dependencies in our project, I think the time saved is absolutely worth it. &lt;/p&gt;

&lt;p&gt;If you run &lt;code&gt;$ npm i -D lint-staged husky&lt;/code&gt; these features are just a quick addition to our &lt;code&gt;package.json&lt;/code&gt; away. Smoosh the below options somewhere within the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"husky"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"pre-commit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"lint-staged"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"lint-staged"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"*.{ts,js,tsx,jsx}"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"eslint --fix"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"jest --coverage --findRelatedTests"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"git add"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
 &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, when we &lt;code&gt;git commit&lt;/code&gt; our files we'll automatically run our linter and any tests that directly relate to what's in our staging area. &lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus 2: VS Code Settings
&lt;/h3&gt;

&lt;p&gt;I find people who use VS Code generally have excellent taste, but I tend to flick between it and Vim depending on whether I'm settling down for a while or just nipping in to a file to make a couple of tweaks. VS Code is prepared to do a lot of our heavy lifting, but as we've already configured so much elsewhere it's worth letting VS Code know it can ease off a bit.&lt;/p&gt;

&lt;p&gt;First, grab the &lt;a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint"&gt;ES Lint extension for VS Code&lt;/a&gt;. Then add the below to your &lt;code&gt;settings.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"eslint.autoFixOnSave"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"eslint.validate"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"javascript"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"autoFix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"typescript"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"autoFix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"javascriptreact"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"autoFix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"language"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"typescriptreact"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"autoFix"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"eslint.alwaysShowStatus"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"editor.formatOnSave"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"[typescript], [javascript]"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"editor.formatOnSave"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This tells both VS Code to take a break and ESLint to stop lounging around and do some work when it comes to JavaScript and TypeScript, and now our linting config should dutifully kick in and autoformat whenever we save. Neat!&lt;/p&gt;




&lt;p&gt;Has this post been useful for you? I'd really appreciate any comments and feedback on whether there's anything that could be made clearer or explained better. I'd also massively appreciate any corrections!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The Life-Changing Magic of Refactoring</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Fri, 05 Jul 2019 22:12:51 +0000</pubDate>
      <link>https://forem.com/martingaston/the-life-changing-magic-of-refactoring-4mk1</link>
      <guid>https://forem.com/martingaston/the-life-changing-magic-of-refactoring-4mk1</guid>
      <description>&lt;p&gt;There's a drawer underneath my desk I use to store all the little bits and bobs I randomly accrue, rarely use but don't really feel like getting rid of. But the contents of this drawer also have a naughty habit of escaping this space, then promptly spreading around my desk, office and eventually the rest of the flat.&lt;/p&gt;

&lt;p&gt;In a perfect universe I'd find a way to efficiently store all this junk. Still, there's no need to be too dogmatic about it. This is the real world. Sometimes you've just got to cram half the contents of your living room into a cupboard because you've got guests coming over. &lt;/p&gt;

&lt;p&gt;In our coding lives, this is called refactoring: applying small changes to our code in order to spruce up our design without changing the core functionality. Still, refactoring? I find the word a little cold, almost mathematical - it gives me feelings of a scholarly, scientific process. I think it sounds precise and clinical and, well, like it's absolutely no fun whatsoever. It feels like &lt;em&gt;work&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;There are fantastic reasons why many smart people have helped create a common language around code smells and refactoring patterns, of course, and it's not just so that you can try to look smart when you write blog posts. Having shared conventions means you can discuss them in teams, and a common rubric for communicating these ebbs and flows of moving code around creates the comforting dependability of a context. I am incredibly glad the discourse around software design exists, that the software engineers of today can stand on the shoulders of giants and I dream of being able to make even a fraction of the positive impact of these luminaries. &lt;/p&gt;

&lt;p&gt;Sometimes, though, all these raw smarts can be a bit of a drag. With so many formal and complicated-sounding names, even approaching a discussion about a refactoring pattern can be intimidating for new programmers. Then they're often explained with some too-good-to-be-true examples that are hard to contextualise within your current work, which has always left me reeling - &lt;em&gt;how do I apply that beautiful pattern to my actual code?&lt;/em&gt; I also think it's really hard to see why you should use a refactoring pattern until you've been stung by it. I'm intimated into sheer indecision. &lt;/p&gt;

&lt;p&gt;But, really, refactoring is just doing a bit of tidying up. It's deciding to do the dishes before you're down to your last big plate, or making sure you've loaded the dishwasher before bed so you don't have to deal with it in the morning. &lt;/p&gt;

&lt;p&gt;I'm terrible at examples, so let me pluck out quite literally what I'm working on as I write this. I was rushing to implement a feature to pass a test (if Java can find an "app.port' System Property then a server will bind to that port number) and threw in the below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Connection&lt;/span&gt; &lt;span class="n"&gt;connection&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;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"app.port"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;connection&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;Connection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&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;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"app.port"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; 
        &lt;span class="n"&gt;connection&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;Connection&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;  
    &lt;span class="nc"&gt;Server&lt;/span&gt; &lt;span class="n"&gt;app&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;Server&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&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 tests pass. Awesome. I won't pretend that 75% of me wasn't super ready to just &lt;code&gt;git commit&lt;/code&gt; and go for lunch. But no! There was time for a spot of cleaning. &lt;/p&gt;

&lt;p&gt;So, first - I don't like doing anything in &lt;code&gt;main&lt;/code&gt; other than setting up classes, so I felt like extracting most of the &lt;code&gt;if&lt;/code&gt; statement would make the code easier to read for Future Me.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;makeConnection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;app&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;Server&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;connection&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;start&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;Sure, that's just moving code around. But chunking our code down into smaller functions usually has the side-effect of making  our work easier to test, maintain and extend. Now, my next step was to make all that boolean logic a little more expressive and more communicative of our intent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;makeConnection&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&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;hasSpecifiedPort&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;getPort&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;Connection&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;port&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;Connection&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getPort&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="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;parseInt&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;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"app.port"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;  

&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;portIsSpecified&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="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isNull&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;getProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"app.port"&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;We've just extracted something in our extraction! Wow. Achingly meta. But now &lt;code&gt;makeConnection&lt;/code&gt; doesn't really have to worry about what Java System Property we're fishing for, or the specifics on obtaining it. It simply... gets on with it. &lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;if&lt;/code&gt; statement has also been tweaked to just bail early if it matches. That's a little refactoring I really enjoy because it gets us out of our braces, which - as someone mid-way through Invisalign right now - is a good thing indeed. &lt;/p&gt;

&lt;p&gt;That's only a couple of simple steps, and if I'm being honest IntelliJ did most of the work there. But after doing just a couple of little steps, I was ready to &lt;code&gt;git commit&lt;/code&gt;. Then I had a sandwich. It was delicious, thank you. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Wait"&lt;/em&gt;, you might be thinking. &lt;em&gt;"Don't you have any more extravagant examples of, say, refactoring an Elixir module using Structs and Protocols to achieve polymorphism?"&lt;/em&gt; you plead, teary-eyed, coincidentally thinking about the immediate last project I was working on. &lt;em&gt;"Please,"&lt;/em&gt; you beg, &lt;em&gt;"aren't there some more complicated patterns you could apply?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And, well, sure. There's plenty of example of those, most of which are birthed from the tome of &lt;a href="https://refactoring.com/"&gt;Refactoring&lt;/a&gt;. Yet I argue that, sometimes, that's not the point. Especially when deadlines, project managers and the need for lunch breaks come into play. We should celebrate each of our little wins, not worry too much about how a few dozen of those wins makes for a fascinating example of before and after. &lt;/p&gt;

&lt;p&gt;I'd say it's always worth doing the little nips and tucks we can achieve &lt;em&gt;today&lt;/em&gt; rather than trying to train ourselves to do the bigger, meatier, more interesting refactorings we promise we'll do &lt;em&gt;tomorrow&lt;/em&gt; - a tomorrow that, if we're being honest, might never happen. &lt;/p&gt;

&lt;p&gt;Refactoring is a broad church, but I think it's worth remembering how it always comes down to just taking that one item off that chair in your house you use for piling stuff onto; the one that you tell yourself is just being used to hold that pile of books temporarily. &lt;/p&gt;

&lt;p&gt;The little changes? That's how we spark joy. &lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>career</category>
      <category>testing</category>
    </item>
    <item>
      <title>Getting Started With a Java Environment on macOS</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Fri, 28 Jun 2019 16:32:33 +0000</pubDate>
      <link>https://forem.com/martingaston/getting-started-with-a-java-environment-on-macos-541c</link>
      <guid>https://forem.com/martingaston/getting-started-with-a-java-environment-on-macos-541c</guid>
      <description>&lt;p&gt;Alright, real talk: I finished my first Java project as a like, &lt;em&gt;totally&lt;/em&gt; professhional developer without having much of a clue about, well, anything to do with the Java build/tooling ecosystem. I pressed enough buttons in IntelliJ for things to run, and (thankfully) they ran, and I spent the rest of the project perpetually swaddled within IntelliJ's gentle embrace. &lt;/p&gt;

&lt;p&gt;Winging it? Nah, I call it &lt;em&gt;falling with style&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Basically, I found comprehending Java tooling seriously perplexing. So before kicking off my second Java project I wanted to forge a better understanding of just what the heck was going on.&lt;/p&gt;

&lt;p&gt;Over the course of a few posts we're going to learn a little more about how Java is installed on our local machine before building a straightforward (and only somewhat contrived) &lt;code&gt;Hello World&lt;/code&gt; application, making use of external libraries and multiple files so we can make some sense of a standard Java directory structure and how to build our application from the command line. Then we'll move on to incorporating the same app with help from Gradle, a popular build tool, and we'll finish by incorporating our project into the IntelliJ IDE. At the end of this series we should all have a much more comfortable knowledge of what goes on under the hood of all our tooling, and way more confidence to tinker with our project builds. &lt;/p&gt;

&lt;p&gt;First steps: the absolute basics. Time for a forensic journey. The first time I quickly fudged my way through installing OpenJDK 12 and I can't even tell you how I did it. Downloading &lt;a href="https://openjdk.java.net/"&gt;OpenJDK&lt;/a&gt;, as far as I can tell, gives you a directory of unloved binaries which virtually zero guidance on what you're supposed to do with them. I felt like &lt;del&gt;Ted Danson&lt;/del&gt; Steve Guttenberg in &lt;em&gt;Three Men and a Baby&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;**tl;dr:&lt;/em&gt;* copy the uncompressed OpenJDK directory - e.g. &lt;code&gt;jdk-12.jdk&lt;/code&gt; - into &lt;code&gt;/Library/Java/JavaVirtualMachines/&lt;/code&gt; and you'll be good.* &lt;/p&gt;

&lt;p&gt;But whyyyy? Let's unmangle that. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Caveat&lt;/em&gt;: this is specific to macOS right now. Linux and Windows will inevitably be different, and likely have a swathe of gremlins of their very own. &lt;/p&gt;

&lt;h3&gt;
  
  
  Where is Java actually installed?
&lt;/h3&gt;

&lt;p&gt;Strap in, because descending through an installation of Java is like staring Cthulhu in the face for a fortnight. Descending into its heart of darkness is a guarantee that you're going to end up as Kurtz. You've been warned. &lt;/p&gt;

&lt;p&gt;Let's start at the top:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;java &lt;span class="nt"&gt;--version&lt;/span&gt;
openjdk 12 2019-03-19
OpenJDK Runtime Environment &lt;span class="o"&gt;(&lt;/span&gt;build 12+33&lt;span class="o"&gt;)&lt;/span&gt;
OpenJDK 64-Bit Server VM &lt;span class="o"&gt;(&lt;/span&gt;build 12+33, mixed mode, sharing&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then we can peek inside our &lt;code&gt;java.home&lt;/code&gt; &lt;a href="https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html"&gt;system property&lt;/a&gt; from within &lt;code&gt;jshell&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;jshell&amp;gt; System.getProperty&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"java.home"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Alright, cool. We've got Java installed. It's the latest version, and it's blissfully not the Oracle JDK. Things are looking rosy. So presumably our shell will point to our &lt;code&gt;java.home&lt;/code&gt; directory?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;which java
/usr/bin/java
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;OK. But that file is 74 bytes, which if you ask me a tell-tale sign a symbolic link. This ain't our first rodeo!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;greadlink &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;which java&lt;span class="si"&gt;)&lt;/span&gt;
/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Well, that's new. And if we have a peep within the &lt;code&gt;JavaVM.framework&lt;/code&gt; directory we can see all our familiar Java files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-lh&lt;/span&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/ | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"java"&lt;/span&gt;
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;  1 root  wheel    57K 21 Mar 06:07 java
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;  1 root  wheel    74K 21 Mar 06:07 java_home
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;  1 root  wheel    57K 21 Mar 06:07 javac
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;  1 root  wheel    57K 21 Mar 06:07 javadoc
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;  1 root  wheel    57K 21 Mar 06:07 javah
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;  1 root  wheel    57K 21 Mar 06:07 javap
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;  1 root  wheel    57K 21 Mar 06:07 javapackager
&lt;span class="nt"&gt;-rwxr-xr-x&lt;/span&gt;  1 root  wheel    56K 21 Mar 06:07 javaws
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These aren't symbolic links, so that's good. Things still don't seem to be lining up, mind. But a little more digging on &lt;a href="https://stackoverflow.com/a/15133344"&gt;Stack Overflow suggests these are stub applications&lt;/a&gt; which determine the version of the JVM to use. Have you ever seen anything like that before? Because I haven't. &lt;/p&gt;

&lt;p&gt;Some more internet trawling reveals &lt;code&gt;java_home&lt;/code&gt;, which according to its &lt;code&gt;man&lt;/code&gt; page: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;returns a path suitable for setting the JAVA_HOME environment variable.  It determines this path from the user's enabled and preferred JVMs in the Java Preferences application.  Additional constraints may be provided to filter the list of JVMs available.  By default,  if  no  constraints match the available list of JVMs, the default order is used.  The path is printed to standard output.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Only I'm fairly sure Apple removed the Java Preferences application yonks ago, but no matter. Did they just sort of half-remove the Java support from the OS, keeping us lightly tethered to an unloved, unmaintained ghost in the machine? That's an open question: I have no idea. I only know we wouldn't be Java developers if we cared for such consistencies. Let's just boot it up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;/usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Alright! Fantastic. We finally got to &lt;code&gt;/Library/Java/JavaVirtualMachines&lt;/code&gt;. I've only lost part of my sanity, so I'll take this as a win. &lt;code&gt;java_home&lt;/code&gt; seems to prioritise more recent versions of the JDK by default, so if you've got 12 installed it will supersede 11 when you run &lt;code&gt;java&lt;/code&gt;, &lt;code&gt;javac&lt;/code&gt;, &lt;code&gt;jshell&lt;/code&gt; and the like. You can look at all your currently installed versions with the &lt;code&gt;-V&lt;/code&gt; (capitalised) flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;/usr/libexec/java_home &lt;span class="nt"&gt;-V&lt;/span&gt;
Matching Java Virtual Machines &lt;span class="o"&gt;(&lt;/span&gt;2&lt;span class="o"&gt;)&lt;/span&gt;:
    12, x86_64: &lt;span class="s2"&gt;"OpenJDK 12"&lt;/span&gt;    /Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
    11, x86_64: &lt;span class="s2"&gt;"OpenJDK 11"&lt;/span&gt;    /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Meanwhile, the &lt;code&gt;-v&lt;/code&gt; (lower case) flag will return the path to said JVM, which can then be set as the &lt;code&gt;JAVA_HOME&lt;/code&gt; environment variable. &lt;code&gt;JAVA_HOME&lt;/code&gt;, when set, takes precedence over the default ordering of &lt;code&gt;/usr/libexec/java_home&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;JAVA_HOME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;/usr/libexec/java_home &lt;span class="nt"&gt;-v&lt;/span&gt; 11&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;java &lt;span class="nt"&gt;--version&lt;/span&gt;
openjdk 11 2018-09-25
OpenJDK Runtime Environment 18.9 &lt;span class="o"&gt;(&lt;/span&gt;build 11+28&lt;span class="o"&gt;)&lt;/span&gt;
OpenJDK 64-Bit Server VM 18.9 &lt;span class="o"&gt;(&lt;/span&gt;build 11+28, mixed mode&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Want to relive the halcyon days of JDK 1.6? Go right ahead. Have a blast. No, I'm OK - I'll stick around here, cheers. Of course, running the &lt;code&gt;export&lt;/code&gt; command in your shell will only last the lifetime of that shell instance. For a more permanent solution you'll need to nip into your &lt;code&gt;.bashrc&lt;/code&gt;/&lt;code&gt;.zshrc&lt;/code&gt; or equivalent. &lt;/p&gt;

&lt;p&gt;I'm assuming that the default choices of &lt;code&gt;/usr/libexec/java_home&lt;/code&gt; mirror what happens when you run one of the stub applications in &lt;code&gt;/System/Library/Frameworks/JavaVM.framework/&lt;/code&gt;. Or, you might have spotted that there was also a &lt;code&gt;java_home&lt;/code&gt; binary in the &lt;code&gt;JavaVM.framework&lt;/code&gt; directory earlier. Wait a second...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;greadlink &lt;span class="nt"&gt;-f&lt;/span&gt; /usr/libexec/java_home
/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java_home
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Look, I &lt;em&gt;did&lt;/em&gt; warn you that it would be messy. But at least now we know a lot more about how Java is running on our system!&lt;/p&gt;




&lt;p&gt;In the next post we'll start looking at the challenges of compiling and running our first Java application. I'll update this post with a link when it's live! &lt;/p&gt;

&lt;p&gt;Has this post been useful for you? I'd really appreciate any comments and feedback on whether there's anything that could be made clearer or explained better. I'd also massively appreciate any corrections! &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
    </item>
    <item>
      <title>Understanding Java Objects, Abstract and Concrete</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Fri, 17 May 2019 13:46:02 +0000</pubDate>
      <link>https://forem.com/martingaston/understanding-java-objects-abstract-and-concrete-488a</link>
      <guid>https://forem.com/martingaston/understanding-java-objects-abstract-and-concrete-488a</guid>
      <description>&lt;p&gt;I've been working a lot in Java lately, but only when my project required me to adopt some of the famous object-oriented design patterns did I realise how totally unsure I was of what was going on at a fundamental level, so I found it a real challenge to keep up with all the terminology. I felt like I had hit a brick wall.&lt;/p&gt;

&lt;p&gt;I decided to &lt;a href="https://docs.oracle.com/javase/tutorial/java/TOC.html"&gt;invest some time&lt;/a&gt; in unpacking some of those building blocks - so let's blow the doors off some of this Java jargon. I'm hoping that if you've ever felt in a similar position that these notes might be able to help further your own understanding of Java's sprawl.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java Jargon #1: Creating Objects
&lt;/h3&gt;

&lt;p&gt;In Java, almost everything is an object - such as &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;ArrayList&lt;/code&gt; and &lt;code&gt;HashMap&lt;/code&gt;. Objects are created from classes. Think of your class code as an instruction manual for Java to produce an object.&lt;/p&gt;

&lt;p&gt;Creating an object requires creating an instance of a class - so the phrase &lt;em&gt;instantiating a class&lt;/em&gt; is often used by people who want to sound especially smart. But you can use both terms interchangeably, depending on who you're talking to or if you're trying to win a game of Scrabble.&lt;/p&gt;

&lt;p&gt;To formally create an object/instantiate a class, which is to say what we have to do in order to stop the compiler from going full on Mr Resetti at us, we follow a three-step process: declaration, instantiation and initialisation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Declaration&lt;/strong&gt; is achieved by specifying the &lt;code&gt;type&lt;/code&gt; and the &lt;code&gt;name&lt;/code&gt; of  a variable, so we could tell Java &lt;code&gt;Dragon viserion;&lt;/code&gt;, which would get everything prepped and primed to accept a &lt;code&gt;Dragon&lt;/code&gt; type. But at this point our object is currently closer to my checking account than a fearsome dragon: it's empty. Nothing in it. Zip, nada, nill or, specifically, &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Always watch out for &lt;code&gt;null&lt;/code&gt;. If I had kids, I wouldn't like to see them hanging around with &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For the record: Java also has both &lt;em&gt;primitive&lt;/em&gt; and &lt;em&gt;reference&lt;/em&gt; variables, which we can wave our hands at now but can potentially create some additional bother every now and then, generally when it comes to assignment and comparison. But, hey, we're programming in Java so it's probably safe to assume we're all kind of into a spot of bother every now and then.&lt;/p&gt;

&lt;p&gt;Anyway, we're ready for steps 2 and 3 - instantiation and initialisation, which like to rock up as a pair:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="n"&gt;viserion&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;Dragon&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; operator &lt;strong&gt;instantiates&lt;/strong&gt; the class, allocating the required memory behind the scenes and then returning a reference (the address) of that memory so our &lt;code&gt;viserion&lt;/code&gt; variable knows where to send anyone who comes knocking. Calling &lt;code&gt;new&lt;/code&gt; also requires a call to a constructor, which is where the parenthesis enter the scene. The &lt;code&gt;()&lt;/code&gt; part handles &lt;strong&gt;initialisation&lt;/strong&gt;, which goes sniffing out a class constructor that matches the signature and finally gets our object fully setup and ready to come out.&lt;/p&gt;

&lt;p&gt;Constructors in a Java class have the same name as the class and no return type. If we take a look at our &lt;code&gt;Dragon&lt;/code&gt; class we can see that it has two constructor signatures: one which can respond to a zero-argument constructor, which will in turn call our two-argument constructor that requires a &lt;code&gt;Stomach&lt;/code&gt; and &lt;code&gt;Mouth&lt;/code&gt; argument with defaults.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;Dragon&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;Stomach&lt;/span&gt; &lt;span class="n"&gt;stomach&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;Mouth&lt;/span&gt; &lt;span class="n"&gt;mouth&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;Dragon&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BigStomach&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;BigMouth&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="nf"&gt;Dragon&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Stomach&lt;/span&gt; &lt;span class="n"&gt;stomach&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Mouth&lt;/span&gt; &lt;span class="n"&gt;mouth&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;stomach&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stomach&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;mouth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mouth&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="nf"&gt;eat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Food&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mouth&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;consume&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;stomach&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;digest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;food&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="nf"&gt;dracarys&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Target&lt;/span&gt; &lt;span class="n"&gt;target&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;mouth&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;breatheFire&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&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;Constructors are mandatory for classes, but if one isn't explicitly defined then the compiler will treat the class to a zero-argument &lt;a href="https://en.wikipedia.org/wiki/Default_constructor"&gt;default constructor&lt;/a&gt; totally free of charge.&lt;/p&gt;

&lt;p&gt;We've also split declaration and instantiation/initialisation of &lt;code&gt;viserion&lt;/code&gt; across two lines, which isn't necessary - we can also declare all of our assignment on a single line if we'd like. We don't even need to assign our objects to variables, either, and can instead use them directly in an expression. Let's bring all of that together and create another &lt;code&gt;Dragon&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Dragon&lt;/span&gt; &lt;span class="n"&gt;drogon&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;Dragon&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;GiantStomach&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;GiantMouth&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We're declaring a reference variable &lt;code&gt;drogon&lt;/code&gt;, instantiating our Dragon class with &lt;code&gt;new&lt;/code&gt; and then initialising it with our constructor which responds to the &lt;code&gt;Stomach&lt;/code&gt; and &lt;code&gt;Mouth&lt;/code&gt; signature. We're also instantiating two new objects, &lt;code&gt;GiantStomach&lt;/code&gt; and &lt;code&gt;GiantMouth&lt;/code&gt;, directly in our constructor call.&lt;/p&gt;

&lt;p&gt;Now, how about a real example? Look at how &lt;code&gt;ArrayList&lt;/code&gt; features three constructor signatures in the Java source code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Constructs an empty list with the specified initial capacity.&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;initialCapacity&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;initialCapacity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&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;elementData&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;Object&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;initialCapacity&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;initialCapacity&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="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;elementData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;EMPTY_ELEMENTDATA&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Illegal Capacity: "&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;initialCapacity&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Constructs an empty list with an initial capacity of ten.&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ArrayList&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;elementData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DEFAULTCAPACITY_EMPTY_ELEMENTDATA&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Collection&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;elementData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toArray&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;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;elementData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="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;elementData&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;[].&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;elementData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;copyOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;elementData&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;[].&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;elementData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;EMPTY_ELEMENTDATA&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;Yikes, that was quite a lot to take in. Consider it work on the foundations. If you're anything like me, you often &lt;em&gt;know&lt;/em&gt; this stuff without actually, you know, &lt;em&gt;knowing&lt;/em&gt; it, so it's nice to have a refresher.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java Jargon #2: Concrete Classes
&lt;/h3&gt;

&lt;p&gt;This word pops up all over the place and I've found you're supposed to know what it means by default. For the record, I didn't really know what it meant for &lt;strong&gt;months&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To summarise it in a sentence, a concrete class is any class you can create (instantiate) with the &lt;code&gt;new&lt;/code&gt; keyword. Concrete classes have &lt;em&gt;all&lt;/em&gt; of their methods implemented, regardless of however many interfaces they &lt;code&gt;implement&lt;/code&gt; or classes they &lt;code&gt;extend&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There's no specific language keywords needed when it comes to making something concrete, but you'll probably see the word pop-up a lot in UML diagrams and people shooting the breeze about object-oriented programming, so it's nice to know what's going on so you can take part in the conversation.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;Dragon&lt;/code&gt; class is bona fide, 100% certified concrete, which is totally easy to do when you're not using anything abstract. I find it easier to think of classes as concrete by default and only really in terms of concrete and abstract when using either inheritance (via &lt;code&gt;extends&lt;/code&gt;) and interfaces (via &lt;code&gt;implements&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Java Jargon #3: Interfaces
&lt;/h3&gt;

&lt;p&gt;If a concrete class is an instruction manual, an interface is a blueprint - it's a list of unimplemented method signatures that, by stating we will &lt;code&gt;implement&lt;/code&gt; the interface, our class is pledging to support. When a class opts to &lt;code&gt;implement&lt;/code&gt; an interface, this is often said to be a &lt;strong&gt;contract&lt;/strong&gt; - because the class that's implementing is essentially promising to include these methods.&lt;/p&gt;

&lt;p&gt;Not fulfilling an Interface contract will also have the compiler visit you in the dead of night, provided you're compiling your code at night. It won't be happy about it.&lt;/p&gt;

&lt;p&gt;A Java class can implement many interfaces, as you can see with &lt;code&gt;ArrayList&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;implements&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="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="nc"&gt;RandomAccess&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Cloneable&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Serializable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You'll spot a lot of the '-able' interfaces when you work in Java - here we can see &lt;code&gt;Cloneable&lt;/code&gt; and &lt;code&gt;Serializable&lt;/code&gt;, and you'll see these pop up time and time again, but for now we'll focus on &lt;code&gt;List&lt;/code&gt;. Notice how &lt;code&gt;List&lt;/code&gt; uses the &lt;code&gt;interface&lt;/code&gt; keyword instead of &lt;code&gt;class&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="c1"&gt;//... there's plenty more empty methods&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Neither &lt;code&gt;size()&lt;/code&gt; nor &lt;code&gt;isEmpty()&lt;/code&gt; are implemented in this interface. We couldn't create a &lt;code&gt;List&lt;/code&gt; object from this interface with the &lt;code&gt;new&lt;/code&gt; keyword, which is why it's not a concrete class. But, by implementing &lt;code&gt;List&lt;/code&gt;, &lt;code&gt;ArrayList&lt;/code&gt; pledges to follow the contract and ensure it knows how to respond when sent requests for any of the interface method signatures.&lt;/p&gt;

&lt;p&gt;Interfaces are like a little bow tie for some of your classes - they add a neat degree of formality, which the type checker respects. You can use interface names as the type for your reference variables, too, which means any class implementing that interface will be allowed to be assigned to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java Jargon #4: Abstract Classes
&lt;/h3&gt;

&lt;p&gt;Abstract classes can have both implemented and unimplemented methods, but they're not concrete because they can't create an object with the &lt;code&gt;new&lt;/code&gt; keyword. However, subclasses can use the &lt;code&gt;extends&lt;/code&gt; keyword to inherit their functionality.&lt;/p&gt;

&lt;p&gt;Many of Java's List collections inherit from the abstract class &lt;code&gt;AbstractList&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// AbstractList.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AbstractList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;E&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="no"&gt;E&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;abstract&lt;/code&gt; keyword used to denote both an abstract class and an abstract method: use of the latter requires declaration of the former. Also notice how the abstract method &lt;code&gt;get&lt;/code&gt;  lacks braces and features a semicolon, similar to the way we declared our interface methods earlier.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ArrayList&lt;/code&gt; is a subclass of &lt;code&gt;AbstractList&lt;/code&gt;, and so has to implement a &lt;code&gt;get&lt;/code&gt; method but will inherit the &lt;code&gt;add&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ArrayList.java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AbstractList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kd"&gt;implements&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="no"&gt;E&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="nc"&gt;RandomAccess&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Cloneable&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;Serializable&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="no"&gt;E&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;checkIndex&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;elementData&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;ArrayList&lt;/code&gt; did not implement its own &lt;code&gt;get&lt;/code&gt; method while extending &lt;code&gt;AbstractList&lt;/code&gt; it would have to declare its class &lt;code&gt;abstract&lt;/code&gt; in order to satiate the compiler, which would mean it could not create objects using the &lt;code&gt;new&lt;/code&gt; keyword and would also be unable to reach that much-desired concrete status.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping Up
&lt;/h3&gt;

&lt;p&gt;We've taken a look at what makes a concrete and abstract class in Java, which meant familiarising ourself with how classes are instantiated. We moved from a simple example &lt;code&gt;Dragon&lt;/code&gt; class to looking at some slices of source code from JDK 12, taking a look at how &lt;code&gt;ArrayList&lt;/code&gt; extends &lt;code&gt;AbstractList&lt;/code&gt; and implements &lt;code&gt;ArrayList&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Understanding these building blocks creates a massively useful foundation for getting to grips with the strict object-oriented nature some of Java's most prominent design patterns.&lt;/p&gt;




&lt;p&gt;Has this post been useful for you? I'd really appreciate any comments and feedback on whether there's anything that could be made clearer or explained better. I'd also massively appreciate any corrections! &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
      <category>oop</category>
    </item>
    <item>
      <title>Where's my inheritance? Understanding the Liskov Substitution Principle</title>
      <dc:creator>Martin Gaston</dc:creator>
      <pubDate>Sun, 12 May 2019 09:20:50 +0000</pubDate>
      <link>https://forem.com/martingaston/where-s-my-inheritance-understanding-the-liskov-substitution-principle-1911</link>
      <guid>https://forem.com/martingaston/where-s-my-inheritance-understanding-the-liskov-substitution-principle-1911</guid>
      <description>&lt;p&gt;What's the Liskov Substitution Principle? In the SOLID team, Liskov must have been subbed on more times than Olivier Giroud during his Arsenal tenure. In all seriousness, Barbara Liskov is a totally awesome &lt;a href="https://en.wikipedia.org/wiki/Barbara_Liskov"&gt;pioneer of computer science whose Wikipedia page is well worth a read&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;The design principle, essentially, specifies that a subclass should be substitutable for its superclass. Let's stop right there just to clarify some of that jargon: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Superclass&lt;/strong&gt;: The &lt;em&gt;parent&lt;/em&gt; class (object) being inherited from. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Subclass&lt;/strong&gt;: The &lt;em&gt;child&lt;/em&gt; class receiving (via the &lt;code&gt;extends&lt;/code&gt; keyword) the inheritance. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yeah, we're talking about inheritance. Now, you probably do what I do when you hear the word &lt;em&gt;inheritance&lt;/em&gt;: format your computer and run for the hills. It's the safest way.&lt;/p&gt;

&lt;p&gt;Inheritance is powerful, sure. Having easy, natural access to all those &lt;code&gt;Array&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt; methods in JavaScript is great. Getting all that functionality from descending from &lt;code&gt;Object&lt;/code&gt; in Ruby is pretty awesome. I'm usually pretty delighted to use inheritance when it's baked into the language spec. But maybe it's also too powerful, like the Hulk, and when it loses control it smashes up your application and breaks your heart. I don't think we should always avoid inheritance, but I do think being nervous around it is a healthy, survivalist instinct. It's fire. &lt;/p&gt;

&lt;p&gt;To illustrate the situation, let's totally violate the Liskov Substitution Principle. Let's &lt;a href="https://jestjs.io/"&gt;create tests&lt;/a&gt; for a prominent and respectable business tycoon, who can give interviews and work for their enormous fortune. Then let's build a quick class to pass our contrived example. &lt;/p&gt;

&lt;h3&gt;
  
  
  liskov.test.js
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Liskov&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./liskov&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;describe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tycoon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;tycoon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Liskov&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Tycoon&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Gives Long Interviews To Big Newspapers&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tycoon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;interview&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;toBeTruthy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Goes to work for their fortune&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tycoon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;work&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;toBeTruthy&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;h3&gt;
  
  
  liskov.js
&lt;/h3&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Tycoon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// initialisation&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;work&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="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;interview&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="kc"&gt;true&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;Awesome! What an impressive display of industrious capitalism. Now let's create a child &lt;code&gt;Socialite&lt;/code&gt; class of our &lt;code&gt;Tycoon&lt;/code&gt;, who should be able to neatly get swapped in to any instance we might want to use our &lt;code&gt;Tycoon&lt;/code&gt; to do glamorous interviews or enterprising work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Socialite&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Tycoon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// initialisation&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;work&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="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sounds horrible&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;interview&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="kc"&gt;true&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;Drat. Our industry, the backbone of our economy, will &lt;em&gt;totally collapse&lt;/em&gt; if we try and swap in our &lt;code&gt;Socialite&lt;/code&gt; class! &lt;/p&gt;

&lt;p&gt;We're inheriting our classes, then, but their intended &lt;strong&gt;behaviour&lt;/strong&gt; is all different even though they can often look the same. Commonly, inheritance is used to refer to &lt;code&gt;IS-A&lt;/code&gt; relationships that can inherit via (language permitting) class or interface. Academically it can &lt;a href="https://en.wikipedia.org/wiki/Circle-ellipse_problem"&gt;boil down to the Square/Rectangle problem&lt;/a&gt;, and in real life in can be neatly distilled to everything breaking two minutes before you were due to go home for the day. &lt;/p&gt;

&lt;p&gt;Violating the Liskov Substitution Principle is great way to realize how the real-world concept of objects doesn't directly map to object oriented &lt;em&gt;programing&lt;/em&gt;, which totally kind of scared me when I first realised it as it would totally undo some of those contrived 'intro to OOP' examples, rife with analogy, we're so used to seeing.  &lt;/p&gt;

&lt;p&gt;In its place within the SOLID principles, Liskov Substitution sits quite happily alongside the Open/Closed Principle, which states that a class should be closed for modification - in short, &lt;a href="https://gaston.space/2019-04-25-exploring-the-open-closed-principle-with-the-ninja-turtles/"&gt;we should override the original class to add new functionality to our application&lt;/a&gt;. Our fancy new subclasses can bring additional functionality to the party, but they'll still be able to hit all of the same notes as the classes they're descending from. &lt;/p&gt;

&lt;p&gt;It's always worth remembering not to over-engineer ourselves, though, and not to add the complexity of these patterns until they're needed to scale our application further. Together, and used in the appropriate situations, the Liskov Substitution Principle is another design pattern that can help us plan for the inevitability of change in our applications as they grow. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oop</category>
      <category>solid</category>
      <category>design</category>
    </item>
  </channel>
</rss>
