<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jonathan Kuhl</title>
    <description>The latest articles on DEV Community by Jonathan Kuhl (@jckuhl).</description>
    <link>https://dev.to/jckuhl</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%2F113700%2F64dc8136-15f9-4b8e-a348-5f33fc07cf12.jpeg</url>
      <title>DEV Community: Jonathan Kuhl</title>
      <link>https://dev.to/jckuhl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jckuhl"/>
    <language>en</language>
    <item>
      <title>A Programmer Goes Shopping</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Wed, 03 Jul 2019 23:00:14 +0000</pubDate>
      <link>https://dev.to/jckuhl/a-programmer-goes-shopping-20af</link>
      <guid>https://dev.to/jckuhl/a-programmer-goes-shopping-20af</guid>
      <description>&lt;p&gt;A Programmer is told by his wife "please go buy milk, and if they have eggs, buy a dozen."&lt;/p&gt;

&lt;p&gt;The programmer returns with a dozen jugs of milk.&lt;/p&gt;

&lt;p&gt;When his wife angrily asked him why, he said, "they had eggs."&lt;/p&gt;

</description>
      <category>jokes</category>
    </item>
    <item>
      <title>When isn't inheritance used?</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Sat, 29 Jun 2019 22:27:41 +0000</pubDate>
      <link>https://dev.to/jckuhl/when-isn-t-inheritance-used-273c</link>
      <guid>https://dev.to/jckuhl/when-isn-t-inheritance-used-273c</guid>
      <description>&lt;p&gt;I was asked in an interview "when would you &lt;em&gt;not&lt;/em&gt; use inheritance."  And I paused because the only thing I could articulate was "when it doesn't make sense?"&lt;/p&gt;

&lt;p&gt;This was for a .NET position, by the way.&lt;/p&gt;

&lt;p&gt;The thing that puzzled me about this question was that I didn't really know how to say it.  You don't use inheritance when it doesn't apply to the software architecture.  And that's more or less what I said, but I was wholly unsatisfied with my answer.&lt;/p&gt;

&lt;p&gt;After the interview was done as I was doing my after-action report, I had this sneaking suspicion that he wanted me to mention composition.  And as I write this post, I also feel like I should have added immutability, you don't want inheritance on objects that are meant to be immutable (like Java's &lt;code&gt;String&lt;/code&gt; class).&lt;/p&gt;

&lt;p&gt;But now it bothers me.  What would be a good answer to that question?&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>A Trick With Map</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Fri, 14 Jun 2019 04:09:27 +0000</pubDate>
      <link>https://dev.to/jckuhl/a-trick-with-map-55hl</link>
      <guid>https://dev.to/jckuhl/a-trick-with-map-55hl</guid>
      <description>&lt;h1&gt;
  
  
  The Problem
&lt;/h1&gt;

&lt;p&gt;Consider the following problem I came across on &lt;a href="https://www.reddit.com/r/learnjavascript/"&gt;r/learnjavascript&lt;/a&gt;:  Why does &lt;code&gt;[1,3,11].map(parseInt)&lt;/code&gt; result in &lt;code&gt;[1, NaN, 3]&lt;/code&gt;?&lt;/p&gt;

&lt;h1&gt;
  
  
  The Answer
&lt;/h1&gt;

&lt;p&gt;It is common for new developers to be unaware of how map works and how it feeds parameters to the function it is provided.  We're all aware that the first parameter it gives is the element in the array we're currently iterating over.&lt;/p&gt;

&lt;p&gt;For example: &lt;code&gt;[1,3,11].map(element =&amp;gt; console.log(element))&lt;/code&gt; gives us an output that lists 1, 3, 11.&lt;/p&gt;

&lt;p&gt;But the fact is, map actually feeds three parameters to the given function.  Map provides the current element being iterated over, the current index we're at, and the array being iterated over.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;//... )&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can see this by changing our console.log by referring to it directly rather than wrapping it in an anonymous function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&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="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is pasted directly from the node REPL.  You can see here, we get on each line, the element, the index, and the array.&lt;/p&gt;

&lt;p&gt;So back to the original problem, why do we get &lt;code&gt;[1, NaN, 3]&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Because these three arguments are being passed into &lt;code&gt;parseInt&lt;/code&gt;.  I believe that many new developers forget that &lt;code&gt;parseInt&lt;/code&gt; actually takes &lt;em&gt;two&lt;/em&gt; arguments, more than they would forget that &lt;code&gt;map&lt;/code&gt; provides three arguments.  &lt;code&gt;parseInt&lt;/code&gt; takes a number and the &lt;em&gt;radix&lt;/em&gt;.  The radix tells &lt;code&gt;parseInt&lt;/code&gt; which number system we're using.  0 is decimal, 2 is binary, 3 is trinary, 8 is octal, 16 is hex, and so on and so forth.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;parseInt(11, 3)&lt;/code&gt; outputs &lt;code&gt;2&lt;/code&gt; because that's it's trinary equivalent.&lt;/p&gt;

&lt;p&gt;So on each pass in &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;parseInt&lt;/code&gt; looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The array argument is ignored because &lt;code&gt;parseInt&lt;/code&gt; only takes two arguments.  The number provided becomes the element from the array and the radix comes from the index.&lt;/p&gt;

&lt;p&gt;On the first pass, we're converting 1 to decimal, which is of course, 1.  A radix of 0 and 10 are &lt;em&gt;decimal&lt;/em&gt;, so nothing changes with decimal numbers.  On the second pass, we get &lt;code&gt;NaN&lt;/code&gt; because the value &lt;code&gt;1&lt;/code&gt; is invalid for a radix.  And on the third pass, we're converting &lt;code&gt;11&lt;/code&gt; to &lt;code&gt;2&lt;/code&gt;.  &lt;code&gt;11&lt;/code&gt; in binary is of course, &lt;code&gt;3&lt;/code&gt;.  We have a 1 in the 2^1 position and a 1 in the 2^0 position, 2^1 + 2^0 = 2 + 1 = 3.&lt;/p&gt;

&lt;p&gt;In short, the reason we get unexpected values, is because &lt;code&gt;map&lt;/code&gt; provides more arguments to the &lt;code&gt;parseInt&lt;/code&gt; function than we desired.&lt;/p&gt;

&lt;h1&gt;
  
  
  How do we fix this?
&lt;/h1&gt;

&lt;p&gt;The solution is very simple, we don't directly pass a reference to a function like &lt;code&gt;parseInt&lt;/code&gt; to &lt;code&gt;map&lt;/code&gt;.  Instead, it's best to wrap it in an anonymous function first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&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;blockquote&gt;
&lt;p&gt;Of course you can omit the &lt;code&gt;index&lt;/code&gt; or &lt;code&gt;array&lt;/code&gt; parameters if you aren't using them, I left them in for illustration&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then you can pass your arguments to the function as needed.&lt;/p&gt;

&lt;p&gt;Now this is only necessary for functions that take, or potentially take, multiple arguments.  If you want to map with a function that only takes one argument, feel free to pass it the reference directly.&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;square&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//  [1, 4, 9]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Just be careful to be sure whatever you pass into &lt;code&gt;map&lt;/code&gt;, and this goes for &lt;code&gt;foreach&lt;/code&gt; and many of the other array methods as well, only takes one argument.&lt;/p&gt;

&lt;p&gt;You can read more about how &lt;code&gt;parseInt&lt;/code&gt; works on the Mozilla Developer Network &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy coding.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>The Definition of Recursion is Recursion</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Thu, 13 Jun 2019 17:31:12 +0000</pubDate>
      <link>https://dev.to/jckuhl/the-definition-of-recursion-is-recursion-4iai</link>
      <guid>https://dev.to/jckuhl/the-definition-of-recursion-is-recursion-4iai</guid>
      <description>&lt;h1&gt;
  
  
  Recursion
&lt;/h1&gt;

&lt;p&gt;In programming, there are two types of looping, recursion and iteration.  Iteration is the most common form of looping, using your basic &lt;code&gt;while&lt;/code&gt; and &lt;code&gt;for&lt;/code&gt; loops or more complicated generators.  Recursion however is when a function is called within its own definition.&lt;/p&gt;

&lt;p&gt;Recursion occurs when an algorithm to solve a problem involves repeating the same steps in a diminishing pattern, until you get to a condition where you can break out of the recursion.&lt;/p&gt;

&lt;p&gt;To illustrate, let's consider multiplication as being solvable by recursive addition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x * y = x + x(y-1) + x(y-2) + . . . x(y-n) where y-n &amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In any form of multiplication, x * y, the multiplication can be thought of as x plus x times y minus one.  Repeat, until y is one.  Let's take a look with actual numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = 3, y = 4
3 * 4
= 3 + 3 * 3
= 3 + 3 + 3 * 2
= 3 + 3 + 3 + 3 * 1
= 3 + 3 + 3 + 3
= 12
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note how it is recursive because you're repeating the same steps over and over again as the value of y diminishes.  When y is 1, we have our &lt;em&gt;base case&lt;/em&gt; where we break out of the recursion.  When this happens, the recursion ends, and we simply add up all the threes.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Stack
&lt;/h1&gt;

&lt;p&gt;So how does this work programmatically?  Well, whenever you call a function, a new frame is added to the stack.  The &lt;em&gt;stack&lt;/em&gt; is a region in memory that handles your functions.  When the function completes, it's output value, if it has one, is returned to the stack that called it.  This is why the keyword &lt;em&gt;return&lt;/em&gt; is named what it is.  We're &lt;em&gt;returning&lt;/em&gt; to the lower stack frame.&lt;/p&gt;

&lt;p&gt;At the bottom is our problem, 3 * 4.  To solve this issue, we add a new frame to the stack and input the next step of the problem, which is the bold part of 3 + &lt;strong&gt;3 + 3 * 3&lt;/strong&gt;.  Meanwhile, the current frame will wait for the frame above to return.  That frame, will then do the same, making a new frame and inputting the bold part of 3 + 3 + &lt;strong&gt;3 + 3 * 2&lt;/strong&gt;.  And again, the frame will wait for the frame above to return.  This continues until we reach the base case.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If there is no base case, the frames will stack on each other until the stack runs out of memory, which leads to a &lt;em&gt;stack overflow&lt;/em&gt; error.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once we reach the base case, the frames start returning.  The top frame will return a 3, and pass it to the frame beneath it, which can then finish its calculation and return a 6, and so on and so forth until the frame at the bottom finally receives the returned value it needs and returns a 12.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a-U4GIh8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mz0ehvwg85akvvw29nc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a-U4GIh8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/mz0ehvwg85akvvw29nc5.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the image above, you can see the whole process laid out.  We start at "start" and go up, creating new frames until we reach the base case, then we start going down the stack back to the bottom, returning the value of the function in the frame above it all the way down.&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;Most languages support recursion.  As Python is one of the easiest languages to read, I'll show you how this can be implemented in programming.&lt;/p&gt;

&lt;p&gt;The first thing to think about is, where does the recursion end?  This is called the &lt;em&gt;base case&lt;/em&gt;. In the above example of &lt;code&gt;3 * 4&lt;/code&gt;, our base case was when we reached &lt;code&gt;3 + 3 + 3 + 3 * 1&lt;/code&gt;.  In terms of &lt;code&gt;x * y&lt;/code&gt;, it's when &lt;code&gt;y = 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If x is 3:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 * y
= 3 + 3 * (y - 1)
= 3 + 3 . . . + 3 * y: y = 1
= 3 + 3 . . . + 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When y is 1, the recursion is complete, and the stack can collapse to the value we want.&lt;/p&gt;

&lt;p&gt;So now we have the base case, we also need to know which part is the recursion.  And that part is the &lt;code&gt;x(y - 1)&lt;/code&gt; portion of &lt;code&gt;x + x + ... + x(y - 1)&lt;/code&gt; of our equation.  This is the part that repeats in each step.  The repetition of x + x...+x is resolved as the stack collapses.&lt;/p&gt;

&lt;p&gt;Here's the most basic version of our function, in python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;if y is one, we're at our base case.  Because &lt;code&gt;x * 1&lt;/code&gt; is &lt;code&gt;x&lt;/code&gt;, we can just return &lt;code&gt;x&lt;/code&gt;.  Otherwise we're going to add the recursive portion of our algorithm, &lt;code&gt;x(y-1)&lt;/code&gt;, to x and return that value.  &lt;code&gt;x(y-1)&lt;/code&gt; translates to &lt;code&gt;mult(x, y-1)&lt;/code&gt; in this case.&lt;/p&gt;

&lt;p&gt;Now we can just call the function and give it a try:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]]&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here I have it set up so we can call the function with arguments in the command line.  We're using a list comprehension to turn those arguments into integers and unpacking them into variables x and y, which we then pass into the &lt;code&gt;mult()&lt;/code&gt; function, and print to the screen.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We have to slice &lt;code&gt;sys.argv&lt;/code&gt; because the zeroth value is always the name of the file, we only want the first and second arguments.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We call with &lt;code&gt;python3 multiply.py 3 4&lt;/code&gt; and it will display a 12.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The command might be different on your machine, depending on how python is installed and aliased in your PATH, and how you've named the file.  The syntax is &lt;code&gt;[Python] [filename.py] [x] [y]&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When we enter 3 and 4 as x and y, &lt;code&gt;mult(3,4)&lt;/code&gt; is called and placed in a new frame on the stack.  This function in turn calls itself with &lt;code&gt;mult(3, 4-1)&lt;/code&gt; and pushes that on the stack.  And this repeats until &lt;code&gt;mult(3,1)&lt;/code&gt; is called, where y is 1 and the base case is met.&lt;/p&gt;

&lt;p&gt;This final base case will return a &lt;code&gt;3&lt;/code&gt; to the frame beneath it.  The function in that frame can now finally complete its return statement and return a value to the frame beneath it, and so on and so forth, collapsing to the bottom.  What we end up with is &lt;code&gt;3 + 3 + 3 + 3&lt;/code&gt;, which is 12.&lt;/p&gt;

&lt;h1&gt;
  
  
  Improving our function
&lt;/h1&gt;

&lt;p&gt;However our function only works with positive or zero values of y.  Negative values will always result in a stack overflow (or in Python, &lt;code&gt;RecursionError: maximum recursion depth exceeded in comparison&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;We can make our function work with negatives.  The logic is the same, we just have to switch some of the signs around because we're going from left to right on the number line now, whereas with positive values, we're going from right to left.  &lt;code&gt;x&lt;/code&gt; needs to be made negative and &lt;code&gt;y&lt;/code&gt; needs to increase to positive 1 rather than decrease to positive 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 * -4
= -3 + 3 * -3
= -3 - 3 + 3 * -2
= -3 - 3 - 3 + 3 * -1 
= -3 - 3 - 3 - 3
= -12
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or in Python, we add a new &lt;code&gt;if&lt;/code&gt; statement:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'__main__'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]]&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

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



&lt;p&gt;Now it works with negative values as well.  Negative values in &lt;code&gt;x&lt;/code&gt; are handled by the natural way negatives work in addition and subtraction, so we don't need to worry about that scenario.  Plug in any integer and this function should work.&lt;/p&gt;

&lt;h1&gt;
  
  
  Where Do We Use Recursion?
&lt;/h1&gt;

&lt;p&gt;Of course, this function is useless, Python, and most other programming languages, provides us with a multiplication operator and we don't need to worry about the internal implementation of it.&lt;/p&gt;

&lt;p&gt;But recursion itself is useful wherever you see a repeating pattern where the steps for solving a problem involves repeating the steps of an algorithm again and again until diminishing to some base case.  Another example is a factorial.&lt;/p&gt;

&lt;p&gt;Factorials in math are defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x! = x * (x-1) * (x-2) ... * (x - [x-1])
5! = 5 * (5 -1) * (5 - 2) * (5 - 3) * (5 - 4) = 120
5! = 5 * 4 * 3 * 2 * 1 = 120
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here you can see a repeating pattern where the steps to solve are the same and diminishing towards a base case.  This makes recursion a good tool to solve.  When you can see this pattern, you know recursion is a tool you can use to implement a solution.&lt;/p&gt;

&lt;p&gt;In most cases where recursion is used, iteration can also be used.  Iteration is often cleaner, easier to maintain and understand, and is usually more performant, which is why I prefer iteration.  Sometimes however, recursion really is the simplest solution, even against iteration.&lt;/p&gt;

&lt;p&gt;Take for example Minesweeper.  In Minesweeper, when you click on a blank square, all the squares next to that blank, that are also blank, need to be uncovered.  Those squares in turn need to find their neighbors and if they're blank, they need to be uncovered.  And then those squares also need to find their neighbors and and then be uncovered, so on and so forth until all the blanks in a contiguous region are uncovered.  If you'll note, the steps of the algorithm are repeated over and over again, each square needs to find its neighbors, and each of those neighbors need to find their neighbors, this is the recursive part of the algorithm.  The base case is encountered when no more neighboring squares are found that are blank.&lt;/p&gt;

&lt;p&gt;Here it makes the most sense to solve the problem with recursion.  This was a problem I solved when I implemented a Minesweeper game in React.  You can take a look here: &lt;a href="https://github.com/jckuhl/reactsweeper/blob/master/src/components/context/index.js"&gt;ReactSweeper Context Class&lt;/a&gt;.  Within the &lt;code&gt;clearBlanks&lt;/code&gt; method in our context, is a &lt;code&gt;getAllAdjacentBlanks&lt;/code&gt; method.  It's not a clean method and can clearly use further refactoring, but the point is you can see how it has a base case, if &lt;code&gt;indicesToBeCleared&lt;/code&gt; has no elements, and a recursive case otherwise where it calls itself passing in the new found blank squares.&lt;/p&gt;

&lt;p&gt;The point is, where I can see a repeating pattern, I can usually find a solution with recursion, so long as there's a base case that causes the recursion to stop and then collapse back down to the value I'm looking for.  In multiplication, that value is the product of two numbers.  In factorial, it's the factorial of a number, and in &lt;code&gt;getAdjacentSquare&lt;/code&gt;, it's an array of all the squares that need to be revealed when a blank is clicked.&lt;/p&gt;

&lt;p&gt;Where you see this repeating pattern, is where you can start thinking about recursion.  But be careful.  Interation with loops and generators is usually the simpler and more performant solution and they don't run the risk of stack overflow errors (though they do run the risk of infinite looping.)&lt;/p&gt;

</description>
      <category>recursion</category>
    </item>
    <item>
      <title>Automobiles and OOP, a SOLID understanding</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Wed, 08 May 2019 23:12:57 +0000</pubDate>
      <link>https://dev.to/jckuhl/automobiles-and-oop-a-solid-understanding-3cag</link>
      <guid>https://dev.to/jckuhl/automobiles-and-oop-a-solid-understanding-3cag</guid>
      <description>&lt;h1&gt;
  
  
  SOLID
&lt;/h1&gt;

&lt;p&gt;SOLID is an acronym for five best practice principles in Object Oriented Programming.  The purpose is to make OOP more reusable and maintainable and easier to understand.&lt;/p&gt;

&lt;p&gt;The SOLID Principles are as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Single Responsibility&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class does one thing and one thing well&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Open-Closed&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class is open to extension but not to direct modification&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Liskov Substitution&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A super class can be replaced by a sub class without breaking the program&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Interface Segregation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is better to have small specific interfaces than one big one&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Dependency Inversion&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class should depend on abstraction over concretion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's a bit of a mouthful though so I'm going to extend my car analogy to talk about each principle in kind.&lt;/p&gt;

&lt;h1&gt;
  
  
  Single Responsibility
&lt;/h1&gt;

&lt;p&gt;A car is made of many discrete parts.  Brakes, wheels, engine, etc.  I'm going to focus on the engine.  Now, most people think that the engine's responsibility is to make the car go, but this isn't true.&lt;/p&gt;

&lt;p&gt;The engine's responsibility is to spin the crankshaft.  That's it.  Making the car go is a combined effort of many different parts each that do one single thing.  The engine however doesn't care about the brakes or the axles or the wheels.  It's only job is to turn the crankshaft.  In fact, you can even take the engine out of the car and put it in something else and the engine will do its job.&lt;/p&gt;

&lt;p&gt;Every part in a car behaves this way.  The brakes only care about applying friction to the wheels.  The spark plugs only care about firing in the "combustion" phase of the piston.  &lt;/p&gt;

&lt;p&gt;Now this might be a limitation of mechanical engineering, creating a part that does multiple things at once is not easy, but in programming, it's very easy and even seductive.  Why not make one master class?&lt;/p&gt;

&lt;p&gt;Because it turns into a mess, that's why.  If you have a single object that is concerned with multiple parts of the application, changing one thing here might break something else, somewhere else, in a less predictable manner.  Perhaps the code is old and you've forgotten that this value here is doing double duty over there.&lt;/p&gt;

&lt;p&gt;This is a principle that can be expanded beyond OOP, it's also a good principle for programming in general, especially in functional programming.  A function should do one thing, and do it well.&lt;/p&gt;

&lt;p&gt;But what does it mean to "do one thing?"  After all, it's pointless to have a function or an object that literally only has one line of instruction.  This is a question of design, based on your own judgement.  It's up to you and your team to divide up the software into small discrete parts with distinct roles and create classes, objects and functions to perform them.&lt;/p&gt;

&lt;p&gt;As an example, imagine an object responsible for connecting to your database.  That's all it should do.  It should not also do CRUD operations or expose the public API.  It should connect to the database and manage that connection.  That's it.  Like a car's engine, you'd be able to decouple such an object from your software and use it elsewhere.  If you start building REST methods and CRUD methods into it, it becomes more complex and less reusable.&lt;/p&gt;

&lt;h1&gt;
  
  
  Open-Close
&lt;/h1&gt;

&lt;p&gt;The principle of Open-Close says that an object should not be modified, but merely extended.  This is akin to inheritance.  When Subaru or Ford builds a car, they &lt;em&gt;extend&lt;/em&gt; upon the original concept of a car.  (We'll touch upon this further when I talk about the Dependency Inversion principle.)  We have a base idea of a car that no one wants to change, and for good reason.  If Subaru "changed" the idea of a car, they'd produce a car we wouldn't be able to easily operate.&lt;/p&gt;

&lt;p&gt;Now, the analogy here fails because our concept of a car is abstract, but pretending it was concrete, if you change the base class, then all the derived classes also break.&lt;/p&gt;

&lt;p&gt;Once an object or a module is created, one should be able to write code that extends it, adding fields and properties as needed to the extension, but not to the base object itself.  The base object instead keeps its internals encapsulated and only exposes what it needs to expose.&lt;/p&gt;

&lt;h1&gt;
  
  
  Liskov-Substitution
&lt;/h1&gt;

&lt;p&gt;Liskov-Substitution states that supertypes can be substituted by subtypes without breaking the code.  If I write code with a base class object, I should be able to replace that base class object with any of its subtypes and it still works.  Inheritance is typically the method that allows this to happen.&lt;/p&gt;

&lt;p&gt;Most people know how to drive a car.  If you consider Ford or Subaru or Chevrolet to be subclasses of type Car, you can replace any car with any other car and still know how to operate it.  Each car make and model has different properties, but they're the same enough, because they derive from a base class of Car, that they can be substituted without modifications.&lt;/p&gt;

&lt;h1&gt;
  
  
  Interface Segregation
&lt;/h1&gt;

&lt;p&gt;It is better to have many small specific interfaces than one big one.  A large general purpose interface, much like a large object, is unmaintainable and unwieldy.  Instead, the software should be broken up into smaller components that do single simple things.&lt;/p&gt;

&lt;p&gt;For example, the wipers in my car aren't dependent on some other system (other than the basic electronics) to be active or inactive first.  They don't depend on the state of other parts.&lt;/p&gt;

&lt;p&gt;This has a further use when I can add or remove interfaces as necessary.  Think of a computer for example.  One interface is a keyboard.  It does one thing, it sends keyboard instructions to the motherboard.  It doesn't depend on any other piece (not even the motherboard, as it can plug into &lt;em&gt;other&lt;/em&gt; motherboards and possibly other pieces of hardware.)  You can remove it without affecting the other parts of the computer.&lt;/p&gt;

&lt;p&gt;This is a downside to laptops, especially Macs.  You can't easily swap parts, the interface is one huge chunk, and Mac users don't have the ability to add new RAM or internal ROM.&lt;/p&gt;

&lt;p&gt;Break the software down into independent parts and keep them separate from each other.  Try to think in terms of "plug and play."&lt;/p&gt;

&lt;h1&gt;
  
  
  Dependency Inversion
&lt;/h1&gt;

&lt;p&gt;The principle here is that a class should be based on abstraction, and not concrete implementation.&lt;/p&gt;

&lt;p&gt;Again, we all have an idea of what a car is and how one is to be operated.  The cars we drive, the concrete metal objects we drive, based on concrete schematics stored in the headquarters for the car companies, are based on this abstract idea.  The exact details on how to implement a car are found in the schematics (the class) and constructed in the cars themselves (the instance objects.)&lt;/p&gt;

&lt;p&gt;Java Servlets serve as a great example for object oriented programming.  The hierarchy is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Servlet is an interface.
&lt;/li&gt;
&lt;li&gt;GenericServlet is an abstract class that implements Servlet&lt;/li&gt;
&lt;li&gt;HttpServlet is a concrete class that implements GenericServlet.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We start at the abstract idea of what a servlet is.  The &lt;em&gt;interface&lt;/em&gt; lets us know what the classes need to implement.  The &lt;em&gt;abstract class&lt;/em&gt; will provide more details on what needs to be implemented by extending classes and will provide some behavior and properties that are already set.  HttpServlet is a concrete class based on those two higher templates.&lt;/p&gt;

&lt;p&gt;We know that HttpServlet implements &lt;code&gt;init()&lt;/code&gt;, &lt;code&gt;destroy()&lt;/code&gt; and &lt;code&gt;service()&lt;/code&gt; because the interface at the top demands that it does.  This is a top down system where the top lays out the behavior and state that the extending/implementing class needs to perform and the class at the bottom provides the implementation.&lt;/p&gt;

&lt;p&gt;This is a best practice because the higher implementation details are, the more brittle the whole inheritance chain is.  If I change something at the top, that effect can ripple downward and break everything beneath.  Dependency inversion puts only a contract at the top, the derived objects decide for themselves what the implementation details are.&lt;/p&gt;

&lt;p&gt;There's a basic idea of what a car is, a Subaru Outback is a specific concrete implementation of what a car is.  A car is a vague definition with few details, a Subaru Outback is a specific definition with tangible details.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;SOLID is a set of five principles to help a developer create software that is reusable, understandable and easier to maintain.  Knowing and applying these principles will make you a better programmer over all.&lt;/p&gt;

&lt;p&gt;Happy coding.&lt;/p&gt;

</description>
      <category>oop</category>
      <category>solid</category>
    </item>
    <item>
      <title>Making A Switch In CSS</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Sat, 30 Mar 2019 18:28:05 +0000</pubDate>
      <link>https://dev.to/jckuhl/making-a-switch-in-css-5no</link>
      <guid>https://dev.to/jckuhl/making-a-switch-in-css-5no</guid>
      <description>&lt;h1&gt;
  
  
  Let's switch it up!
&lt;/h1&gt;

&lt;p&gt;For a recent project I thought I'd use Materialize CSS and after working with it, determined that, while it's a good library, it wasn't for my project.  However, I really liked the switch element they designed so I decided to remake it myself in CSS.&lt;/p&gt;

&lt;p&gt;Doing so required I go into areas in CSS I'd never gone before, namely, pseudoelements.  By manipulating &lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt;, I can make a functional switch out of a checkbox.&lt;/p&gt;

&lt;p&gt;Below is the final product.  It's a switch that toggles from off to on, and turns grey and unusable when disabled.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/jkuhl/embed/ZPRYOw?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting Started
&lt;/h1&gt;

&lt;p&gt;We're going to begin with the humble checkbox and wrap it in a label, as we should so that when the user clicks on the label, it will toggle the checkbox&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For accessibility purposes you should always use labels in conjunction with form elements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To ensure our CSS doesn't affect other checkboxes on our site, we'll give the label a class, and modify that class and its descendents as we continue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"switch"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Off
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  On
&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's set up the CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first thing we need to do is get rid of the default checkbox styling.  This took some research, but I found the solution, &lt;code&gt;-webkit-appearance: none;&lt;/code&gt;.  Our checkbox will now disappear.&lt;/p&gt;

&lt;p&gt;Then we need to determine the placement of our knob (the red or blue circle) and the little slider the knob "travels" on (the grey rectangle in the back).  These will be made with pseudo elements and they need to be moved &lt;em&gt;relative&lt;/em&gt; to the &lt;em&gt;position&lt;/em&gt; of the checkbox element.&lt;/p&gt;

&lt;p&gt;That's a big hint, we will need to style the knob for absolute positioning, but absolute positioning, as a default will be based off the whole window.  We'll want &lt;code&gt;position: relative&lt;/code&gt; on our checkbox because it is the parent element for the &lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt; pseudo elements.&lt;/p&gt;

&lt;p&gt;Finally, we'll want some spacing around our switch, once it appears, so the "on" and "off" text doesn't smash right next to it, and we can do this with margin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;-webkit-appearance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;1rem&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;That sets up our checkbox.  But right now, we have nothing to see, so lets make the slider&lt;/p&gt;

&lt;h1&gt;
  
  
  Mmmm Sliders
&lt;/h1&gt;

&lt;p&gt;Nothing would please me more than a plate of mini burgers.  But this a CSS article, not a local small town grill with American food, country music and goofy sh*t on the walls.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/2agCyh9S58i2I/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/2agCyh9S58i2I/giphy.gif" alt="Shenanigans"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this context, the slider is just the track our knob travels on.  We'll make it out of the &lt;code&gt;::after&lt;/code&gt; pseudo element.&lt;/p&gt;

&lt;p&gt;But first, what is the &lt;code&gt;::after&lt;/code&gt; element, and for that matter, the &lt;code&gt;::before&lt;/code&gt; element?  Well, pseudo elements are elements that come with various tags that can handle various things.  I know that's a very generic and unhelpful response, but there are a lot of them.  Some, like &lt;code&gt;::first-line&lt;/code&gt; can modify the first line of a paragraph.  There's an experimental one that lets you mess around with &lt;code&gt;::spell-check&lt;/code&gt;.  The MDN has a lot of information on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements"&gt;pseudo elements&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;::before&lt;/code&gt; and &lt;code&gt;::after&lt;/code&gt; are elements found on most HTML elements and they are considered children of their element.  &lt;code&gt;::before&lt;/code&gt; is the first child and &lt;code&gt;::after&lt;/code&gt; is the last child.  For our purposes, it won't matter which one is the first or last child.  These can be used for a lot of purposes, even creating &lt;a href="https://www.youtube.com/watch?v=Y0_FMCji3iE"&gt;mustaches&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But we're going to keep it simple and use &lt;code&gt;::after&lt;/code&gt; to create our slider.  It'll just be a regular rectangular grey box.&lt;/p&gt;

&lt;p&gt;To target it, we simply need to append &lt;code&gt;::after&lt;/code&gt; to a new css rule&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::after&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;There are five things we need to do to make it appear.  First, pseudo elements need some content.  Ours will be a simple empty string, so &lt;code&gt;content: ''&lt;/code&gt; will suffice.  Next, it needs to have display property.  We'll make it &lt;code&gt;block&lt;/code&gt;, because after some finagling, that's what I found works best here.  And it needs a background color, and I picked &lt;code&gt;grey&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Don't make the mistake I always do of setting the &lt;code&gt;color&lt;/code&gt; property!  &lt;code&gt;color&lt;/code&gt; is for text!  &lt;code&gt;background&lt;/code&gt; is what we want to set!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally, we need height and width.  As it should be set within a body of text, having it based on the root em (rem) seems the most logical.  I set it to be 1 rem wide and 0.5 rem high.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::after&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;content&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="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grey&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;A wild grey rectangle appears!&lt;/p&gt;

&lt;h1&gt;
  
  
  Styling the Knob
&lt;/h1&gt;

&lt;p&gt;Now for the knobs.  Here we have a knob that can be either right or left, but is always a circle, always has a shadow (to make it have some 3d effect) and always has the same content and size, so we want to write our CSS to be DRY, Don't Repeat Yourself.&lt;/p&gt;

&lt;p&gt;We want to write a baseline for our &lt;code&gt;::before&lt;/code&gt; pseudo element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Again, we need to give it content, dimensions and a display property for it to show.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;content&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="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&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;That will make a square for us.  We can't see it yet because there's no color though, so we'll want to make the rules for when the checkbox is checked and unchecked, since the color changes depending on that value.&lt;/p&gt;

&lt;p&gt;Targeting a checked checkbox is simple, we can make a new rule that selects &lt;code&gt;:checked&lt;/code&gt;.  Targeting an unchecked box is a little more tricky because there's no &lt;code&gt;:unchecked&lt;/code&gt;.  We have to use the &lt;code&gt;:not()&lt;/code&gt; pseudo selector, so we can make two new rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:checked::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The order is important here.  We want to target the &lt;code&gt;::before&lt;/code&gt; pseudo-element of a checked input element of type checkbox that is descendent of an element of the switch class.  We don't want &lt;code&gt;:checked&lt;/code&gt; &lt;em&gt;after&lt;/em&gt; &lt;code&gt;::before&lt;/code&gt; because &lt;code&gt;::before&lt;/code&gt; doesn't have a &lt;code&gt;checked&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;If it is checked, I want it to be blue and moved to the left 10 pixels.  And if it is not checked, I want it to be red and moved to the right 10 pixels.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:checked::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&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;Because we're modifying &lt;code&gt;left&lt;/code&gt; and &lt;code&gt;right&lt;/code&gt; attributes, our baseline needs to be set for &lt;code&gt;position: absolute&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;content&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="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we're almost there.  The knob is a bit uncentered on the track, to fix it, I found that setting the top position -0.25 put it where I wanted it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;content&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="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-0.25rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&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 finally, let's make it a circle with some shadow.  Circles are easy, just give it a border radius of 50%.  Shadows on the other hand, I never hand craft shadows, I found a good online tool for that.  I like to use &lt;a href="https://www.cssmatic.com"&gt;CSSMatic&lt;/a&gt; for this.  They have a real-time box shadow tool that autogenerates the code for you.  With the border radius and values I liked, the final CSS for the baseline selector looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;content&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="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-0.25rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;-webkit-box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0.75&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;-moz-box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0.75&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0.75&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 there you have it!  A functional switch!&lt;/p&gt;

&lt;h1&gt;
  
  
  But wait, there's more!
&lt;/h1&gt;

&lt;p&gt;No form element is complete without styles to target &lt;code&gt;:disabled.&lt;/code&gt;  Our users need a visual cue to know this element can't be used at the moment.  For this, we can take our knob, our &lt;code&gt;::before&lt;/code&gt; element, and change the color if the checkbox is disabled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:disabled::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;darkgrey&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;That will turn the knob grey if it is disabled.  Try adding 'disabled' to the &lt;code&gt;input&lt;/code&gt; tag to test it out.&lt;/p&gt;

&lt;h1&gt;
  
  
  And we're done here!
&lt;/h1&gt;

&lt;p&gt;Here's the full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"switch"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  Off
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"checkbox"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  On
&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:checked::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:not&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;:checked&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;content&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="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-0.25rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;-webkit-box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0.75&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;-moz-box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0.75&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;box-shadow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;2px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt; &lt;span class="n"&gt;rgba&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0.75&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;::after&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;content&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="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0.5rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grey&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;-webkit-appearance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.switch&lt;/span&gt; &lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"checkbox"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="nd"&gt;:disabled::before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;darkgrey&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a simple little switch that can add a little flavor to a site.  It works great for binary situations, if the user wants &lt;em&gt;this&lt;/em&gt; or the user wants &lt;em&gt;that&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to use and modify this as you see fit.  Try adjusting size or colors.  Make it fancier, have fun with it.  And if you see any way this can be done better in CSS, let me know.&lt;/p&gt;

&lt;p&gt;Happy coding&lt;/p&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>Some Closure on Closures</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Wed, 06 Mar 2019 22:43:57 +0000</pubDate>
      <link>https://dev.to/jckuhl/some-closure-on-closures-44ga</link>
      <guid>https://dev.to/jckuhl/some-closure-on-closures-44ga</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Closures are a concept that take a while for many new JavaScript developers to get used to.  It's one of the more abstract concepts of JavaScript.  A closure exists when you have a function within a function that has access to the outer function's state.&lt;/p&gt;

&lt;p&gt;What?&lt;/p&gt;

&lt;p&gt;See, that's the definition I always see when someone defines a closure.  But it's not as clear cut as to what it really means, so let me explain&lt;/p&gt;

&lt;h1&gt;
  
  
  Execution Context
&lt;/h1&gt;

&lt;p&gt;When a function is called, JavaScript's engine creates what is called an &lt;em&gt;execution context&lt;/em&gt;.  This context contains all the state required for that function.  In simple terms, state is simply the variables and their current values.&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;function&lt;/span&gt; &lt;span class="nx"&gt;foo&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the function &lt;code&gt;foo()&lt;/code&gt; above, when you call &lt;code&gt;foo()&lt;/code&gt; an execution context is created, the variable &lt;code&gt;a&lt;/code&gt; is set to &lt;code&gt;3&lt;/code&gt; and then the function ends, the context is destroyed and the variable is destroyed and the function returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is also how scope works.  &lt;code&gt;a&lt;/code&gt; is declared in the function and only exists within that function and any lower blocks that may appear in that function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Any internal functions within &lt;code&gt;foo()&lt;/code&gt; can access &lt;code&gt;foo()&lt;/code&gt;'s state.&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;function&lt;/span&gt; &lt;span class="nx"&gt;foo&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;()&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="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But this is just basic scoping right?  Well yes, in this example, but here's what's powerful about closures.  If the outer function &lt;code&gt;foo()&lt;/code&gt; is destroyed, the internal &lt;code&gt;log()&lt;/code&gt; function, if it was brought out of the function, would still have access to &lt;code&gt;foo()&lt;/code&gt;'s state.&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;function&lt;/span&gt; &lt;span class="nx"&gt;foo&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;()&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="nx"&gt;a&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="nx"&gt;log&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;log&lt;/span&gt; &lt;span class="o"&gt;=&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;log&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;// logs 3&lt;/span&gt;

&lt;span class="c1"&gt;// foo()() would also be acceptable, and would also log 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The internal &lt;code&gt;log()&lt;/code&gt; function still has &lt;code&gt;foo()&lt;/code&gt;'s execution context, even though &lt;code&gt;foo()&lt;/code&gt; was called, created, and destroyed.&lt;/p&gt;

&lt;p&gt;To illustrate this further, let's make &lt;code&gt;foo()&lt;/code&gt; take a parameter rather than a hard coded variable.&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;function&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;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;()&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="nx"&gt;a&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="nx"&gt;log&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;log3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;log4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;log3&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;//logs a '3'&lt;/span&gt;
&lt;span class="nx"&gt;log4&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;    &lt;span class="c1"&gt;//logs a '4'&lt;/span&gt;

&lt;span class="c1"&gt;// alternatively&lt;/span&gt;
&lt;span class="nx"&gt;foo&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)();&lt;/span&gt;    &lt;span class="c1"&gt;//logs 'hello'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here you can see &lt;code&gt;foo()&lt;/code&gt; is called 3 times with different values for &lt;code&gt;a&lt;/code&gt; and the returned function still "remembers" the value of &lt;code&gt;a&lt;/code&gt; from the execution context.&lt;/p&gt;

&lt;p&gt;That's essentially what a closure is.  It's an internal function that has access to the the outer function's state.&lt;/p&gt;

&lt;h1&gt;
  
  
  But Why?
&lt;/h1&gt;

&lt;p&gt;Why would I need to use this?  Well, there are a number of situations where its useful to utilize closures.  Generators use closures.  Event handlers use closures.  Partial application of functions use closures.  Closures are a major component of functional programming.&lt;/p&gt;

&lt;p&gt;Here's how you can create a generator in JavaScript.  This one is similar to (but simpler than) Python's &lt;code&gt;range()&lt;/code&gt; object:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is a handmade generator by the way, rather than the built in generator syntax that comes with JavaScript.  For more information on the official JavaScript generators, check out the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator"&gt;Mozilla Developer Network&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&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;count&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="k"&gt;return&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;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;end&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="nx"&gt;start&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&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="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;// logs 1&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="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;// logs 2&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="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;// logs 3&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="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;// logs 4&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="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;// logs 5&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="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;// logs false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The ternary &lt;code&gt;count !== 1 ? step : 0;&lt;/code&gt; is used to ensure the range includes the start value&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;code&gt;range()&lt;/code&gt; function returns an anonymous function that keeps track of the current state of the three parameters passed into the function.  Each time you call &lt;code&gt;r()&lt;/code&gt;, it will return the next iteration of that state, which is mutated by the expression &lt;code&gt;start += step&lt;/code&gt;.  Starting with this range object, it's not terribly difficult to use closures to rewrite many of the JavaScript array functions to functional functions that work on generators instead.&lt;/p&gt;

&lt;p&gt;Here's what &lt;code&gt;map()&lt;/code&gt; might look like.&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;function&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapping&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;range&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="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;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;range&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;false&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="nx"&gt;mapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&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="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;//logs 1&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="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;//logs 4&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="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;//logs 9&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="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;//logs 16&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="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;//logs 25&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="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;    &lt;span class="c1"&gt;//logs false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note that with arrow notation &lt;code&gt;()=&amp;gt; ()=&amp;gt; {}&lt;/code&gt; is what a closure would look like.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here you have a generator to create square numbers.  Each time the function is called, it "remembers" the execution context of the outer function.&lt;/p&gt;

&lt;p&gt;You can, of course, loop over the generators as well.&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;let&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;squares&lt;/span&gt;&lt;span class="p"&gt;())&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="nx"&gt;s&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;blockquote&gt;
&lt;p&gt;Don't get confused with the expression in the while loop.  That &lt;code&gt;=&lt;/code&gt; is an assignment operator, not an equality operator.  &lt;code&gt;=&lt;/code&gt; is a function under the hood however, and it will return the value of &lt;code&gt;squares()&lt;/code&gt;, which is either a number, or false, and thus we can take advantage of truthy/falsy here.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But I felt writing it out was clearer.&lt;/p&gt;

&lt;p&gt;You can see the code for these generators in action at &lt;a href="https://repl.it/@jkuhl87/WheatTameWebportal"&gt;Repl.it&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Saving State
&lt;/h1&gt;

&lt;p&gt;Closures also work when you need to save state.  Imagine you have a large app that needs to connect to multiple mongo databases.  I have an express back end and I need to export multiple connection functions to multiple javascript files.  A closure can be a simple way of doing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//in a file called "database.js"&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&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;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGO_USER&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;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGO_PW&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;db1URI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`mongodb+srv://&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="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;@cluster1.mongodb.net/database1?retryWrites=true`&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;db2URI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`mongodb+srv://&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="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;@cluster2.mongodb.net/database2?retryWrites=true`&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;db3URI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`mongodb+srv://&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="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;@cluster3.mongodb.net/database3?retryWrites=true`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// wrap the connection in a closure so I can export it with the URI&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uri&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="p"&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;password&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&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;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connection error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;once&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;open&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;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="dl"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s1"&gt;Successfully connected to Mongo!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;db1Connect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db1URI&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;db2Connect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db2URI&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;db3Connect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db3URI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;db1Connect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;db2Connect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;db3Connect&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then in various modules in your Express code you could say&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;MongooseConnect&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;./database.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;MongooseConnect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db1Connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//and in another file somewhere else&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MongooseConnect&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;./database.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;MongooseConnect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db2Connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;//etc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the &lt;code&gt;Connect()&lt;/code&gt; method saves the URI parameter passed in in a closure so that later when you actually call it, it can connect to Mongo (through Mongoose) with that URI.  This allows me to have one single function for connecting and one central location with all the connection strings gathered in one spot.  I could simply export a function and pass the string as a parameter, but then I'd have to define a connection string in different files that use the &lt;code&gt;Connect()&lt;/code&gt; function or have an object defined in another file with all the connection strings in one spot.  With a closure, I can simply export the functions and have all my connection strings in one spot where I can maintain them with ease.&lt;/p&gt;

&lt;h1&gt;
  
  
  Events
&lt;/h1&gt;

&lt;p&gt;Closures also work with asynchronous operations and events.  In fact, when you pass a callback to a click handler, that is by definition a closure.  &lt;code&gt;addEventListener&lt;/code&gt; is a function, the handler you pass into it would be the closure.&lt;/p&gt;

&lt;p&gt;Here's a piece of code I wrote when it finally clicked how a closure works for me:&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;function&lt;/span&gt; &lt;span class="nx"&gt;clicker&lt;/span&gt;&lt;span class="p"&gt;()&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;counter&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;myDiv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mydiv&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;btn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;btn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&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;myDiv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;counter&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="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;clicker&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I had a need to have the event listener added to a DOM element within a function and I wrote the above to make sure the concept itself worked.  It's a simple counter, you click on a button the number goes up.  Hurray, I guess.&lt;/p&gt;

&lt;p&gt;But the point is, the anonymous click event handler still has access to the &lt;code&gt;counter&lt;/code&gt; variable and the &lt;code&gt;myDiv&lt;/code&gt; element, even though the &lt;code&gt;clicker()&lt;/code&gt; function will already have its execution context destroyed by the time the user clicks the button (unless he's got a super fast millisecond reaction speed I suppose.)  Even though &lt;code&gt;counter&lt;/code&gt; and &lt;code&gt;myDiv&lt;/code&gt; are scoped to &lt;code&gt;clicker()&lt;/code&gt;, the event handler can still access them.&lt;/p&gt;

&lt;p&gt;Asynchronous functions and events work just fine with closures because that closure &lt;em&gt;still&lt;/em&gt; has access to the state of the enclosing function even if there's some time between the destruction of the enclosing function and the calling of the closure.  If you have some closure that calls some network API and it takes 250 milliseconds to get a response, then that's fine, the closure still has access to the enclosing state.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;Closures are tricky to understand.  Hopefully some of the examples above made it clearer how they work.  Remember, a closure is simply an internal function that has access to the state of the function that it is contained in.  Here's an idea to get a better handle of closures, use the &lt;code&gt;range()&lt;/code&gt; function I provided above and try to make a &lt;code&gt;toArray()&lt;/code&gt; method that gives an array for each value in the generator.  Or try to make a &lt;code&gt;filter()&lt;/code&gt; function or rework any of the other JavaScript Array methods to work on &lt;code&gt;range()&lt;/code&gt;.  I've made a few on my own and they'll all require you to use closures.&lt;/p&gt;

&lt;p&gt;Thank you and happy coding.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>closures</category>
    </item>
    <item>
      <title>Batman and Promises</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Tue, 29 Jan 2019 16:44:29 +0000</pubDate>
      <link>https://dev.to/jckuhl/batman-and-promises-1p2n</link>
      <guid>https://dev.to/jckuhl/batman-and-promises-1p2n</guid>
      <description>&lt;h1&gt;
  
  
  Alfred
&lt;/h1&gt;

&lt;p&gt;Imagine you're the CEO of some business.  Wayne Enterprises I suppose.  Here you are putzing around in the Batcave trying to figure out what the Joker's going to do next, but you, Mr. Wayne, have a hankering for some caffeine.  So you fetch Alfred and tell him, "get me a coffee."&lt;/p&gt;

&lt;p&gt;So Alfred goes to get you a coffee.  While he's out and about, you're still free to continue polishing the Batmobile and searching for information on the Joker on the Batcomputer and fixing the holes and tears in your Bat armor.&lt;/p&gt;

&lt;p&gt;After a little while, Alfred returns.&lt;/p&gt;

&lt;p&gt;Promises are Alfred.  A Promise is a task JavaScript performs with an assurance that it will, at some point, return with some value.  It might not be the value you want, but it will be some sort of value.  Promises are asynchronous.  Actions passed to a Promise are put on the event loop rather than the function stack, which allows the other functions in your code to continue executing while you await your promise.&lt;/p&gt;

&lt;p&gt;Here's a basic Promise:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;doTheThing&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;reject&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;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'something broke'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have some function, &lt;code&gt;doTheThing()&lt;/code&gt; that gets some value.  What it is doesn't really matter.  Maybe it's an API call or something.  Either way, the basics work like this.  If you get the value you want, you pass it to the &lt;code&gt;resolve()&lt;/code&gt; function.  If you don't get the value you want, you call &lt;code&gt;reject()&lt;/code&gt; and pass it an error or some other value you'd want if the Promise returns an error.&lt;/p&gt;

&lt;p&gt;Any object passed to resolve, is then passed to the callback function of any &lt;code&gt;then()&lt;/code&gt; call.  &lt;code&gt;then()&lt;/code&gt; is fired immediately after &lt;code&gt;resolve()&lt;/code&gt; and will perform the action you desire to have happen once your Promise is resolved.  &lt;code&gt;catch()&lt;/code&gt; works with &lt;code&gt;reject()&lt;/code&gt;.  &lt;code&gt;catch()&lt;/code&gt; also takes a callback and the value passed into &lt;code&gt;reject()&lt;/code&gt; is then passed into the callback &lt;code&gt;catch()&lt;/code&gt; uses.  If you have multiple promises and multiple &lt;code&gt;then()&lt;/code&gt; methods chained onto it, &lt;code&gt;catch()&lt;/code&gt; is a one stop shop for all errors that are thrown; you only ever need one &lt;code&gt;catch()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So to rewrite this in terms of our Alfred example:&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;coffee&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;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;getCoffee&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;coffee&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;reject&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;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'no coffee!'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coffee&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;coffee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;coffee&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;drink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;coffee&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Alfred comes back and says he has your favorite coffee!  Your Promise is resolved and you can drink the coffee.  Alfred comes back and tells you he failed to get coffee because the Joker blew up all the coffee shops.  Your Promise has been rejected.  You get mad, suit up and head out in the batmobile to handle this . . . error.&lt;/p&gt;

&lt;h1&gt;
  
  
  Sandwiches
&lt;/h1&gt;

&lt;p&gt;After handling the Joker, you are hungry.  You want a sandwich!  But not just any sandwich.  The Batman has a sophisticated taste after all.  So you tell Alfred, that you want cheese from a specialty cheese shop, meat from your favorite deli, and fresh bread from your favorite baker.&lt;/p&gt;

&lt;p&gt;Alfred now has to handle three promises at once.&lt;/p&gt;

&lt;p&gt;This is where &lt;code&gt;Promise.all()&lt;/code&gt; comes in.  &lt;code&gt;Promise.all()&lt;/code&gt; handles an array of promises and will not resolve until &lt;em&gt;all&lt;/em&gt; the promises in the array are resolved.  If any one of them are rejected, they are &lt;em&gt;all&lt;/em&gt; rejected.&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;sammich&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;getBread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getCheese&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getMeat&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;sammich&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;nom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nom&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When Alfred returns, you either have all three parts to your sandwich, or you don't have a sandwich.&lt;/p&gt;

&lt;p&gt;But why get the individual ingredients one at a time when there are so many delicious Gotham City sandwich shops to choose from?  You decide you want a sandwich, but you can't decide between three different stores, so you'll just eat the first sandwich that is returned from you.  You send Alfred, Robin, and Gordon out separately to get you a sandwich.&lt;/p&gt;

&lt;p&gt;This is &lt;code&gt;Promise.race()&lt;/code&gt;.  &lt;code&gt;Promise.race()&lt;/code&gt; takes an array of Promises and returns the first one to be resolved, skipping any that get rejected.  Only if all of them are rejected, will &lt;code&gt;Promise.race()&lt;/code&gt; be rejected.&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;sammich&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;race&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;sandwich1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sandwich2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;sandwich3&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="nx"&gt;sammich&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;nom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nom&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Robin came back first, so you eat his sandwich.&lt;/p&gt;

&lt;h1&gt;
  
  
  Async/Await
&lt;/h1&gt;

&lt;p&gt;But here's the thing, using &lt;code&gt;.then&lt;/code&gt; and &lt;code&gt;.catch&lt;/code&gt; get clunky.  Can we get around this?  You are after all, &lt;em&gt;The Batman&lt;/em&gt; and your batcave could use a little cleaning up.&lt;/p&gt;

&lt;p&gt;Async/Await offers some clean up.  It allows for your flow control in your code to feel more natural.  The way it works is simple, but it only works on Promise objects.  If you mark a function as &lt;code&gt;async&lt;/code&gt; and mark a line inside that function with &lt;code&gt;await&lt;/code&gt;, execution of that function will halt on that line while waiting for that promise to be resolved or rejected.  Meanwhile, all the other functions will keep executing.  The execution of the async function is thrown into the event loop and will only be executed once a value is returned and the function stack is cleared.&lt;/p&gt;

&lt;p&gt;While Alfred, Gordon and Robin are going about getting your sandwiches, you're free to tinker about the batcave and do whatever you want while you wait, so long as what you want to do doesn't require those sandwiches.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;await&lt;/code&gt; is exactly what it sounds like, the execution &lt;em&gt;must&lt;/em&gt; &lt;code&gt;await&lt;/code&gt; the resolution or rejection of this promise.  Any rejection is picked up by the &lt;code&gt;catch&lt;/code&gt; block of a try/catch.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;waitForCoffee&lt;/span&gt;&lt;span class="p"&gt;()&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;coffee&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;coffee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;getCoffee&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;coffee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;drink&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;thank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Alfred'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forServices&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;blockquote&gt;
&lt;p&gt;We're assuming &lt;code&gt;getCoffee()&lt;/code&gt; returns a Promise in this example.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here you have a more top to bottom flow of control with async/await.  There's no other benefit to using async/await over &lt;code&gt;.then()/.catch()&lt;/code&gt;, it isn't faster or necessarily more performant.  It's just easier to read and manage.&lt;/p&gt;

&lt;p&gt;Just be aware that async/await only works on Promises.  If you try using it on something like the geolocation API, it won't work, unless you wrap those functions in Promises first.&lt;/p&gt;

&lt;p&gt;That is Promises in a nutshell.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>promises</category>
      <category>asyncawait</category>
    </item>
    <item>
      <title>On Automobiles and OOP</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Fri, 18 Jan 2019 20:33:04 +0000</pubDate>
      <link>https://dev.to/jckuhl/on-automobiles-and-oop-1k1k</link>
      <guid>https://dev.to/jckuhl/on-automobiles-and-oop-1k1k</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Stand at any street corner and watch the traffic for a while.  A Subaru Crosstrek.  A Ford Escort.  Now Dodge Ram.  A Subaru WRX with a loud rumbling engine.  A Yugo held together by duct tape and desperation.&lt;/p&gt;

&lt;p&gt;All of these, are cars.  They have the same basic structure.  Four wheels, an engine, a steering wheel, runs on gasoline or diesel.  Yet, they vary greatly in color, horsepower, shape, features, and even the type of gasoline they might use.  Every busy street is a cacophony of different car shapes, yet most of the vehicles we see, everyone will agree, is a car.  And yes, depending on how you define it, your Dodge Ram could be considered "a car."&lt;/p&gt;

&lt;p&gt;This in turn is what Object Oriented Programming (OOP) is about.  An object is a bundle of individual properties that is based on some basic template.  Each individual car is a bundle of individual features based on some basic template.  Of course, some templates are templates of templates.  There is no "basic car", it is an abstract idea, a collection of properties and methods every derivative car must implement, but every derivative is free to implement as they desire within the confines of that abstraction.  We have a general idea of what a car is, and then expanded into different types of cars.  A Subaru Outback is an station wagon.  A GMC Yukon is an SUV.  A Ford Fusion is a sedan and no one really knows what a Honda Fit is.  From these types, station wagon, SUV, sedan, etc, you can find even more specific ideas, like the vehicles I listed as an example. Ultimately, you get to an abstraction specific enough to build individual cars from.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Car
    Sports Utility Vehicle
        GMC Yukon
    Station Wagon
        Subaru Outback
    Sedan
        Ford Fusion
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These are called classes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Classes
&lt;/h1&gt;

&lt;p&gt;If you walk into a Subaru factory, you won't find a group of crazed engineers trying to hobble together a car willy-nilly.  That of course, would be ludicrous, inefficient and lead to the creation of cars that are not uniform.&lt;/p&gt;

&lt;p&gt;Artistically, non-uniformity is typically valued, but in the programming world, it causes problems.  Objects don't fit their required role.  Testing is nearly impossible and therefore quality assurance goes right out the window. Mass production is slowed.&lt;/p&gt;

&lt;p&gt;Instead what you find is a long procedural assembly line of workers (most of whom are now robots) following a precise set of instructions to build a car based on a previously drawn out schematic.&lt;/p&gt;

&lt;p&gt;This schematic is precisely what a class is.  Classes themselves are not the objects they create, but rather a template for creating objects.  If I were to pull the schematics of a 2017 Subaru Outback, it tells me in detail where every nut and bolt goes and how every functionality works and is implemented.&lt;/p&gt;

&lt;h1&gt;
  
  
  Constructors
&lt;/h1&gt;

&lt;p&gt;Of course, there are variations in how individual cars are created.  While, for some reason, orange Subaru Crosstreks (I too enjoy driving pumpkins down the street!) seem to dominate the freeways, obviously, one has a lot of leeway in color  and numerous other minor features.&lt;/p&gt;

&lt;p&gt;The constructor sets properties and initializes them.  It allows us to set the individual details for an individual instance of the object.  Then, it creates the object.&lt;/p&gt;

&lt;p&gt;Constructors, and initializers, come in many different flavors.  And before I continue, there is a clear difference between a constructor and an initializer.  A constructor creates the object in memory (the Subaru factory plops out a new Outback) and the initializer sets its properties (the new Outback gets painted with optional features plugged in.)  Some languages do this at once, others do it separately.&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;// Java&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyObject&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;MyObject&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;param&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;param&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// JavaScript&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MyObject&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="nx"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;param&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;param&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;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Python
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Think of constructors like the assembly line at a Subaru factory.  You start it up by telling the robots what colors and features you want (okay, it probably doesn't actually work like that in the real world factories but bear with me) and the robots then, based on the class template they're given, build the car with your desired parameters.&lt;/p&gt;

&lt;p&gt;At the end of the line is a nice shiny Outback with all the customization you wanted, but it has enough Outback features that it is as much an Outback as any other Outback you encounter.  Even the old blocky ones.&lt;/p&gt;

&lt;h1&gt;
  
  
  Interfaces and Abstract Classes
&lt;/h1&gt;

&lt;p&gt;However, not every object needs to be constructed.  As I said before, the basic car idea is not a concrete object, but an abstract idea.  The basic car is just a list of features that every car &lt;em&gt;must&lt;/em&gt; implement.  If you don't implement these features, whatever your building is not a car.  Maybe it has wheels and an engine, but it is not a car.  If a semi comes out of the other end of your factory, it is not a car because it has too many wheels.  If an M1 Abrams Main Battle Tank comes out of the other end, it's not a car because it has treads, instead of wheels, and the DOD would like to know why you're building military tanks.&lt;/p&gt;

&lt;p&gt;An interface is a contract.  It guarantees that whatever comes out the other end will be a car.&lt;/p&gt;

&lt;p&gt;To drop the car analogy for a moment, imagine you have a function that takes an object as one of its parameters and within that function, it calls one of that object's methods.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;greet&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;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s says 'Hello'"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// assume we're in class main&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;personGreet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;personGreet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jim"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above example, we have a method that takes a &lt;code&gt;Person&lt;/code&gt; and calls its &lt;code&gt;greet&lt;/code&gt; method.  This works fine of course, but there's a problem.  What if I have multiple person types, and inheritance is a model doesn't work to my favor?  What if I have &lt;code&gt;Employee&lt;/code&gt; and &lt;code&gt;Dog&lt;/code&gt;, all of which might have a greeting, but maybe don't work as a subclass of Person?  &lt;code&gt;Employee&lt;/code&gt; might, but &lt;code&gt;Dog&lt;/code&gt; wouldn't!  Maybe the structure of my code is such that &lt;code&gt;Employee&lt;/code&gt; works out better with composition rather than inheritance.  We can ensure that both classes work in the &lt;code&gt;personGreet()&lt;/code&gt; method with an interface:&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;interface&lt;/span&gt; &lt;span class="nc"&gt;Greetable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Greetable&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;void&lt;/span&gt; &lt;span class="nf"&gt;greet&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;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
            &lt;span class="s"&gt;"%s says 'Hello', I work for %s as %s"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; 
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;job&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;jobTitle&lt;/span&gt;
        &lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Greetable&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;void&lt;/span&gt; &lt;span class="nf"&gt;greet&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;printf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="n"&gt;barks&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;Arf&lt;/span&gt; &lt;span class="nc"&gt;Arf&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// and back to the Main class&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;personGreet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Greetable&lt;/span&gt; &lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;greeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now our greet class can take any object that implements &lt;code&gt;Greetable&lt;/code&gt; because we can be confident that it implements the required methods.&lt;/p&gt;

&lt;p&gt;It's also incredibly helpful with any modern IDE.  If an IDE knows your variable implements Greetable, it will helpfully suggest methods in the Greetable interface, so you don't have to memorize how to spell them or in what order the method parameters might go.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some languages like JavaScript don't have static typing and for those languages interfaces are meaningless.  If I wanted to pass an object to the &lt;code&gt;personGreet()&lt;/code&gt; method, there's nothing to ensure that the &lt;code&gt;personGreet()&lt;/code&gt; has a &lt;code&gt;greet&lt;/code&gt; method, except for me to actually program such a check.  An interface is where statically typed languages shine.  If you're a fan of JavaScript but also want static types, try Typescript.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In drivers ed, you were not taught how to drive a specific car.  You were taught how to drive in a very generic fashion.  We all understand that cars implement a specific interface.  The steering wheel is on the left (unless your British), the accelerator is on the right and it makes it go.  Flick the blinker up to single a right turn, and the blinker down to signal a left turn.  Cars all implement the same interface.  If you can drive one car, you can drive them all.&lt;/p&gt;

&lt;p&gt;And any vehicles that require special licensing, they might implement the Car interface, but they also implement some other interfaces the user would need to be aware of.  A semi might implement some of the same details as a car.  A semi is operated in the same basic way after all, but would also implement some other interfaces, since it's size, weight, number of wheels would all affect its handling and make it a different object.  In fact, it might be better, to take some ideas out of car (like basic driving) and move it up into a Vehicle interface.&lt;/p&gt;

&lt;p&gt;Perhaps it would be better if car were an abstract class rather than an interface, that implements the Vehicle interface, but also has its own properties like &lt;code&gt;hasFourWheels&lt;/code&gt; and other things that are unique to cars, but not to vehicles.&lt;/p&gt;

&lt;p&gt;In Java you can see this progression from the Servlet interface to the GenericServlet abstract class to HttpServlet abstract class to your own implementation of Servlet that inherits from HttpServlet or GenericServlet.&lt;/p&gt;

&lt;h1&gt;
  
  
  Properties and Methods
&lt;/h1&gt;

&lt;p&gt;Let's go back to that road I opened this article up with.  We had all those cars driving by, including that god awful orange Subaru Crosstrek.  (Why are they always orange?)  While they all inherit from Vehicle and Car, they all look pretty unique.  People have a broad choice in shape, color, performance, radio, cabin controls and so on and so forth.  When you construct an object, it is a unique data structure stored in memory.  You can have any number of them (until memory runs out of course) and they might have the same basic shape, yet they're all unique.&lt;/p&gt;

&lt;p&gt;Now before I continue, a property is defined in different languages as different things.  In JavaScript, they are properties.  In Python and Ruby, they are attributes.  In Java, they are fields.  For clarity sake, when I say "property", it applies to all of these things.&lt;/p&gt;

&lt;p&gt;A property is simply some state applied to an individual object.  A car's color or its horsepower, for example.&lt;/p&gt;

&lt;p&gt;A method is some behavior an individual object might perform.  When you hit the gas in your car, you call the &lt;code&gt;accelerate()&lt;/code&gt; method.  And even though every object has the same &lt;code&gt;accelerate()&lt;/code&gt; method, not every car will accelerate at once!  Only the car calling its own &lt;code&gt;accelerate()&lt;/code&gt; will start to accelerate and increase its &lt;code&gt;speed&lt;/code&gt; property.  If two cars accelerate at the same time, they only modify their own internal state.  Me pressing the gas in my car, only makes my car go.&lt;/p&gt;

&lt;h1&gt;
  
  
  Static vs Member
&lt;/h1&gt;

&lt;p&gt;When people talk about static methods and properties, they often say something obscure like "static methods belong to the class, while member methods belong to an object."  Once you get a handle on OOP, that's not terribly difficult of a concept to grok.  But for someone brand new to programming, it might be a bit confusing.  After all, don't all methods and properties belong to the class?  A new programmer might not yet realize that a class and an object are not the same.  As I said earlier, a class is a schematic, an object is the concrete thing the schematic designs.&lt;/p&gt;

&lt;p&gt;Anything marked as "static" is something meant to apply to the class as a whole, something that wouldn't make sense to apply to an individual object, nor make sense to separate from the class.&lt;/p&gt;

&lt;p&gt;To explain it, I'll continue the car analogy.  Imagine you take your car on a road trip.  Because you're fabulously rich, you ferry your car across the Atlantic from the United States to Europe.  When you get there, you realize Europeans, who like to keep things simple, use the metric system.  Fortunately, your car came with a function that swaps the miles-per-gallon display to kilometers-per-liter.  Such a formula is useful for any car, but it won't change if you're driving a Subaru WRX or a Ford Fusion or a clunky Yugo held together by duct tape and desperation.  MPG to KPL is the same function no matter what you drive.  Even if you drive a gas guzzler, the unit conversion doesn't change.  So why make the method specific for individual cars?  Make it static so that all cars can share it.&lt;/p&gt;

&lt;p&gt;The MPG-to-KPL conversion doesn't belong in a car.  Certainly an individual might build in a method to display such a conversion in its own, but the &lt;em&gt;actual implementation&lt;/em&gt; of the mathematics doesn't belong in a car.  It instead can just be stored as common knowledge to all automakers and drivers out there.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="no"&gt;MPG_TO_KPL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.425144&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;double&lt;/span&gt; &lt;span class="nf"&gt;mpgToKpl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;mpg&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;mpg&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;MPG_TO_KPL&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Outback&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;displayMetricFuelConsumption&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;fuelConsumption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mpgToKpl&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;mpg&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;"Current fuel consumption in kpl: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fuelConsumption&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, the individual object itself can call its own method that displays the converted unit, but the unit conversion itself is static and kept at the class level.  Furthermore, &lt;code&gt;Outback&lt;/code&gt; wouldn't have to extend &lt;code&gt;Car&lt;/code&gt; for the static method to be available, any object can call it from the &lt;code&gt;Car&lt;/code&gt; class.&lt;/p&gt;

&lt;h1&gt;
  
  
  Inheritance
&lt;/h1&gt;

&lt;p&gt;At this point you should have a broad idea of what inheritance is.  To put it all in one place, the idea of inheritance is that subtype inherits the properties and methods of its parents.&lt;/p&gt;

&lt;p&gt;A station wagon is a Car, therefore it has four wheels and is meant to be used by the general population.  A Car in turn is a Vehicle and therefore it has a steering wheel, accelerator and brake pedals, some sort of piston engine, and so on and so forth.&lt;/p&gt;

&lt;p&gt;A Subaru Outback is a station wagon, therefore it is a car with a hatchback with extra space in the trunk.  And because it is a station wagon, it is therefore a Car, and because it is a Car, it is therefore a Vehicle.&lt;/p&gt;

&lt;h1&gt;
  
  
  Polymorphism
&lt;/h1&gt;

&lt;p&gt;But not every subtype will implement methods in the same way their parent does!  A Subaru WRX and a Subaru Outback certainly have different acceleration methods!  In any OOP language, children can override their parent's methods and properties.  This is done through overriding and overloading, though not every language will support overloading.  JavaScript, for example, does not.&lt;/p&gt;

&lt;p&gt;An override is when we change the implementation, but keep the method signature the same.  In the Subaru WRX, maybe within our acceleration method there's a turbocharge method, something you wouldn't typically find in an Outback.&lt;/p&gt;

&lt;p&gt;An overload is when the method signature is the same but the arguments and their &lt;em&gt;types&lt;/em&gt; are different.  You can override a method multiple times so long as you overload it with different arguments and different argument types.  But again, not every language supports this.  JavaScript does not.&lt;/p&gt;

&lt;p&gt;You can emulate overloading in JavaScript by using the &lt;code&gt;typeof&lt;/code&gt; or &lt;code&gt;instanceof&lt;/code&gt; keywords and programmatically determining what the arguments are and what the implementation does depending on what arguments are given, but you can't have multiple functions with the same name and different parameters.&lt;/p&gt;

&lt;p&gt;You can in Java:&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="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;b&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;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;concat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;add&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;a&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;b&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&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;Both methods have the same name, but different signatures.  Overloading is simply changing the implementation depending on what the inputs are.&lt;/p&gt;

&lt;h1&gt;
  
  
  Application Programming Interface
&lt;/h1&gt;

&lt;p&gt;Before I continue, I do want to take a brief moment to explain what an API (application programming interface) is.  An API is the collection of public facing properties and methods a user can use.&lt;/p&gt;

&lt;p&gt;Your car has an API.  There's a port for it to take in gas.  There's a pedal that makes it go and another that makes it stop.  There's a wheel that allows me to steer. These things allow you to interface with the car and change its state and behavior in a controlled and predictable way.&lt;/p&gt;

&lt;p&gt;The API of any object can be summed up as its public properties and methods that are meant to be used by the user.&lt;/p&gt;

&lt;h1&gt;
  
  
  Encapsulation
&lt;/h1&gt;

&lt;p&gt;When I drive down the road, when my foot is on the brake or the gas, it affects the internal state of my car.  I'm slowing down or speeding up.  It does not affect the other cars around me.  The internal state of my car is encapsulated.  As the user, I don't need to know what the engine is doing, and neither does anyone around me.  I just need to know what the public API is. When I interface with that API, I don't need to be aware of what is going on inside.  If I hit the gas, I don't need to know specifically which pistons must rise and which ones must lower at any given moment; the engine handles that for me.  And if I were allowed to go in and modify that while the engine is running, I could break something.&lt;/p&gt;

&lt;p&gt;This is encapsulation.  A class's internal state is only exposed to the outside world through specific methods explicitly designed for use by an outside user.  A singleton for example, uses encapsulation to ensure there's only one instance of itself.  The constructor is only accessible through a specific public method that ensures it can only be called once.  Typically, it checks if the instance is created and either creates the instance, or returns a reference to that instance, but the constructor itself, set to private, cannot be called from the outside.&lt;/p&gt;

&lt;h1&gt;
  
  
  Abstraction
&lt;/h1&gt;

&lt;p&gt;Now let's return to that driver's ed course again.  Remember that you were not taught to drive a specific car, but a generic idea &lt;em&gt;of&lt;/em&gt; a car.  We know that any car you do drive will inherit from Car and Vehicle, so we know it will have things like an accelerator, a brake pedal, a steering wheel, turn signals, and everything else in more or less the same place and the same configuration (but maybe on the other side for you British types!)&lt;/p&gt;

&lt;p&gt;It doesn't matter how those methods are implemented, but only that they are, and in a predictable manner. Abstraction is related to encapsulation, in that you don't know or care about the internals.  What you do know, is that if you call a method, predictable behavior should occur.  If I hit the gas, the car should move forward.  If I hit the gas and the blinkers come on, that . . . that would be weird.&lt;/p&gt;

&lt;p&gt;You know how to drive any car because you were given an abstract idea of what a car is and what its methods are.  You know every car will have these methods in a similar API you can use.  The actual implementation of the gas pedal or the turn signal or whatever is black boxed from you.  If you want to accelerate, you just hit the gas.  There's no need to understand how pressing that pedal causes gas to flow into your engine, which makes the pistons rise and fall and allows them to cycle through a system of intake, compression, combustion, and exhaust.  Sure, that might be handy for maintenance, but you can just as easily drive a car without any clue what that big loud metal thing under the hood is actually doing.&lt;/p&gt;

&lt;p&gt;Abstraction hides implementation details from where it is not necessary.  It allows you to take that implementation and bundle it into something more meaningful and reusable.  This reduces complexity on the user's end.  Take for example JavaScript's library of Array methods.&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;squares&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//[1, 4, 9]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;While JavaScript is an OOP language, it is based on a prototypal inheritance scheme rather than a classical one.  That's something to keep in mind.  With that said, most of what I've stated here applies to JavaScript as well, but perhaps not exactly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Do I as the user care what &lt;code&gt;map()&lt;/code&gt; does under the hood?  No.  All I care is the knowledge that it performs some function on every element.  I don't care &lt;em&gt;how&lt;/em&gt;.  I don't want to implement a mapping function every time I want to change the elements of an array.  Why not abstract it away into a method so I can reuse it as necessary?&lt;/p&gt;

&lt;p&gt;That is abstraction.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Object Oriented Programming can be a bit confusing but I find it is best understood if you think about objects as literal everyday objects.  Cars are mass produced objects that bear a similar basic shape even though there's an incredibly wide variety of types out there, from hulking SUVs to station wagons, to pick up trucks, to sedans and sportscars.  If you think about how cars relate to each other in the real world, you can have a better understanding on how objects work in the programming world.&lt;/p&gt;

&lt;p&gt;I hope this helped create a better understanding of what OOP is and what the basic concepts and terms involved in OOP are.  I did not intend to really go into actual language implementation of OOP, but hopefully when you read about how different languages implement OOP, having some sort of analogy will help make the concepts clearer.&lt;/p&gt;

</description>
      <category>oop</category>
    </item>
    <item>
      <title>A Case Against Switches</title>
      <dc:creator>Jonathan Kuhl</dc:creator>
      <pubDate>Sun, 11 Nov 2018 01:31:22 +0000</pubDate>
      <link>https://dev.to/jckuhl/a-case-against-switches-13pd</link>
      <guid>https://dev.to/jckuhl/a-case-against-switches-13pd</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Switches are ugly.  They are bug prone.  The default fallthrough behavior is begging for errors.  I think Swift did the right thing and made &lt;code&gt;fallthrough&lt;/code&gt; a keyword, rather than a default, but even then, I'd rather avoid them if I can.  They just don't fit in with the rest of my code.  The indentation is awkward and no one can seem to decide if the case statements are indented or not.&lt;/p&gt;

&lt;p&gt;Python didn't even bother to implement them in the language.&lt;/p&gt;

&lt;p&gt;I primarily work in JavaScript, so I'll be focusing on that language.  However, any language with first class functions and some sort of key/value pair structure can avoid switches.  Java, for example, can use maps and lambdas.  But I'll be sticking with JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  The JavaScript Object
&lt;/h2&gt;

&lt;p&gt;How do we avoid switches?  Well, each case in a switch is essentially a key-value pair.  You're matching a single key, the case, with a single value, an expression to be evaluated, or a set of instructions.  Sound familiar?  Boil it down to two key works, &lt;em&gt;key&lt;/em&gt; and &lt;em&gt;value&lt;/em&gt; and you have a basic JavaScript object.  How do we use a JavaScript object in place of a switch?&lt;/p&gt;

&lt;p&gt;Well let's start with an example.  Let's pretend we have some code that displays an error message when a log in fails.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;status&lt;/span&gt; &lt;span class="o"&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;401&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;Please check your username and password&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;404&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;Account not found, have you registered?&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;500&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;Something went wrong with the server, please try again later&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;Failed to fetch&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;Servers are currently down, please try again later&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&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;Here we have code that behaves like a switch.  We have 4 different error messages that can get thrown, a 401 if verification fails, a 404 if the user isn't found, a 500 if something broke, or a &lt;code&gt;Failed to fetch&lt;/code&gt; if the server is down.  All we have to do is a very basic look up on an object and that's it.  No fall through, no jarring &lt;code&gt;switch&lt;/code&gt; structure.  Just a basic JavaScript object.&lt;/p&gt;

&lt;p&gt;But what if I wanted a default case?  Well that's simple too, we just need to check if the value is in the object itself.  There are a number of ways to do this, but I'll just check if the property exists by checking for &lt;code&gt;undefined&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;status&lt;/span&gt; &lt;span class="o"&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;401&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;Please check your username and password&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;404&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;Account not found, have you registered?&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;500&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;Something went wrong with the server, please try again later&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;Failed to fetch&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;Servers are currently down, please try again later&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Something borked, sorry!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&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="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;default&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;return&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&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;Current JavaScript is also fairly flexible.  If I wanted to use numbers rather than strings for object keys, I can do that.  JavaScript will, under the hood, turn them into strings.  Therefore the following is also valid JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;two&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;three&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// 'one'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, you can't use dot notation on this object, &lt;code&gt;object.1&lt;/code&gt; is invalid, but if we're simply using this object as a switch, then it doesn't matter.  With bracket notation, dot notation isn't mandatory anyways.  But what's important here is that we can recreate switch behavior with both strings and numbers.  Now you &lt;em&gt;could&lt;/em&gt; use &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt; as strings for keys if you wanted to make a boolean, but I argue a switch is overkill for a boolean anyways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions?
&lt;/h2&gt;

&lt;p&gt;However, when we're using &lt;code&gt;switch&lt;/code&gt;, we're often doing more than grabbing strings and numbers, we might also be holding functions.  Thankfully, JavaScript is a language that treats functions as first class citizens.  Functions can be passed around like any other object, and of course, can be the values of properties in our objects.&lt;/p&gt;

&lt;p&gt;Here arrow functions truly shine, though if you need to preserve &lt;code&gt;this&lt;/code&gt;, you'll have to reach for &lt;code&gt;Function.prototype.bind()&lt;/code&gt;, or use the old school syntax for JavaScript anonymous functions,&lt;code&gt;function () { ...&lt;/code&gt;.  Function shorthand in JavaScript objects also preserve the context of &lt;code&gt;this&lt;/code&gt; and in that case, the name of the function, becomes the key, and the instruction block becomes its value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;sayHello1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nf"&gt;sayHello2&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;sayHello3&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&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;In this example, &lt;code&gt;greet.sayHello1()&lt;/code&gt; and &lt;code&gt;greet.sayHello2()&lt;/code&gt; do exactly the same thing.  &lt;code&gt;greet.sayHello3()&lt;/code&gt; is slightly different because it is an arrow function and therefore the &lt;code&gt;this&lt;/code&gt; keyword is lost.  However since the function isn't using &lt;code&gt;this&lt;/code&gt;, all three are exactly the same in this particular scenario.  If you needed &lt;code&gt;this&lt;/code&gt; for an arrow function, you can do &lt;code&gt;greet.sayHello3.bind(greet)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Imagine we have a text based RPG.  You play a wizard who has a number of spells he can cast.  The user types in the spell he wants and the wizard casts it.  You could use a switch to determine which spell to cast, or use an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;castSpell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;spellname&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;spellbook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;fireball&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wizard casts Fireball!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;iceshard&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wizard casts Ice Shard!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;arcanemissiles&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wizard casts Arcane Missiles&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;polymorph&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wizard casts Polymorph!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;default&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wizard doesn&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s1"&gt;t know that spell.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;spellbook&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;spellname&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="nx"&gt;spellbook&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;default&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;return&lt;/span&gt; &lt;span class="nx"&gt;spellbook&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;spellname&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;So what the function does is, you pass in a spell name, and it uses the spellname to match a value in the spellbook.  That value is a function, so by using &lt;code&gt;()&lt;/code&gt; after grabbing the value will call that function.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm using bracket notation for the default method here for consistency.  &lt;code&gt;spellbook.default()&lt;/code&gt; would also be valid.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we can call functions the same way we would in a switch.  You can abstract away all the code that would be your case statements and shove them in an object methods and simply call them via bracket notation.&lt;/p&gt;

&lt;p&gt;This does have some trade offs, as it's harder to tell what &lt;code&gt;spellbook[spellname]()&lt;/code&gt; is doing than &lt;code&gt;case 'fireball': return fireball();&lt;/code&gt; but the code is more elegant, there are fewer levels of indentation, and no threat of fallthrough.&lt;/p&gt;

&lt;h2&gt;
  
  
  But I Want Fallthrough!
&lt;/h2&gt;

&lt;p&gt;Oh.  Well then.  Obtaining fallthrough behavior in an object is more difficult and there's no one way to do it.  There could be an argument here where &lt;code&gt;switch&lt;/code&gt; might actually be a better construct to use.  And if so, then use &lt;code&gt;switch&lt;/code&gt;.  But understanding that objects have a number methods on them, there are other solutions as well.  With &lt;code&gt;Object.values()&lt;/code&gt;, &lt;code&gt;Object.keys()&lt;/code&gt; and &lt;code&gt;Object.entries()&lt;/code&gt;, you can get all of your key/value pairs into arrays and then run them through any number of array functions.  This can be done to achieve fallthrough.&lt;/p&gt;

&lt;p&gt;Imagine we have an object with a bunch of functions and, given a number, we need to call all the functions upto, and not including that number.  This is one case where a switch fallback is useful, but this is also easily done with an object.  Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;callFns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;two&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;three&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;four&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;five&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="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Call &lt;code&gt;callFns(3)&lt;/code&gt; and it will log &lt;code&gt;'three'&lt;/code&gt;, &lt;code&gt;'four'&lt;/code&gt;, and &lt;code&gt;'five'&lt;/code&gt; to the console.  This would simulate using &lt;code&gt;switch(3) {&lt;/code&gt; with no &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;return&lt;/code&gt; in any cases.  By combining Object and Array methods, we can simulate a fallthrough for our situation.  But again, this may be a case where a &lt;code&gt;switch&lt;/code&gt; might be the better construct.  After all, the primary cause of bugs in a switch &lt;em&gt;is&lt;/em&gt; the fallthrough feature.  However, by using an Object, you get access to a number of methods that can make an object more flexible than a switch statement.  By getting an array of your object's entries, you get access to &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;some&lt;/code&gt;, &lt;code&gt;every&lt;/code&gt;, as well as iteration methods like &lt;code&gt;map&lt;/code&gt; and &lt;code&gt;forEach&lt;/code&gt;, and structures like &lt;code&gt;for of&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;Objects in JavaScript give you a simple alternative to the &lt;code&gt;switch&lt;/code&gt; statement.  Objects are flexible and less bug prone than &lt;code&gt;switch&lt;/code&gt; statements and they aren't as jarring in your code as switch statements.  If you don't want fallthrough, using an object in place of a switch is a better option.  If you do want fallthrough, it can be achieved through Object and Array methods, but a regular &lt;code&gt;switch&lt;/code&gt; might be a better option.&lt;/p&gt;

&lt;p&gt;All in all, your style of code is up to you, but I suggest being like Python and throwing the switch out entirely.&lt;/p&gt;

&lt;p&gt;Happy Coding.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
