<?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: W. Brian Gourlie</title>
    <description>The latest articles on DEV Community by W. Brian Gourlie (@dubyabrian).</description>
    <link>https://dev.to/dubyabrian</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%2F562%2FSvRDPg_h.jpg</url>
      <title>DEV Community: W. Brian Gourlie</title>
      <link>https://dev.to/dubyabrian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dubyabrian"/>
    <language>en</language>
    <item>
      <title>Mutability in Rust, and how it differs from object-oriented languages</title>
      <dc:creator>W. Brian Gourlie</dc:creator>
      <pubDate>Tue, 04 Feb 2020 00:26:15 +0000</pubDate>
      <link>https://dev.to/dubyabrian/mutability-in-rust-and-how-it-differs-from-object-oriented-languages-32e</link>
      <guid>https://dev.to/dubyabrian/mutability-in-rust-and-how-it-differs-from-object-oriented-languages-32e</guid>
      <description>&lt;p&gt;This post started out as a long-winded answer to a stackoverflow question. The question made me realize that there's a pretty universal way of thinking about mutability when coming from popular languages like C# and Java, and that coming to rust with this mindset often results in confusion and frustration.&lt;/p&gt;

&lt;p&gt;I’ll be comparing and contrasting solutions to common problems involving mutability in a way that (hopefully) expresses the differences intuitively. For the non-rust examples, I'll be using Java, but these examples can be expressed similarly in other object-oriented languages.&lt;/p&gt;

&lt;p&gt;We'll start with a simple goal—Define a &lt;code&gt;Dog&lt;/code&gt; type with two fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name, which will be immutable&lt;/li&gt;
&lt;li&gt;Age, which will be mutable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the first few examples, we'll avoid common idioms like getters and setters in order to keep things simple. &lt;/p&gt;

&lt;p&gt;Here's how we'll define our type in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&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;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Dog&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An equivalent type in rust would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Dog&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;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the rust example, you may notice that there's no modifier on our &lt;code&gt;name&lt;/code&gt; field to suggest that we're controlling mutability in any way. This is because &lt;em&gt;rust has no concept of mutability at the field level.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Rust takes a fundamentally different approach. If we want to modify any field on our type, we need to declare a mutable variable pointing to it. By default, all variables are immutable and you must add the &lt;code&gt;mut&lt;/code&gt; modifier if you intend to mutate it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// Declare an immutable variable&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;harris&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Harris"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;harris&lt;/span&gt;&lt;span class="py"&gt;.age&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="c"&gt;// Compile error!&lt;/span&gt;

&lt;span class="c"&gt;// Declare a mutable variable&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;harris&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Harris"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;harris&lt;/span&gt;&lt;span class="py"&gt;.age&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="c"&gt;// No problem!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, it would appear as if Java provides finer-grained control over mutability than rust—If a variable's mutable, we can mutate all of its fields; if it's immutable, we can't mutate any of them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Mutability and &lt;code&gt;self&lt;/code&gt; parameters
&lt;/h2&gt;

&lt;p&gt;Let's update our examples to make them more idiomatic. We'll make our fields private and expose methods to access or modify them.&lt;/p&gt;

&lt;p&gt;First in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Dog&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="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="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="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getAge&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;age&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;incrementAge&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;age&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="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;And our rust example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Dog&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;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&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;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt;'static&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;increment_age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.age&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the rust example, the &lt;code&gt;get_name&lt;/code&gt;, &lt;code&gt;get_age&lt;/code&gt; and &lt;code&gt;increment_age&lt;/code&gt; methods all include a &lt;code&gt;self&lt;/code&gt; parameter. Like variables, parameters are either mutable or immutable. &lt;code&gt;self&lt;/code&gt; differs from other parameters in how it's passed to the method: If we have a variable named &lt;code&gt;dog&lt;/code&gt; and we call &lt;code&gt;dog.increment_age()&lt;/code&gt;, the &lt;code&gt;dog&lt;/code&gt; variable is implicitly passed to the &lt;code&gt;increment_age&lt;/code&gt; method as the &lt;code&gt;self&lt;/code&gt; parameter, and can access private fields declared on the type.&lt;/p&gt;

&lt;p&gt;This is even more interesting when we consider how this works in conjunction with variables. If we have an immutable variable, and we call a method on it that takes a &lt;code&gt;&amp;amp;mut self&lt;/code&gt;, we'll encounter a compile error because the method &lt;em&gt;requires&lt;/em&gt; mutable access to our value!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c"&gt;// Declare an immutable dog variable&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Harris"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="nf"&gt;.increment_age&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c"&gt;// Compile error!&lt;/span&gt;

&lt;span class="c"&gt;// Declare a mutable dog variable&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Harris"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="nf"&gt;.increment_age&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c"&gt;// This works!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what happens if we declare a variable as mutable, and call a method that takes an immutable &lt;code&gt;self&lt;/code&gt; parameter? In this case, the call is valid, but the method is still only granted immutable access to the &lt;code&gt;self&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;To reiterate: In order to call a method that requires mutable access to &lt;code&gt;self&lt;/code&gt;, the variable &lt;em&gt;must&lt;/em&gt; be declared as mutable, but methods that declare an immutable &lt;code&gt;self&lt;/code&gt; parameter can be called via mutable and immutable variables alike.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encoding mutability in return types
&lt;/h2&gt;

&lt;p&gt;We're going to introduce a new field to illustrate how mutability works with more complex types. We'll add a &lt;code&gt;List&lt;/code&gt; (&lt;code&gt;Vec&lt;/code&gt; in rust) to our example named &lt;code&gt;friends&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;friends&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArrayList&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;();&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getFriends&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;friends&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;addFriend&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="n"&gt;friend&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;friends&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;friend&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;In the java example, our new &lt;code&gt;friends&lt;/code&gt; field is declared &lt;code&gt;final&lt;/code&gt;, which means we can never change what the field points to, &lt;em&gt;however&lt;/em&gt;, we're still free to add and remove items from the list via our getter method as seen below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;   &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="n"&gt;harris&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;Dog&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Harris"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="n"&gt;buck&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;Dog&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Buck"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
   &lt;span class="n"&gt;harris&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getFriends&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buck&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So how would this work in rust? Let's update our example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
    &lt;span class="n"&gt;friends&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;&amp;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;impl&lt;/span&gt; &lt;span class="n"&gt;Dog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_friends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Dog&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;return&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.friends&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;In our rust example, we're actually encoding mutability into the return type of our &lt;code&gt;get_friends&lt;/code&gt; method! The &lt;code&gt;&amp;amp;Vec&amp;lt;Dog&amp;gt;&lt;/code&gt; return type represents an immutable reference because it lacks the &lt;code&gt;mut&lt;/code&gt; keyword. It would be a compile error if we attempted to add an item to the returned &lt;code&gt;friends&lt;/code&gt; list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;harris&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Harris"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;buck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Dog&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Buck"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;harris&lt;/span&gt;&lt;span class="nf"&gt;.get_friends&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buck&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c"&gt;// Compile error!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break this down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We declared a mutable &lt;code&gt;harris&lt;/code&gt; variable &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;get_friends&lt;/code&gt; method returns an &lt;em&gt;immutable&lt;/em&gt; reference to our &lt;code&gt;friends&lt;/code&gt; field&lt;/li&gt;
&lt;li&gt;The &lt;a href="https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push"&gt;Vec&lt;/a&gt; type has a method named &lt;code&gt;push&lt;/code&gt; which requires a mutable reference to &lt;code&gt;self&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Our code fails to compile because the immutable reference returned by &lt;code&gt;get_friends&lt;/code&gt; cannot be used to call a method that requires mutable access to &lt;code&gt;self&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, even though our original variable was declared as mutable, we use the &lt;code&gt;get_friends&lt;/code&gt; method to hide access to our list behind an immutable reference. Consequently, it's a compile error if we attempt to mutate it.&lt;/p&gt;

&lt;p&gt;As an exercise, let's say we wanted the &lt;code&gt;get_friends&lt;/code&gt; method to provide mutable access to the the returned &lt;code&gt;friends&lt;/code&gt; reference. Take a moment to consider what we'd need to change.&lt;/p&gt;

&lt;p&gt;Have an idea? Here's the answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_friends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Dog&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;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="py"&gt;.friends&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To summarize, we had to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declare our &lt;code&gt;self&lt;/code&gt; parameter as mutable,&lt;/li&gt;
&lt;li&gt;Declare the return type as mutable, and&lt;/li&gt;
&lt;li&gt;Return a mutable reference to our list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can now modify the &lt;code&gt;friends&lt;/code&gt; list via our &lt;code&gt;get_friends&lt;/code&gt; method! Whether or not we'd ever want to do this in practice is another question.&lt;/p&gt;

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

&lt;p&gt;Mutability in rust is very different from what most people are used to, and it can take some time for it to sink in. Once it does, however, it becomes an incredibly powerful way to enforce guarantees that aren't possible in most languages. It's also a fundamental part of the language, working in concert with other language features to achieve a unique combination of safety and performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  A quick note about rust idioms
&lt;/h3&gt;

&lt;p&gt;The rust examples aren't particularly idiomatic, but that was done for the sake of clarity and simplicity. Perhaps I'll touch on interesting rust idioms in a separate post!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>java</category>
      <category>mutability</category>
    </item>
    <item>
      <title>NPM hot-takes and the pitfalls of trivialization</title>
      <dc:creator>W. Brian Gourlie</dc:creator>
      <pubDate>Tue, 17 Apr 2018 05:27:08 +0000</pubDate>
      <link>https://dev.to/dubyabrian/npm-hot-takes-and-the-pitfalls-of-trivialization-3n9l</link>
      <guid>https://dev.to/dubyabrian/npm-hot-takes-and-the-pitfalls-of-trivialization-3n9l</guid>
      <description>&lt;h2&gt;
  
  
  The infamous left-pad controversy
&lt;/h2&gt;

&lt;p&gt;Most people are probably aware of the &lt;a href="http://blog.npmjs.org/post/141577284765/kik-left-pad-and-npm"&gt;left-pad controversy&lt;/a&gt; by now. If not, I'll get you up to speed:&lt;/p&gt;

&lt;p&gt;The maintainer of a popular NPM package deleted their package in protest, breaking thousands of other packages that had depended on it. The problem was quickly remedied and solutions put in place to prevent it from happening again, however, following the controversy was a deluge of blog posts and articles asking why we're dependent on tiny libraries that solve such trivial problems. &lt;a href="http://www.haneycodes.net/npm-left-pad-have-we-forgotten-how-to-program/"&gt;"Have we forgot how to program?"&lt;/a&gt; one blog post asked, illustrating the general sentiment at the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Left-pad redux
&lt;/h2&gt;

&lt;p&gt;The left-pad controversy still comes up from time-to-time, usually as the butt of jokes. I recently ran across a Medium article pointing out the absurdity of the &lt;a href="https://www.npmjs.com/package/is-odd"&gt;is-odd&lt;/a&gt; package, which as of this writing has over &lt;em&gt;3 million downloads over the past seven days&lt;/em&gt;. Indeed, on the surface, relying on a package to determine if a number is odd appears to be the epitome of overkill. One of the most highly clapped comments made the following claim:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you can’t write a function to determine if a given integral input is odd in under 10 seconds, you’re either a crappy typist or you shouldn’t be programming.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's a pretty bold claim, and needlessly insulting to boot. Is it &lt;em&gt;really&lt;/em&gt; that simple? The typical 10 second solution would probably look something like this:&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="nx"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&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;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&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;Let's write some unit tests just for the hell of it:&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;(&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;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;(&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;toBe&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isOdd&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="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isOdd&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="nx"&gt;toBe&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;(&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;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom! Done. Let's call it a day.&lt;/p&gt;

&lt;h2&gt;
  
  
  Just kidding. We're not done yet.
&lt;/h2&gt;

&lt;p&gt;When we initially considered this problem, we kind of assumed that all inputs would be integers. However, there's no way to enforce that a particular type be passed to the function. Not only that, but all numbers in javascript are actually double precision floats. Let's add a few unit tests to make sure our function works with floating point inputs:&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isOdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// FAIL!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, so our function doesn't work with floating point numbers. There are a couple options at this point:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I don't intend to pass anything but whole numbers to the function, so I don't care if it returns the wrong result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I'll just return true if it's a not a whole number.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I'll throw an exception if it's not a whole number.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these solutions is necessarily &lt;em&gt;wrong&lt;/em&gt;, since the best solution could involve any number of considerations. Either way, &lt;em&gt;it still warrants consideration&lt;/em&gt;. Not quite the 10 second solution we initially thought it was.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok we're done. The end.
&lt;/h2&gt;

&lt;p&gt;Just kidding, there's one last thing we need to consider here: Javascript is dynamically-typed and we need to determine how to handle non-numeric inputs. Again, we're faced with a few different solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I don't intend on passing non-numeric inputs to the function, so I don't care what happens if I do.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I'll just let javascript do whatever wacky implicit conversion it wants and accept the result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I'll throw an exception.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Again, none of these is necessarily wrong, but it's yet another consideration we didn't initially think of. &lt;strong&gt;Trivial libraries handle these considerations so we don't have to.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Behavior is not trivial
&lt;/h2&gt;

&lt;p&gt;Trivial libraries do more than just solve trivial problems. They consider all the little edge cases that we're likely to overlook and provide consistent and robust behavior. This is especially important with languages like javascript.&lt;/p&gt;

&lt;p&gt;As a general rule of thumb, I try not to trivialize things that look trivial on the surface. Programming, like the real world, is non-trivial and full of nuance.&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>I recently discovered Parcel, and it's amazing</title>
      <dc:creator>W. Brian Gourlie</dc:creator>
      <pubDate>Mon, 09 Apr 2018 04:46:22 +0000</pubDate>
      <link>https://dev.to/dubyabrian/i-recently-discovered-parcel-and-its-amazing-37h3</link>
      <guid>https://dev.to/dubyabrian/i-recently-discovered-parcel-and-its-amazing-37h3</guid>
      <description>&lt;p&gt;While playing around with WebAssembly and rust, I stumbled across &lt;a href="https://github.com/koute/parcel-plugin-cargo-web"&gt;a plugin&lt;/a&gt; for an asset bundler I hadn't heard of before. Admittedly, my initial thought was "Dammit, why couldn't they just use webpack?" After all, webpack has served all my needs in the past and has seemingly become the defacto standard. "Time to roll up my sleeves and learn how to configure this thing..." I thought.&lt;/p&gt;

&lt;p&gt;The first thing that struck me about &lt;a href="https://github.com/parcel-bundler/parcel"&gt;Parcel&lt;/a&gt; is that there &lt;em&gt;is&lt;/em&gt; no configuration file. In theory, I thought, that's great, but inevitably I'll need fine-grained control over &lt;em&gt;something.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Narrator: "He has yet to encounter such a scenario.")&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Indeed, I've only used Parcel for two personal projects thus far, but I haven't had to configure anything at all. Everything Just Works™, and I've used it to do some relatively exotic things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compiling rust to WebAssembly and bundling the resulting artifact&lt;/li&gt;
&lt;li&gt;Bundling and exposing markdown files as React components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And then some not-so-exotic things, which are all supported out-of-the-box: Javascript, TypeScript, CSS, HTML, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  More than just zero configuration
&lt;/h3&gt;

&lt;p&gt;Zero configuration is a huge selling point, but it's not the only thing Parcel has going for it. Whereas bundlers like webpack transform javascript files exclusively, Parcel can transform anything (correction: webpack 4 can now transform other types of files). Any file type not supported out-of-the-box can be supported by way of plugin. The practical implication here is that you can use any type of file as your entry point, not just javascript. This allows for a more natural way of bundling assets. For example, I can specify an HTML file as my entry point, and it will recursively locate and bundle all assets from there.&lt;/p&gt;

&lt;p&gt;Parcel accommodates this by operating at a fundamentally higher level than its counterparts–It actually &lt;em&gt;parses&lt;/em&gt; supported file types and transforms the resulting abstract syntax tree. When it encounters a javascript file, it transforms &lt;code&gt;import&lt;/code&gt; and &lt;code&gt;require&lt;/code&gt; declarations. When it encounters HTML, it will transform &lt;code&gt;script&lt;/code&gt; and &lt;code&gt;link&lt;/code&gt; tags. When it encounters CSS files, it transforms &lt;code&gt;import&lt;/code&gt; and &lt;code&gt;url&lt;/code&gt; declarations.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Oh and it's f***ing fast
&lt;/h2&gt;

&lt;p&gt;According to the README on Parcel's github page, it's twice as fast as webpack when not caching assets, and nearly an order-of-magnitude faster when caching assets. It caches assets by default, and I have yet to encounter a situation where I've needed to turn off caching. Anecdotally, the numbers check out.&lt;/p&gt;

&lt;p&gt;So, next time you're in need of an asset bundler, you should seriously consider Parcel.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>NodeJS leadership is a toxic mess</title>
      <dc:creator>W. Brian Gourlie</dc:creator>
      <pubDate>Wed, 27 Sep 2017 00:11:21 +0000</pubDate>
      <link>https://dev.to/dubyabrian/nodejs-leadership-is-a-toxic-mess-dl6</link>
      <guid>https://dev.to/dubyabrian/nodejs-leadership-is-a-toxic-mess-dl6</guid>
      <description>&lt;h3&gt;
  
  
  Preface
&lt;/h3&gt;

&lt;p&gt;In this article, I’m going to be calling out toxic behavior exhibited by certain high-profile members of the NodeJS community. As is the nature of such things, some people will use this as justification to harass them. I want to be clear: Under no circumstances should anyone respond in any sort of harassing or threatening manner to the people called out in this article (or anyone, really). My goal in writing this is to improve the tech community as a whole, for everyoneâ€Š–â€ŠNot to incite anger and hate.&lt;/p&gt;

&lt;p&gt;Also, since this article touches on issues of diversity in tech, I want to make clear that I absolutely support diversity initiatives. This article is specifically highlighting toxic behavior that is being excused and justified because it is, supposedly, done in the interest of diversity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inclusion by exclusion
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“If you are not a white dude and have things to say about JavaScript testing, please submit a talk to [redacted]”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above quote is from the NPM CEO, who, for context, is a white male who planned to speak at the conference he was promoting (the conference organizer reached out to tell me his talk was never formally accepted).&lt;/p&gt;

&lt;p&gt;This is only a single example, but his twitter feed is filled with equally exclusive and adversarial messaging. His response to any criticism follows a general theme among prominent members of the NodeJS community wherein they hoist themselves into a position of moral authority by asserting that any criticism is a result of people simply not supporting diversity measures. For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“You’re sarcastically whinging at a stranger because you fear losing your unearned privilege. I’m bored now.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A discussion quickly followed on &lt;a href="https://www.reddit.com/r/node/comments/71snxa/npm_ceo_if_you_are_not_a_white_dude_and_have/"&gt;/r/node&lt;/a&gt; where people voiced displeasure at the messaging put forth. Among the responses were members of underrepresented groups talking about how the CEO’s messaging made them uncomfortable and didn’t feel like it was helping their cause. In response, the NPM CEO dismissed the node subreddit entirely:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“/r/node is not part of the node community. It’s a place for GamerGate/KiA/TRP to launch attacks on the @nodejs community.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s worth noting that, overwhelmingly, the discussions that took place on reddit were not anti-diversity, but rather criticizing this individual’s toxic behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  A pattern of behavior
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;“In fact, if you were a white dude and you wanted to talk at the conference, your chances were basically nil.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This quote comes from a member of the Node foundation board of directors (who, for context, is also white). This quote in particular was among many that were part of a code-of-conduct report (&lt;a href="https://www.reddit.com/r/node/comments/6whs2e/multiple_coc_violations_by_nodejs_board_member/"&gt;detailed here&lt;/a&gt;) filed against the quoted individual. In response to the code-of-conduct report and ensuing discussion, another individual (and NPM employee) is on record as saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“reminder to reddit manbabies that they are kinda pathetic, irrelevant, boring, creepy nerds who’ll never amount to anything. The only thing they seem to know how to do is complain, masturbate about upvotes and karma, and be creepy stalkers when women are involved. Like holy shit, do these weirdos even realize what they’re doing? Y’all are literally the sketchy, sweaty nerds that make us wanna vomit. But keep doing what you’re doing, because I’d rather that y’all vomit-inducing rapey gross dweebs actually keep being open about who you are”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This resulted in another &lt;a href="https://github.com/nodejs/TSC/issues/325"&gt;code-of-conduct report&lt;/a&gt; that was summarily dismissed as “random trolling not submitted in good faith. It’s worth pointing out the following excerpt from the NodeJS code-of-conduct:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the case of the NPM code-of-conduct, which contains similar language, there’s a clause stating “&lt;em&gt;The definitions of various subjective terms such as â€˜discriminatory’, â€˜hateful’, or â€˜confusing’ will be decided at the sole discretion of the npm abuse team.&lt;/em&gt; In other words, there’s no objective means of enforcing the code-of-conduct, and if there were, many high-profile members of the Node community would be in violation of it based on any reasonable interpretation.&lt;/p&gt;

&lt;h3&gt;
  
  
  My personal take
&lt;/h3&gt;

&lt;p&gt;A concerning aspect of the messaging put forth by high-profile members of the Node community is that it overwhelmingly comes from members of majority groups (white, often male). At best, it’s a highly misguided attempt to do the right thing. At worst, it’s exploiting a good cause for the purpose of self-promotion while actively hurting the case for diversity and inclusion.&lt;/p&gt;

&lt;p&gt;Promoting diversity through exclusionary and adversarial messaging does not help to foster a safe, welcoming environment for underrepresented groups. Furthermore, I legitimately believe it will result in brain-drain from communities where this behavior is deemed acceptableâ€Š–â€ŠNot because white men will feel excluded, but because good people of all backgrounds will not want to be associated with the rhetoric put forth by community leaders.&lt;/p&gt;

</description>
      <category>node</category>
      <category>npm</category>
      <category>diversity</category>
    </item>
    <item>
      <title>Cross-platform development on Windows is suddenly awesome</title>
      <dc:creator>W. Brian Gourlie</dc:creator>
      <pubDate>Mon, 07 Nov 2016 03:42:04 +0000</pubDate>
      <link>https://dev.to/dubyabrian/cross-platform-development-on-windows-is-suddenly-awesome</link>
      <guid>https://dev.to/dubyabrian/cross-platform-development-on-windows-is-suddenly-awesome</guid>
      <description>&lt;p&gt;Anyone who's done cross-platform development on Windows knows that getting things to compile can be a huge pain in the ass. Sometimes it's not even possible. The whole situation kinda sucked, until now.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter Linux on Windows
&lt;/h2&gt;

&lt;p&gt;There's this new thing called the &lt;a href="https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux"&gt;Windows Subsystem for Linux&lt;/a&gt;. Without going into too much detail, WSL provides a bash shell that exposes a Linux environment. It's native Linux, too — These are ELF binaries running inside Windows. The Windows file-system is fully accessible, with each drive having its own mount point. Linux processes can bind to the Windows loopback address and it all just works. You can read the technical details &lt;a href="https://blogs.msdn.microsoft.com/wsl/2016/10/19/windows-and-ubuntu-interoperability/"&gt;here&lt;/a&gt;, but the practical implications are simple enough: Windows is finally a good OS for doing cross-platform development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IuUmDKxU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d262ilb51hltx0.cloudfront.net/max/1600/1%2Ax5Qn3W34ZHqe0VTlng3kEg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IuUmDKxU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d262ilb51hltx0.cloudfront.net/max/1600/1%2Ax5Qn3W34ZHqe0VTlng3kEg.png" alt="A native Linux environment running inside Windows."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;WSL isn't installed by default, so you'll need to follow &lt;a href="https://msdn.microsoft.com/en-us/commandline/wsl/install_guide"&gt;these instructions&lt;/a&gt; to get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical example
&lt;/h2&gt;

&lt;p&gt;I do a lot of Rust development in my personal time. Rust actually has excellent Windows support, however, there are packages that link against C libraries that aren't well supported on Windows. One of my projects depends transitively on OpenSSL, for example. Trying to compile on Windows results in this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lZqP5XZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d262ilb51hltx0.cloudfront.net/max/2000/1%2AS54oqozA4mHXB8z4DauBow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lZqP5XZb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d262ilb51hltx0.cloudfront.net/max/2000/1%2AS54oqozA4mHXB8z4DauBow.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While most of these issues can be resolved with enough persistence, it's a huge waste of time and effort. My typical reaction is to ditch my beefy desktop and its multiple monitors for a small, relatively under-powered MacBook where things just work™. Not anymore!&lt;/p&gt;

&lt;p&gt;To get started, I needed to install the Linux Rust toolchain. Something I haven't mentioned to this point is that the WSL uses Ubuntu, meaning it uses &lt;a href="https://en.wikipedia.org/wiki/Advanced_Packaging_Tool"&gt;apt&lt;/a&gt; for package management. Installing things from the bash shell inside Windows is no different than doing it on any other Ubuntu installation:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brian@DESKTOP-O4RQ37I:~$ sudo apt-get install build-essential
...
brian@DESKTOP-O4RQ37I:~$ curl https://sh.rustup.rs -sSf | sh
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;First I install the &lt;code&gt;build-essential&lt;/code&gt; package which installs gcc (among other things). Then I install rustup, Rust's toolchain manager, via its handy shell script. That's it — I navigate to the same code base that failed to compile within the standard prompt and compile it from the bash shell instead:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sz24nezi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d262ilb51hltx0.cloudfront.net/max/1600/1%2AT3gmdGd6d9k5QPL0Iu9YBQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sz24nezi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://d262ilb51hltx0.cloudfront.net/max/1600/1%2AT3gmdGd6d9k5QPL0Iu9YBQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was even able to configure IntelliJ IDEA to launch the bash shell in its terminal, meaning my entire workflow stays the same. The difference is that I'm now compiling a Linux binary and running it directly in Windows!&lt;/p&gt;

</description>
      <category>windows</category>
      <category>linux</category>
      <category>rust</category>
    </item>
    <item>
      <title>Hi, I'm W. Brian Gourlie</title>
      <dc:creator>W. Brian Gourlie</dc:creator>
      <pubDate>Mon, 07 Nov 2016 03:24:05 +0000</pubDate>
      <link>https://dev.to/dubyabrian/hi-im-w-brian-gourlie</link>
      <guid>https://dev.to/dubyabrian/hi-im-w-brian-gourlie</guid>
      <description>&lt;p&gt;I have been coding for 17 years.&lt;/p&gt;

&lt;p&gt;You can find me on Twitter as &lt;a href="https://twitter.com/dubya_brian" rel="noopener noreferrer"&gt;@dubya_brian&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I live in Madison, WI.&lt;/p&gt;

&lt;p&gt;I mostly program in these languages: Java, Javascript, Rust, and Elm.&lt;/p&gt;

&lt;p&gt;I like turtles.&lt;/p&gt;

&lt;p&gt;Nice to meet you.&lt;/p&gt;

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