<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Frank Meza</title>
    <description>The latest articles on Forem by Frank Meza (@frankmeza).</description>
    <link>https://forem.com/frankmeza</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%2F322386%2F99a2f5cc-97d0-44e9-9a75-476de258cdba.jpeg</url>
      <title>Forem: Frank Meza</title>
      <link>https://forem.com/frankmeza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/frankmeza"/>
    <language>en</language>
    <item>
      <title>Some examples of Rust Lifetimes in a struct</title>
      <dc:creator>Frank Meza</dc:creator>
      <pubDate>Wed, 17 Jun 2020 06:32:31 +0000</pubDate>
      <link>https://forem.com/frankmeza/some-examples-of-rust-lifetimes-in-a-struct-3m53</link>
      <guid>https://forem.com/frankmeza/some-examples-of-rust-lifetimes-in-a-struct-3m53</guid>
      <description>&lt;p&gt;So, you've decided to deep dive into Rust lifetime parameters. Nice!  &lt;/p&gt;

&lt;p&gt;Let's talk about when dinosaurs roamed the earth.  &lt;/p&gt;

&lt;h3&gt;
  
  
  SIMPLE CASE, COMPILER APPROVED ✅
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2015&amp;amp;code=%23!%5Ballow(unused)%5D%0A%23%5Bderive(Debug)%5D%0Astruct%20Earth%20%7B%0A%20%20%20%20location%3A%20String%2C%0A%7D%0A%0A%23%5Bderive(Debug)%5D%0Astruct%20Period%20%7B%0A%20%20%20%20age%3A%20String%2C%0A%7D%0A%0A%23%5Bderive(Debug)%5D%0Astruct%20Dinosaur%20%7B%0A%20%20%20%20location%3A%20Earth%2C%0A%20%20%20%20period%3A%20Period%2C%0A%20%20%20%20name%3A%20String%2C%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20montana%20%3D%20Earth%20%7B%0A%20%20%20%20%20%20%20%20location%3A%20String%3A%3Afrom(%22Montana%22)%2C%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20let%20jurassic%20%3D%20Period%20%7B%0A%20%20%20%20%20%20%20%20age%3A%20String%3A%3Afrom(%22Jurassic%22)%2C%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20let%20t_rex%20%3D%20Dinosaur%20%7B%0A%20%20%20%20%20%20%20%20name%3A%20String%3A%3Afrom(%22Tyrannosaurus%20Rex%22)%2C%0A%20%20%20%20%20%20%20%20location%3A%20montana%2C%0A%20%20%20%20%20%20%20%20period%3A%20jurassic%2C%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20t_rex)%3B%0A%7D%0A"&gt;this simple snippet in a Rust sandbox&lt;/a&gt;&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="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Earth&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Period&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;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dinosaur&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Earth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;period&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Period&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="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&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;montana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Earth&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Montana"&lt;/span&gt;&lt;span class="p"&gt;),&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;jurassic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Period&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="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jurassic"&lt;/span&gt;&lt;span class="p"&gt;),&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;t_rex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Dinosaur&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="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tyrannosaurus Rex"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;montana&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;period&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;jurassic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t_rex&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t_rex&lt;/span&gt;&lt;span class="py"&gt;.location&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t_rex&lt;/span&gt;&lt;span class="py"&gt;.period&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// compiler disallows these next lines, why?&lt;/span&gt;
    &lt;span class="c1"&gt;// println!("{:?}", montana); &lt;/span&gt;
    &lt;span class="c1"&gt;// println!("{:?}", jurassic); &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our aim here is to create a Rust compiler-approved Dinosaur struct, incorporating &lt;code&gt;Earth&lt;/code&gt; and &lt;code&gt;Period&lt;/code&gt; structs. This is what we have after a first pass of putting some props into a few &lt;code&gt;struct&lt;/code&gt; objects.  &lt;/p&gt;

&lt;p&gt;At the bottom of the snippet is a commented-out line that the compiler does not allow. Why? Because &lt;code&gt;montana&lt;/code&gt; and &lt;code&gt;jurassic&lt;/code&gt; are now owned by &lt;code&gt;t_rex&lt;/code&gt; and access to their values is now only allowed via &lt;code&gt;t_rex&lt;/code&gt;. This is not how we want the code to behave.&lt;/p&gt;

&lt;p&gt;Some more change is left to be done with the code, but this is the basic idea. We want a dinosaur to have knowledge of its location and its time period... but definitely not for the &lt;code&gt;struct&lt;/code&gt; itself to own the &lt;code&gt;Earth&lt;/code&gt; and &lt;code&gt;Period&lt;/code&gt; structs.&lt;/p&gt;

&lt;h3&gt;
  
  
  SLIGHTLY LESS NAÏVE APPROACH, NOT COMPILER-FRIENDLY YET ❌
&lt;/h3&gt;

&lt;p&gt;Now let's say you decide to start experimenting with Rust references and come up with this idea:&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;Dinosaur&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Earth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;period&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Period&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="nb"&gt;String&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 even before running any business logic between the structs, the compiler already wants to know more, it wants explicit lifetime parameters on &lt;code&gt;Dinosaur&lt;/code&gt;, so you dig around and put this together:&lt;/p&gt;

&lt;h3&gt;
  
  
  LIFETIME PARAMETER INFORMED CASE, COMPILER APPROVED ✅
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2015&amp;amp;code=%23%5Bderive(Debug)%5D%0Astruct%20Earth%20%7B%0A%20%20location%3A%20String%2C%0A%7D%0A%0A%23%5Bderive(Debug)%5D%0Astruct%20Period%20%7B%0A%20%20%20%20age%3A%20String%2C%0A%7D%0A%0A%23%5Bderive(Debug)%5D%0A%2F%2F%20instead%20of%20receiving%20the%20values%20%60location%60%20and%20%60period%60%2C%0A%2F%2F%20Dinosaur%20borrows%20(pre-existing)%20instances%20of%20Earth%2C%20Period%0Astruct%20Dinosaur%3C'a%3E%20%7B%0A%20%20%20%20location%3A%20%26'a%20Earth%2C%0A%20%20%20%20period%3A%20%26'a%20Period%2C%0A%20%20%20%20name%3A%20String%2C%0A%7D%0A%0A%0Afn%20main()%20%7B%0A%20%20let%20montana%20%3D%20Earth%20%7B%0A%20%20%20%20location%3A%20String%3A%3Afrom(%22Montana%22)%2C%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20let%20jurassic%20%3D%20Period%20%7B%0A%20%20%20%20%20%20age%3A%20String%3A%3Afrom(%22Jurassic%22)%2C%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20let%20t_rex%20%3D%20Dinosaur%20%7B%0A%20%20%20%20%20%20name%3A%20String%3A%3Afrom(%22Tyrannosaurus%20Rex%22)%2C%0A%20%20%20%20%20%20%20%20location%3A%20%26montana%2C%0A%20%20%20%20%20%20%20%20period%3A%20%26jurassic%2C%0A%20%20%20%20%7D%3B%0A%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20t_rex.name)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22ACCESSING%20MONTANA%20DIRECTLY%20%7B%3A%3F%7D%22%2C%20montana)%3B%20%0A%20%20%20%20println!(%22ACCESSING%20JURASSIC%20DIRECTLY%20%7B%3A%3F%7D%22%2C%20jurassic)%3B%20%0A%7D"&gt;this lifetime snippet in a Rust sandbox&lt;/a&gt;&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="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Earth&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Period&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;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// instead of receiving the values `location` and `period`,&lt;/span&gt;
&lt;span class="c1"&gt;// Dinosaur borrows (pre-existing) instances of Earth, Period&lt;/span&gt;
&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Dinosaur&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;'a&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;location&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;'a&lt;/span&gt; &lt;span class="n"&gt;Earth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;period&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;'a&lt;/span&gt; &lt;span class="n"&gt;Period&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="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&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;montana&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Earth&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Montana"&lt;/span&gt;&lt;span class="p"&gt;),&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;jurassic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Period&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="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Jurassic"&lt;/span&gt;&lt;span class="p"&gt;),&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;t_rex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Dinosaur&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="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Tyrannosaurus Rex"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;montana&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;period&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;jurassic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t_rex&lt;/span&gt;&lt;span class="py"&gt;.name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ACCESSING MONTANA DIRECTLY {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;montana&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ACCESSING JURASSIC DIRECTLY {:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;jurassic&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 a nutshell:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Dinosaur&lt;/code&gt; is receiving &lt;code&gt;location&lt;/code&gt; and &lt;code&gt;period&lt;/code&gt; as borrowed references. &lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;Dinosaur&lt;/code&gt; does not own the values, the Rust compiler wants explicit confirmation that the original value that the references refer to will still be valid as long as &lt;code&gt;Dinosaur&lt;/code&gt; needs them.&lt;/li&gt;
&lt;li&gt;This explicit confirmation comes in the form of lifetime parameters, on &lt;code&gt;Dinosaur&amp;lt;&amp;amp;'a&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  HANDLING SEVERAL LIFETIMES IN A SINGLE STRUCT
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;"But why doesn't each prop in &lt;code&gt;Dinosaur&lt;/code&gt; gets its own letter? What about this &lt;code&gt;&amp;lt;'a, 'b&amp;gt;&lt;/code&gt; that I saw somewhere on Stack Overflow? How does that work?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Oh, you mean something like &lt;code&gt;Dinosaur&amp;lt;'a, 'b&amp;gt;&lt;/code&gt; , right? So each of the letters represents a different scope that the struct knows about. Here is a &lt;a href="https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html?highlight=%27b#the-borrow-checker"&gt;link to the official documentation&lt;/a&gt;, but the important part of it right now is the following snippet, illustrating what the lifetime parameters signify in terms of scopes.&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// ---------+-- 'a&lt;/span&gt;
                          &lt;span class="c1"&gt;//          |&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;                     &lt;span class="c1"&gt;//          |&lt;/span&gt;
        &lt;span class="k"&gt;let&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;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// -+-- 'b  |&lt;/span&gt;
        &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;//  |       |&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;                     &lt;span class="c1"&gt;// -+       |&lt;/span&gt;
                          &lt;span class="c1"&gt;//          |&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"r: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//          |&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;                         &lt;span class="c1"&gt;// ---------+&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'll leave integrating this stuff into our &lt;code&gt;Dinosaur&lt;/code&gt; example up to you, o dear reader of mine.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>lifetimes</category>
    </item>
    <item>
      <title>Keep Calm And Carry On Coding: What to Do When Requirements Change</title>
      <dc:creator>Frank Meza</dc:creator>
      <pubDate>Fri, 06 Mar 2020 00:45:40 +0000</pubDate>
      <link>https://forem.com/olioapps/keep-calm-and-carry-on-coding-what-to-do-when-requirements-change-50mo</link>
      <guid>https://forem.com/olioapps/keep-calm-and-carry-on-coding-what-to-do-when-requirements-change-50mo</guid>
      <description>&lt;p&gt;Envision the following scenario: you are working away at the final 5% of a project, glad to be near the end, when you catch the first hints of unexpected changes coming in the near future.&lt;/p&gt;

&lt;p&gt;Your chat channel starts to fill up with innocuous phrases like "next iteration" and "looks good, now let's see how the users respond." It seems harmless, until you begin to receive successive waves of PDF files with pages upon pages of new requirements. “Huh…,” you might think to yourself, “this is turning into a completely different application!”&lt;/p&gt;

&lt;p&gt;If this is the first or second time you’ve experienced this, you might be somewhat new to the field of software development, and you may be feeling defeated. You want to feel that feeling of completion! Of crossing the finish line. It was so close. And now it appears so far away.&lt;/p&gt;

&lt;p&gt;How did this happen? People want to build software. They have a theory about what they want, and what their end users want. The perfect set of features, which will naturally be intuitive, simple and easy to use. But, it's just a theory. Once that theory is tested, it will most likely be only partially correct. That critical feature we built last month? We shipped it! It works perfectly. But, the users don’t understand it, and no one is using it. And so… the requirements will change. The goalposts will move.&lt;/p&gt;

&lt;p&gt;Here are some strategies to help you survive this lamentable, all-too-common predicament.&lt;/p&gt;

&lt;h2&gt;
  
  
  Acknowledge the Emotional Impact
&lt;/h2&gt;

&lt;p&gt;Late stage changes can brew a soul-crushing concoction of emotions.&lt;/p&gt;

&lt;p&gt;The first step out of this quagmire is to acknowledge that yes, you are frustrated. It's reasonable to be so. The code you've painstakingly engineered to match the original requirements might be critically maimed by "a few tweaks" in the 11th hour of the project. You feel stranded and overwhelmed. You start to wonder if life could have been easier if you had taken up a simpler career as a stuntman, or even as an astronaut.&lt;/p&gt;

&lt;p&gt;The next thing to do? Consciously choose constructive over destructive reactions to this distressing situation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Say No to Destructive Reactions
&lt;/h2&gt;

&lt;p&gt;You may be tempted to wallow in self pity, or lash out in frustration. But this will only isolate you from the people you need to work with in order successfully navigate those changes. Succumbing to feelings of helplessness or inadequacy is likewise counter-productive, as that will blunt your ability to confidently rewrite or even discard code that no longer makes sense. Finally, a total emotional disengagement from the project may temporarily insulate you from feeling bad, but apathy will completely block you from any productive work until you get beyond it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Grow by Facing Tough Times Head-On
&lt;/h2&gt;

&lt;p&gt;Try to adopt the mantra "I grow when challenged". Internalize that hard situations are invaluable for your growth as a software engineer.&lt;/p&gt;

&lt;p&gt;Consider an athlete who only trains with other, less capable athletes. While that individual may feel good about their performance, in reality they are capped at their present level of development. Similarly, a workplace devoid of significant and unexpected challenges will be a sterile and lifeless environment, which will stunt your growth as a professional problem solver.&lt;/p&gt;

&lt;p&gt;The path to becoming a better software engineer is a narrow one, full of rough patches and at times, very challenging terrain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tell Your Rubber Duck All About It
&lt;/h2&gt;

&lt;p&gt;Take a deep breath and step away from your computer. It’s time to make a fresh assessment of the situation, unencumbered by the weight of all that code you've written up to this point.&lt;/p&gt;

&lt;p&gt;First, gather up all the information relating to the requested changes. This might include the latest slide deck of changes, previous flow charts that you or a coworker has written, or scribbled notes and pinned chat messages.&lt;/p&gt;

&lt;p&gt;Once you've got that organized, the next thing to do is to try and explain to yourself what needs to be done in the simplest terms possible. Describe the present situation, the changes to be made, and the end goal. Using these thoughts as a guide, tour the regions of the system where changes are likely to occur. You might find that when you put a magnifying glass to them, those changes might turn out to be less pervasive than you initially thought.&lt;/p&gt;

&lt;p&gt;Finally, pick up a whiteboard marker and present your ideas to your peers. See if you can explain the problem and your proposed solution, using large, broad brushstrokes. Chances are, if you can articulate it, you've got a workable first draft solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice Paired Planning
&lt;/h2&gt;

&lt;p&gt;Paired planning is a great way to see the changes through someone else's eyes. It's likely that your partner will have a different perspective, and come up with a novel approach, or bring up some edge cases you hadn’t considered. A taste test between several possible approaches is a great way to find a simple, efficient, and correct solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choose Lo-fi When Designing
&lt;/h2&gt;

&lt;p&gt;During the planning phase, your goal should be to generate as many ideas for efficient solutions as possible. Don’t jump headfirst into coding. It's better to spend an hour outlining what you intend to do, explaining it to someone else, and clarifying your understanding. Whiteboard drawings, sequence diagrams, and high level design summaries can be some of your best tools during this process.&lt;/p&gt;

&lt;p&gt;With a whiteboard, you can virtually reimplement a system hundreds of times in the same duration it would take to incorrectly refactor the codebase once. The disposable nature of whiteboarding helps to not fall in love and commit to your first idea, which may not be the most optimal solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Weigh the Scope of Changes Against Your Budget
&lt;/h2&gt;

&lt;p&gt;Before writing more code, ask the following questions to help guide the process of a refactoring a large software project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  How much tolerance is there in the project schedule? Can we make paradigm shifts, or should we fall back to bandaids until the next release? Can we ask for more time to "do the right thing"?&lt;/li&gt;
&lt;li&gt;  Given a tight delivery deadline, can the changes be quarantined to a particular layer of the application? For example, in the situation where the UI must change but it's too risky at this point to change business logic, one could build transformation logic to retrofit objects flowing between the UI and the business layers.&lt;/li&gt;
&lt;li&gt;  Given a more liberal deadline, which other parts of the codebase should we also improve, while we're in here making the essential changes?&lt;/li&gt;
&lt;li&gt;  Is this an opportunity to clean up the codebase? For example, could we delete logic which models a deprecated requirement? If the answer here is yes, could that be helpful in formulating part of the solution?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Get Up and Take a Break
&lt;/h2&gt;

&lt;p&gt;It's always helpful to stop, take a break, and come back fresh. Move your body, and get outside of your own head for a short while. Do some jumping jacks. Pet a friendly dog. Go for a walk. Go get coffee, enjoy the outside air. Once revitalized, start coding with conviction, and you will prevail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accept that Change Is Inevitable In Living Projects
&lt;/h2&gt;

&lt;p&gt;It's several weeks later, and you've finally got a stable release. You matched the designs and the changes in functionality that they requested. Confidently, you submit your work for review. To your surprise, &lt;em&gt;they ask for even more changes&lt;/em&gt;. The cycle begins anew.&lt;/p&gt;

&lt;p&gt;Actually, this is a great thing! After seeing their vision come to fruition through your labors, and watching how end users react, your product owner better understands what they ultimately need. Based on what you delivered to them already, they can now see how much more is possible. It’s plainly obvious they trust you to make the next set of changes, since you've proven you're more than capable.&lt;/p&gt;

&lt;p&gt;So roll up your sleeves, and take the new challenge in stride. Carry on knowing that you are growing though the experience, and validating your ability to deliver software that is a living organism, always changing, always adapting!&lt;/p&gt;

</description>
      <category>software</category>
      <category>philosophy</category>
    </item>
    <item>
      <title>"The Value of Code Documentation"</title>
      <dc:creator>Frank Meza</dc:creator>
      <pubDate>Fri, 31 Jan 2020 23:02:24 +0000</pubDate>
      <link>https://forem.com/olioapps/the-value-of-code-documentation-1kg4</link>
      <guid>https://forem.com/olioapps/the-value-of-code-documentation-1kg4</guid>
      <description>&lt;p&gt;Code documentation is the collection of easy to understand images and written descriptions that explain what a codebase does and how it can be used.&lt;/p&gt;

&lt;p&gt;It can be simple explanatory comments above functions and blocks, or a full-fledged developer handbook, complete with prescriptive style dos-and-don'ts, overviews of each part of the application, and approaches to the most common types of coding tasks.&lt;/p&gt;

&lt;p&gt;It's often best to find a happy medium between these two extremes when you make the conscious decision to comment your code and outline the gnarlier areas of a codebase in plain language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why document your code?
&lt;/h2&gt;

&lt;p&gt;Why go to the trouble writing &lt;em&gt;about&lt;/em&gt; your code, instead of just &lt;em&gt;writing&lt;/em&gt; code? Wouldn't that just be more productive anyway?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yxEpy-sW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://user-images.githubusercontent.com/5914687/34965265-a6b6f904-fa07-11e7-92ff-22729040dd0a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yxEpy-sW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://user-images.githubusercontent.com/5914687/34965265-a6b6f904-fa07-11e7-92ff-22729040dd0a.png" alt="condescending wonka meme" width="495" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Mental Save Points
&lt;/h3&gt;

&lt;p&gt;Documentation is like a mental save point for those times when you finally get what's going on at the end of the day and don't want to lose momentum. Well documented code will ensure that when you need to dive back in tomorrow morning (or several months from now), you won't have to take as much time getting up to speed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Documented code gets reused
&lt;/h3&gt;

&lt;p&gt;The difference between a used library and an unused library  often comes down to its documentation.&lt;/p&gt;

&lt;p&gt;Who wants to use an undocumented open-source library or contribute to an undocumented project? Almost no one.&lt;/p&gt;

&lt;p&gt;In most cases, you'd rather use a less powerful library that has docs. Why? Because the documentation conveys other points of information about a codebase. The developers believe that the project is worth the time and energy to write about it in plain language so anyone who might be interested can quickly get started and learn how it works, and why key decisions were made.&lt;/p&gt;

&lt;p&gt;Finding a new, interesting, and well-documented project in your favorite language can be very exciting and fun. Let's look at the extreme alternative: being required to use an undocumented code library and potentially contribute to a seemingly byzantine codebase... it sounds pretty painful, right?&lt;/p&gt;

&lt;p&gt;So why haven't you documented your own code?&lt;/p&gt;

&lt;h2&gt;
  
  
  Documenting code is part of writing good code
&lt;/h2&gt;

&lt;p&gt;A cornerstone of good code is maintainability, achieved through understandable, legible documentation.&lt;/p&gt;

&lt;p&gt;There are multiple ways of documenting code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;choosing good names for variables and functions&lt;/li&gt;
&lt;li&gt;leaving brief comments within the code to help give context to future readers&lt;/li&gt;
&lt;li&gt;adding illustrative images such as sequence and entity relationship diagrams&lt;/li&gt;
&lt;li&gt;providing API docs, describing each class, method, argument and return value&lt;/li&gt;
&lt;li&gt;using a statically typed language, such as TypeScript (types as documentation)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Compare two examples
&lt;/h2&gt;

&lt;p&gt;Let's say you're getting some data from a backend API and "ingressing" it - converting it into a more useful shape for a front-end UI. Here's the first of two possible solutions.&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="c1"&gt;// wdgt.js&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;converter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;w&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;wdgtHdr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastTS&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;wdgtDtl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;dtlId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dtlId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;dtlName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dtlNm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;openTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;oTS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;closeTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cTS&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;wdgtRev&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;revs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;r&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prsn1Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prsn1Ln&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prsn1Fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cTS&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;assocId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;prsn1Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;assoc&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;prsn1Fn&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;prsn1Ln&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="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;amt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;note&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;note&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;revTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cTS&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;rr&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;wdgtHdr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;wdgtDtl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;wdgtRev&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 above code has the potential to be &lt;strong&gt;painful&lt;/strong&gt;. You can see by the name that this is some sort of converter that takes &lt;code&gt;w&lt;/code&gt; as its param.&lt;/p&gt;

&lt;p&gt;It looks like it returns a header, maybe details, and something called revs. Where did revs come from, anyway? Of course, there's no details about the incoming variable names &lt;code&gt;note, prsn1Id, prsn1Ln, prsn1Fn, amt, cTS&lt;/code&gt;... or is there? Is &lt;code&gt;rr&lt;/code&gt; short for &lt;code&gt;returned rev&lt;/code&gt;? Who can say.&lt;/p&gt;

&lt;p&gt;Let's look at the second example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// widget_utils.ts&lt;/span&gt;

&lt;span class="c1"&gt;// widgetConverter :: widgetRespDTO -&amp;gt; widget&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;widgetConverter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;widgetRespDTO&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Widget&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="na"&gt;widgetHeader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WidgetHeader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lastTS&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// timestamp&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;widgetDetail&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WidgetDetail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;widgetId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dtlNm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;openTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;oTS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// timestamp&lt;/span&gt;
        &lt;span class="na"&gt;closeTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cTS&lt;/span&gt; &lt;span class="c1"&gt;// timestamp&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// [WidgetRevisionsRespDTO] is nested inside of WidgetRespDTO&lt;/span&gt;

    &lt;span class="c1"&gt;// [WidgetRevisionsRespDTO].map -&amp;gt; [WidgetRevision]&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;widgetRevisions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadonlyArray&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WidgetRevision&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="nx"&gt;wResp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;revs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;wRevs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WidgetRevisionsRespDTO&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;WidgetRevision&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// how that other team names their variables...!&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;cmt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prsn1Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prsn1Ln&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prsn1Fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;amt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;cTS&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;wRevs&lt;/span&gt;

            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cmt&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;associateId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prsn1Id&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;associateName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;prsn1Fn&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;prsn1Ln&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;amt&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;revisionTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cTS&lt;/span&gt; &lt;span class="c1"&gt;// unix timestamp, and "closeTS" fyi&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;associateId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;associateName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nx"&gt;revisionTS&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;widgetHeader&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;widgetDetail&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;widgetRevisions&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;Wow, how different! This function is in a &lt;code&gt;util&lt;/code&gt; file, so the main file is less cluttered already. It's also written in TypeScript instead of plain JavaScript, so we have the benefit of type definitions to help guide us.&lt;/p&gt;

&lt;p&gt;We're converting &lt;code&gt;widgetRespDTO&lt;/code&gt;s into &lt;code&gt;widget&lt;/code&gt;s. Though shortened, we have full knowledge of what &lt;code&gt;wResp&lt;/code&gt; is. We're creating a &lt;code&gt;widgetHeader&lt;/code&gt;, and a &lt;code&gt;widgetDetail&lt;/code&gt;, that's easy to see. We also understand what &lt;code&gt;oTS&lt;/code&gt; and &lt;code&gt;cTS&lt;/code&gt; are. We can tell &lt;code&gt;WidgetRevisionsRespDTO&lt;/code&gt;s are nested inside of &lt;code&gt;WidgetRespDTO&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Someone very kindly renamed the incoming variables so &lt;em&gt;everyone&lt;/em&gt; who sees this code knows what they are. Finally, we see a &lt;code&gt;Widget&lt;/code&gt; must be composed of the returned &lt;code&gt;{widgetHeader, widgetDetail, widgetRevisions}&lt;/code&gt; object, since it must match the return type &lt;code&gt;Widget&lt;/code&gt; specified at the top.&lt;/p&gt;

&lt;p&gt;Which code would you feel better about using?&lt;/p&gt;

&lt;h2&gt;
  
  
  Who benefits?
&lt;/h2&gt;

&lt;p&gt;Well documented code benefits many people:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Junior developers
&lt;/h3&gt;

&lt;p&gt;Because it's in plain language and easy to understand, documentation helps junior developers and new team members feel confident jumping into a codebase. This prevents frustration and early abandonment of a task because it gets too complicated too quickly.&lt;/p&gt;

&lt;p&gt;It's also helpful for junior developers to write their own documentation so they can share their code with others and so they can get a better understanding of their code.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Senior developers
&lt;/h3&gt;

&lt;p&gt;By taking the time to document now, senior devs spend less time explaining their code to others in the future.&lt;/p&gt;

&lt;p&gt;They can also write more complex code. Plain language documentation lets others use it like a black box, without having to understand the inner workings.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Teams &amp;amp; open source projects
&lt;/h3&gt;

&lt;p&gt;Personnel changes can bring a tremendous slowdown to a project. Up-to-date and well documented code can serve as insurance against such slowdowns and allow the remaining team members to take a step back, review the code from a high level and decide on the best course of action moving forward, and if needed, on-board new employees.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. You
&lt;/h3&gt;

&lt;p&gt;When documenting code, you must summarize it. In order to summarize properly, you must be able to understand it, holding the different parts in your head at the same time. Writing or contributing to the documentation is a shortcut to really understanding a codebase, making huge contributions, and feeling like a great part of a great team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Good documentation can DRY up our coding experience
&lt;/h2&gt;

&lt;p&gt;As a programmer you've probably heard of the &lt;em&gt;Don't Repeat Yourself&lt;/em&gt; principle as a key to writing clean and concise code. Instead of repeating the same code block several times, write a function that can be written once and employed all over your application.&lt;/p&gt;

&lt;p&gt;In the same way, you and your team shouldn't be running into similar problems and then reinventing the wheel each time you attempt to solve them. For example, have you ever heard one of your coworkers or found yourself saying:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Agh, I solved something very similar to this but I never remember how I do it when it comes up... I should probably write it down somewhere, huh?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You then find yourself scanning the same Stack Overflow threads again and again.&lt;/p&gt;

&lt;p&gt;Save yourself and your coworkers this grief by following the spirit of the DRY principle and writing a document that you can employ every time the issue comes up! This will help you and others on your team save time, and be more effective in writing simpler, more manageable, and more efficient code.&lt;/p&gt;

&lt;p&gt;How has code documentation, or the lack thereof, affected you?&lt;/p&gt;

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