<?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: Lúcás Meier</title>
    <description>The latest articles on Forem by Lúcás Meier (@cronokirby).</description>
    <link>https://forem.com/cronokirby</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%2F188964%2F54de3908-9da0-4ffd-97f3-994273a0de89.png</url>
      <title>Forem: Lúcás Meier</title>
      <link>https://forem.com/cronokirby</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/cronokirby"/>
    <language>en</language>
    <item>
      <title>React Pitfalls: useState initialization</title>
      <dc:creator>Lúcás Meier</dc:creator>
      <pubDate>Thu, 09 Jan 2020 09:55:43 +0000</pubDate>
      <link>https://forem.com/cronokirby/react-pitfalls-usestate-initialization-39be</link>
      <guid>https://forem.com/cronokirby/react-pitfalls-usestate-initialization-39be</guid>
      <description>&lt;p&gt;&lt;a href="https://cronokirby.github.io/posts/react-pitfalls-usestate/"&gt;Original&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;This is a quick post about a "gotcha" I encountered recently in a React application.&lt;br&gt;
This involved the use of React's &lt;code&gt;useState&lt;/code&gt; hook, which had a subtle difference&lt;br&gt;
between how I thought the hook worked, and how it actually worked.&lt;/p&gt;
&lt;h1&gt;
  
  
  React Hooks
&lt;/h1&gt;

&lt;p&gt;If you're already familiar with hooks in React, feel free to skip this section. This&lt;br&gt;
is just a recap for those out of the loop.&lt;/p&gt;

&lt;p&gt;Although the first real framework I worked with was &lt;em&gt;Vue&lt;/em&gt; (I don't count the one application&lt;br&gt;
I built with JQuery as having used a framework), I've been using &lt;em&gt;React&lt;/em&gt; a ton lately, and&lt;br&gt;
it's really been growing on me. There's a lot of advantages to being the "top dog" in the&lt;br&gt;
framework game, and React's popularity gives you access to a large ecosystem, and great&lt;br&gt;
patterns for working on frontend applications.&lt;/p&gt;

&lt;p&gt;One thing I really like about React is the recent "hooks" feature.&lt;/p&gt;

&lt;p&gt;Previously, react distinguished between &lt;em&gt;function&lt;/em&gt; components, which took in some parameters,&lt;br&gt;
called &lt;em&gt;props&lt;/em&gt;, and returned some HTML to be rendered, e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;TitleCard&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi my name is &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You also had &lt;em&gt;class&lt;/em&gt; components, which at first resemble &lt;em&gt;function&lt;/em&gt; components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;TitleCard&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;render&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi my name is &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;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;But class components also have a lot of other features in addition to just rendering some data.&lt;br&gt;
Notably, they have access to state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;count&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="nx"&gt;render&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;+&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component will render a count, and clicking on the button inside that component will increment&lt;br&gt;
the state of that component.&lt;/p&gt;

&lt;p&gt;Having a clear way to use state, and other effects like network requests, etc was something missing&lt;br&gt;
from function components. That's where hooks come in. Hooks, in brief, allow you to use&lt;br&gt;
these things in function components.&lt;/p&gt;

&lt;p&gt;For example, if we wanted to take our &lt;code&gt;Counter&lt;/code&gt; component from the previous example&lt;br&gt;
using a function component with hooks, it'd look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;+&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The hook provides us with two values: &lt;code&gt;count&lt;/code&gt;, and &lt;code&gt;setCount&lt;/code&gt;. The idea is that&lt;br&gt;
&lt;code&gt;count&lt;/code&gt; always holds the current value of the state, and &lt;code&gt;setCount&lt;/code&gt; allows us to change&lt;br&gt;
this value. The cool thing is that React will always "do the right thing" and magically&lt;br&gt;
update this value and rerender the component if necessary. Neat!&lt;/p&gt;
&lt;h1&gt;
  
  
  Rerendering
&lt;/h1&gt;

&lt;p&gt;One of the core principles when working with React is that the framework strives&lt;br&gt;
to make sure that the values a component logically has is always reflected on screen.&lt;br&gt;
This means that if some value changes, because of a hook, or because one of the props changed,&lt;br&gt;
then a rerender needs to happen to make sure that what's on screen matches what's "in the code".&lt;/p&gt;

&lt;p&gt;For example, let's split our counter in two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;ShowCounter&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;+&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ShowCounter&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the &lt;code&gt;Counter&lt;/code&gt; component is the one actually holding the state, and the &lt;code&gt;ShowCounter&lt;/code&gt; component&lt;br&gt;
is just a simple function that shows the data given to it. In order to make sure that the user is&lt;br&gt;
always seeing the actual value of the counter, React has to rerender the &lt;code&gt;ShowCounter&lt;/code&gt; component&lt;br&gt;
whenever the count passed to it changes value.&lt;/p&gt;

&lt;p&gt;This means that React will end up executing the code of the &lt;code&gt;ShowCounter&lt;/code&gt; function again. This&lt;br&gt;
is why its important to avoid firing off network requests without &lt;code&gt;useEffect&lt;/code&gt; inside&lt;br&gt;
a function component, because you only want to do certain effects when they need to be run,&lt;br&gt;
and not just when the component frivously rerenders.&lt;/p&gt;
&lt;h1&gt;
  
  
  State is initialised only once
&lt;/h1&gt;

&lt;p&gt;Now we come to the main pitfall I want to talk about in this post.&lt;/p&gt;

&lt;p&gt;Let's allow the user set a value for the counter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;initial&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initial&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ShowCounter&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Based on our previous model, if &lt;code&gt;initial&lt;/code&gt; changes, then the component has to rerender,&lt;br&gt;
and so &lt;code&gt;useState&lt;/code&gt; gets called with &lt;code&gt;initial&lt;/code&gt;, and so the count becomes &lt;code&gt;initial&lt;/code&gt;, right?&lt;/p&gt;

&lt;p&gt;Well it turns out that that's not what happens. In fact with the way &lt;code&gt;useState&lt;/code&gt; works,&lt;br&gt;
the initial value matters only &lt;strong&gt;the first time a component renders&lt;/strong&gt;. After that, it will&lt;br&gt;
preserve the state between renders.&lt;/p&gt;

&lt;p&gt;This means that we need to do this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;initial&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&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;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;initial&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;initial&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;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ShowCounter&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We haven't gone over the &lt;code&gt;useEffect&lt;/code&gt; hook in detail, but the way it works is that it executes&lt;br&gt;
the callback function only if the values inside the array have changed. So here it will set the counter&lt;br&gt;
to an initial value, but only when the initial value changes. This is the correct way to do&lt;br&gt;
something like this.&lt;/p&gt;

&lt;p&gt;Basically, to avoid this pitfall, &lt;strong&gt;you never want to have anything dynamic inside the call to useState&lt;/strong&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  Why is this the case?
&lt;/h1&gt;

&lt;p&gt;Well, remember how we went over how React "tries to do the right thing". Well, it turns out that&lt;br&gt;
by doing things this way, you actually preserve state between rerenders, which is generally the behavior you want.&lt;/p&gt;

&lt;p&gt;For example, let's say we had something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;c&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;+&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;Now we're showing a name in addition to the value of the count. We want to rerender if the count or the name&lt;br&gt;
changes, since we want the user to see what the current value actually is, but we don't want the count to be&lt;br&gt;
lost just because the name changed. That's why it makes sense for &lt;code&gt;useState&lt;/code&gt; to preserve the&lt;br&gt;
state between rerenders.&lt;/p&gt;

&lt;p&gt;It'd require a lot more code to create the behavior of preserving state between rerenders if&lt;br&gt;
it didn't work that way, but it didn't take much effort with &lt;code&gt;useEffect&lt;/code&gt; to do what we wanted&lt;br&gt;
in the other case. You generally want to try and make the more common use case easy&lt;br&gt;
with frameworks, don't you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;React Hooks&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>hooks</category>
      <category>frontend</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Structured Immersion</title>
      <dc:creator>Lúcás Meier</dc:creator>
      <pubDate>Wed, 08 Jan 2020 17:19:16 +0000</pubDate>
      <link>https://forem.com/cronokirby/structured-immersion-763</link>
      <guid>https://forem.com/cronokirby/structured-immersion-763</guid>
      <description>&lt;p&gt;When it comes to language learning, one approach that I really subscribe to is immersion.&lt;br&gt;
This means trying to absorb as much of the language as possible, as often as possible.&lt;br&gt;
It's often said that "speaking with locals" is a good way to accelerate the learning process,&lt;br&gt;
and there's a lot of truth with that statement.&lt;/p&gt;
&lt;h3&gt;
  
  
  Comprehensible Input
&lt;/h3&gt;

&lt;p&gt;One aspect of immersion that's often overlooked is the idea of &lt;em&gt;comprehensible input&lt;/em&gt;. The idea&lt;br&gt;
is that the input you get in your target language is more useful if it's understandable despite&lt;br&gt;
your lack of knowledge of some words or grammar. Those new language points can be assimilated because&lt;br&gt;
the entire message remains comprehensible because of context, other language points you do know,&lt;br&gt;
or other cues such as gestures or video.&lt;/p&gt;

&lt;p&gt;For example, if you're trying to learn Japanese, watching anime can be great (provided you don't use subtitles),&lt;br&gt;
because the language used can be simple enough to understand based on the visuals on the screen.&lt;br&gt;
If you can guess what people would be saying with just a few key words, then that's a good candidate&lt;br&gt;
for comprehensible input, even if you don't understand most of the actual Japanese.&lt;/p&gt;
&lt;h3&gt;
  
  
  Comprehensibility varies
&lt;/h3&gt;

&lt;p&gt;Now, not all input is very good at being comprehensible. At the beginning, most native media&lt;br&gt;
will be too difficult to be effective. If you only understand 10% of the sentences in some piece of media,&lt;br&gt;
you're not going to be doing much learning with that source. Conversely, as you advance, media you completely&lt;br&gt;
understand won't teach you new pieces of language, although it does help you solidify your understanding&lt;br&gt;
of the language, and can help with things like pronunciation and choosing native-sounding phrasing.&lt;/p&gt;

&lt;p&gt;Even worse is that it can actually be difficult to tell whether or not a piece of media is&lt;br&gt;
at the right level before actually consuming it. Even if that piece of media is aimed at a native&lt;br&gt;
audience with a similar language level to your own, it's possible that the topic might be one where&lt;br&gt;
you're lacking a lot of vocabulary.&lt;/p&gt;
&lt;h3&gt;
  
  
  Context matters
&lt;/h3&gt;

&lt;p&gt;For example, even if you've watched a lot of anime aimed at young teenagers, if you start watching&lt;br&gt;
a piece of science fiction aimed at the same audience, it's possible to be lacking most of the vocabulary&lt;br&gt;
they'll be using. The common, everyday phrases may be engrained, but space exploration / giant robot&lt;br&gt;
jargon is likely to be completely lost on you.&lt;/p&gt;

&lt;p&gt;Furthermore, if identifying whether or not a piece of media is going to make for good learning&lt;br&gt;
is hard, then finding that media in the first place is even harder. Of course, my recommendation&lt;br&gt;
would be primarily to focus on media you enjoy consuming anyways. After all, the point&lt;br&gt;
of learning a foreign language as a hobby is to have fun, isn't it.&lt;/p&gt;

&lt;p&gt;Even then, being able to organise the media you plan on consuming based on how appropriate&lt;br&gt;
it would be for your level of language would help immensely in expediting the learning process.&lt;br&gt;
If you could know which show to watch next based on what your current vocabulary is, that would&lt;br&gt;
save a lot of time, and make immersing a lot easier.&lt;/p&gt;

&lt;p&gt;With immersion, it's about &lt;code&gt;quantity * quality&lt;/code&gt;, so you want to make it as easy as possible&lt;br&gt;
to get a lot of good quality (i.e. level appropriate) media.&lt;/p&gt;
&lt;h2&gt;
  
  
  Tracking progress
&lt;/h2&gt;

&lt;p&gt;One aspect of this problem that I've briefly gone over without expliciting is tracking your&lt;br&gt;
progress. In order to provide such a structure, and know what media to consume next, you need&lt;br&gt;
to keep good tabs on how much language you know, and the topics where your vocabulary is lacking.&lt;br&gt;
Often people will use flashcard systems like Anki in order to keep track of their progress,&lt;br&gt;
but mainly to remind themselves of old vocabulary to make sure they don't forget it. The problem&lt;br&gt;
is that manual entries need to be created and entered into this flashcard system, even though media is often&lt;br&gt;
consumed in a way where it could be entered manually.&lt;/p&gt;

&lt;p&gt;Well, that's the introduction to some of the problems in the space of language immersion, and thankfully I think&lt;br&gt;
a good deal of them can actually be addressed, thanks to the most media being digital nowadays.&lt;br&gt;
Well, at least, the cheapest (free) and most easily accessible native media is going to be digital&lt;br&gt;
and available on the internet.&lt;/p&gt;
&lt;h2&gt;
  
  
  An ideal immersion process
&lt;/h2&gt;

&lt;p&gt;Ideally, here's what the daily immersion process would look like:&lt;/p&gt;

&lt;p&gt;You arrive at the "dashboard" and have a list of media you can consume. You simply dedicate as much&lt;br&gt;
time as you want that day to consuming it: you read and watch as much as you want to that day.&lt;br&gt;
The media is recommended to you based on how appropriate it is for your immersion, given your current&lt;br&gt;
language level. The media will have a right amount of new vocabulary and grammar to keep you stimulated&lt;br&gt;
and learning, but not enough to drown you in unknown topics. Additionally, your progress is updated&lt;br&gt;
based on that media you've consumed. There's no need to manually enter the new words or grammar points&lt;br&gt;
that you learned.&lt;/p&gt;
&lt;h3&gt;
  
  
  Flashcard systems
&lt;/h3&gt;

&lt;p&gt;Furthermore, the system can act as a flashcard system like Anki, where you automatically review the vocabulary&lt;br&gt;
you previously learned, in order to make sure it stays fresh and accessible in your mind.&lt;br&gt;
I won't go into the benefit of spaced-repetition systems like Anki, but for those that do benefit from them,&lt;br&gt;
having automatic entries is indeed quite convenient.&lt;/p&gt;

&lt;p&gt;I think that you could actually make a system providing that functionality to users.&lt;br&gt;
You could keep track of a user's progress in their target language, and use that to source media&lt;br&gt;
that they immerse in, and that you use to keep track of their ongoing progress.&lt;/p&gt;

&lt;p&gt;The crux of this whole process is keeping track of a user's progress in a language. Without this&lt;br&gt;
feature, none of the others are possible. All of those other aspects are consequences of exploiting&lt;br&gt;
this key feature to the fullest extent.&lt;/p&gt;
&lt;h3&gt;
  
  
  Saving manual work
&lt;/h3&gt;

&lt;p&gt;When a user sources new media for themselves, they're using their knowledge of their own progress&lt;br&gt;
in order to judge whether or not that media is comprehensible enough for them. And when they're entering&lt;br&gt;
new sentences into their flashcard system, they're copying over the new vocabulary from that media.&lt;br&gt;
This is done, once again, using thier knowledge of their own learning process. By keeping track of&lt;br&gt;
this progress in an automated fashion, we can in turn automate so many aspects of the immersion process.&lt;/p&gt;
&lt;h2&gt;
  
  
  Is this all just a pipe dream?
&lt;/h2&gt;

&lt;p&gt;Now, that's a high level overview behind the idea behind such a system, but how feasible would&lt;br&gt;
a system of this kind be? In all honesty, I'm not sure if this scales to any language. I'm not&lt;br&gt;
even confident it would work as well for English as it would for Japanese. On the other hand,&lt;br&gt;
Japanese is structured enough to make grammatical analysis quite easy.&lt;br&gt;
Different langauges have different ways of building up sentences, which can make figuring out what&lt;br&gt;
the user does and does not know in that sentence more or less difficult. Another difficulty relates&lt;br&gt;
to figuring out what's comprehensible in a smart way, as we'll see later.&lt;/p&gt;
&lt;h2&gt;
  
  
  It might not be for Japanese
&lt;/h2&gt;

&lt;p&gt;So, let's look at how realistic such a system could be, through the lens of Japanese.&lt;/p&gt;

&lt;p&gt;Now, one of the core components of this system is figuring out what language concepts&lt;br&gt;
are in a given sentence. Now, I've been using the &lt;em&gt;sentence&lt;/em&gt; as a basic building block of media,&lt;br&gt;
and it's a good unit for splitting large bits of text. Thankfully, Japanese sentences are marked by punctuation&lt;br&gt;
in actual text, and multiple lines of dialogue are easy to split even without punctuation.&lt;/p&gt;
&lt;h3&gt;
  
  
  Luckily simple
&lt;/h3&gt;

&lt;p&gt;Japanese morphology is simple, or regular enough in order to split sentences into words in quite a consistent&lt;br&gt;
way. There are a handful of tools, but the one I've used most recently was&lt;br&gt;
&lt;a href="http://taku910.github.io/mecab/"&gt;MeCab&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This tool takes the form of a command line program, that accepts a stream of sentences. The output of the tool&lt;br&gt;
looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mecab
猫は食べた
猫 名詞,一般,*,*,*,*,猫,ネコ,ネコ
は 助詞,係助詞,*,*,*,*,は,ハ,ワ
食べ  動詞,自立,*,*,一段,連用形,食べる,タベ,タベ
た 助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
EOS
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The first line is our sentence, "猫は食べた", literally "The cat ate". The next lines are the different words&lt;br&gt;
MeCab managed to find throughout that sentence, along with their pronunciation, their part of speech, etc.&lt;br&gt;
For example, the first word it gives us is "猫" meaning "cat". It tells is that it's a noun, pronounced "neko", etc.&lt;/p&gt;

&lt;p&gt;Using MeCab, you can quite easily split native media into the words that compose it. This makes it easy to take all the "new" words&lt;br&gt;
based on this classification, and add them to your flashcard system, along with the surrounding context.&lt;br&gt;
Generally, you want the surrounding sentence for a given word in a flashcard system, but that's really another discussion entirely.&lt;/p&gt;

&lt;h3&gt;
  
  
  Except for grammar
&lt;/h3&gt;

&lt;p&gt;One tricky thing that MeCab doesn't take care of, and that I haven't completely looked into solving is figuring out what grammatical&lt;br&gt;
components figure in a given sentence. As we've just seen, it's quite easy to get the words in a Japanese sentence out, but it&lt;br&gt;
seems much harder to get grammar out. There are &lt;em&gt;some&lt;/em&gt; things you can do. For example, that last sentence used the past tense,&lt;br&gt;
and MeCab considers the past conjugation to be a word, for some reason, so you can pick up on cues like that.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is comprehensible enough?
&lt;/h2&gt;

&lt;p&gt;Now, another problem is when to consider a piece of media suitable for consumption. Taking a sentence based approach seems&lt;br&gt;
appropriate at a first glance. That is to say, we can classify each sentence with a certain comprehensibility score,&lt;br&gt;
and then aggregate that score throughout the whole text, to get an overall score. We can then use that score to&lt;br&gt;
rank the different pieces of media we've managed to source previously. We would also reject pieces of media&lt;br&gt;
that are too comprehensible.&lt;/p&gt;

&lt;p&gt;One simple metric would be to look at what percentage of words are known in a given sentence the user knows.&lt;br&gt;
The problem with this metric comes from its simplicity: it doesn't take into account the surrounding context,&lt;br&gt;
nor how easy some words are to guess. I don't have many ideas for the time being to go beyond this simple metric&lt;br&gt;
however.&lt;/p&gt;

&lt;h3&gt;
  
  
  User input
&lt;/h3&gt;

&lt;p&gt;Other approaches could include user input on whether or not they understood a sentence despite not knowing the&lt;br&gt;
words in that sentence. You could use that to suggest similar sentences.&lt;/p&gt;

&lt;p&gt;That being said, even a simple metric would help immensely in ranking texts, as comprehensibility doesn't need to&lt;br&gt;
be exact to be beneficial. Even ranking materials just on the words you don't know would help immensely with triaging&lt;br&gt;
all the media you have available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sourcing media
&lt;/h2&gt;

&lt;p&gt;Now we come to the problem of sourcing media.&lt;/p&gt;

&lt;p&gt;An advanced approach would be to scrape the web for material and build up a gigantic database of native media.&lt;br&gt;
Although impressive and expansive, this approach would also be expensive, and fail to satisfy a key criteria:&lt;br&gt;
providing media the user is actually interested in.&lt;/p&gt;

&lt;p&gt;As mentioned before, having media the user actually wants to consume is actually important in keeping the&lt;br&gt;
language learning process from becoming a long march of boredom.&lt;/p&gt;

&lt;h3&gt;
  
  
  Accepting user submissions
&lt;/h3&gt;

&lt;p&gt;An easier approach that also satisfies this criterion is accepting texts from user input. The user can submit&lt;br&gt;
text from subtitles or books they're reading, along with whatever sources they want to consume eventually.&lt;br&gt;
The system would also need to keep track of the order of certain texts, because for certain things, like books, you might&lt;br&gt;
want to read them in a linear order. In order to help with the fun of immersion, you want to respect ordering like this.&lt;br&gt;
Respecting the chronological order of media also helps with understanding that media. Obviously,&lt;br&gt;
for serial media, understanding the story requires having understood most of the previous parts&lt;br&gt;
of the story.&lt;/p&gt;

&lt;p&gt;This approach is also easier, because we don't have to worry about sourcing the material, and can instead&lt;br&gt;
rely on the user providing us with material on their own. This is similar to how &lt;a href="//japanese.io"&gt;&lt;/a&gt; works,&lt;br&gt;
where you enter text into their site, and then it augments it with dictionary entries and what not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Presenting media
&lt;/h2&gt;

&lt;p&gt;Another thing I'd like to touch on is how you present this media to the user. One disadvantage to this text based&lt;br&gt;
approach is that the user has to read. What you get out of it is that you can track the progress&lt;br&gt;
of the user in the media, and thus enter these new words automatically.&lt;/p&gt;

&lt;h3&gt;
  
  
  Augmented immersion
&lt;/h3&gt;

&lt;p&gt;When presenting text to the user, you can augment it without know the user's progress, adding&lt;br&gt;
things like an automatic dictionary, sentence, copying, etc. You can use the user's progress to highlight words they&lt;br&gt;
don't know. You can add the words they read to the flashcard system. You can also take into account their use&lt;br&gt;
of the built-in dictionary while reading the text in order to update the flashcard system.&lt;br&gt;
If they've looked up a word, they don't know it as well as previously thought, so that word needs to be bumped&lt;br&gt;
up the flashcard stack.&lt;/p&gt;

&lt;p&gt;You could also just rank the pieces of media, and then let the user read them on their own time.&lt;br&gt;
The disadvantage there would be losing the augmented reading experience, as well as not being able&lt;br&gt;
to automatically track progress. For example, if the user is reading a novel through the augmented&lt;br&gt;
portal, you can track their position and language progress through the book. If they read it on their own,&lt;br&gt;
you don't know how far they've gotten to the book. You could let them tell you when they've finished the whole&lt;br&gt;
book, but adding that large chunk of language after the fact isn't as useful.&lt;/p&gt;

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

&lt;p&gt;To wrap things up, I think that immersion is very important for learning a foreign language, and that&lt;br&gt;
making immersion as easy as possible helps making learning even easier. Having a system that&lt;br&gt;
keeps track of your progress in a language can help save you time in learning and revising vocabulary,&lt;br&gt;
and can help triage and augment the immersion process.&lt;/p&gt;

</description>
      <category>japanese</category>
      <category>learning</category>
      <category>nlp</category>
      <category>language</category>
    </item>
    <item>
      <title>React, Typescript, Parcel: Setting up Hot Module Reloading</title>
      <dc:creator>Lúcás Meier</dc:creator>
      <pubDate>Mon, 25 Nov 2019 21:24:47 +0000</pubDate>
      <link>https://forem.com/cronokirby/react-typescript-parcel-setting-up-hot-module-reloading-4f3f</link>
      <guid>https://forem.com/cronokirby/react-typescript-parcel-setting-up-hot-module-reloading-4f3f</guid>
      <description>&lt;p&gt;Recently I had to setup a new greenfield project using React and Typescript. I'm a big fan of using &lt;a href="https://parceljs.org/" rel="noopener noreferrer"&gt;parcel&lt;/a&gt; over other bundling tools like &lt;em&gt;webpack&lt;/em&gt;. &lt;em&gt;Parcel&lt;/em&gt; usually works with essentially no config whatsoever, and ends up being much simpler.&lt;/p&gt;

&lt;p&gt;I did run into a few issues getting hot-module-reloading to work, so that's why I'm writing up this short post.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is HMR?
&lt;/h2&gt;

&lt;p&gt;Hot-module-reloading lets us change the code of our application while maintaining our current state and position in the website. This allows us to do things like tweak the styling of some page of our app without having to navigate all the way back to that page each time. This helps immensely when prototyping and developing an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up a basic build
&lt;/h2&gt;

&lt;p&gt;After creating an empty directory to set up the project in, we need to run a command to create an npm project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After following the instructions in this command, you'll have a &lt;code&gt;package.json&lt;/code&gt; file with metadata.&lt;/p&gt;

&lt;h3&gt;
  
  
  Essential packages
&lt;/h3&gt;

&lt;p&gt;Now it's time to install the packages we'll end up using in the actual application. There are other packages we'll use as a developer, but that the end-user doesn't actually need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first package is &lt;strong&gt;React&lt;/strong&gt;, of course, which happens to be our framework of choice. &lt;code&gt;react-dom&lt;/code&gt; is necessary for rendering in the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Developer packages
&lt;/h3&gt;

&lt;p&gt;Now we install all the tools we'll be using to develop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev parcel-bundler typescript @types/react @types/react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use &lt;code&gt;--save-dev&lt;/code&gt; to make sure that these won't end up in the code that we end up shipping in production.&lt;/p&gt;

&lt;p&gt;We can setup the typescript compiler with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tsc --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then need to add the following line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"jsx"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"react"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in order to get the Typescript compiler to handle our React html correctly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the basic frontend
&lt;/h3&gt;

&lt;p&gt;Now we need to create a basic UI to test out the packages we've installed:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"generator"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"parceljs"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"./index.tsx"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This file contains the basic HTML for our website. React will be filling in the rest, starting from the element with id &lt;code&gt;root&lt;/code&gt;. We reference our main typescript file, which will be like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.tsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./components/App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we render our main &lt;code&gt;App&lt;/code&gt; component in the element with id &lt;code&gt;root&lt;/code&gt;. The component is defined like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;components/App.tsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FunctionComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FunctionComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Count: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;+&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Understanding everything going on here isn't important, all that matters is having a component with a bit of state (here, the current count) so we can see whether or not HMR works. This component is just a counter we can increment with a button.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bundling
&lt;/h3&gt;

&lt;p&gt;Now, we can bundle and serve the app by adding the following script to &lt;code&gt;package.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"parcel index.html"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in order to start up a development server, we just run &lt;code&gt;npm run dev&lt;/code&gt;. Parcel will magically figure out how to bundle everything based on the imports in the files themselves, without any configuration from us.&lt;/p&gt;

&lt;p&gt;We can now navigate to our frontend at &lt;code&gt;http://localhost:1234&lt;/code&gt; in our browser (or whatever else parcel tells us).&lt;/p&gt;

&lt;p&gt;We should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F19j7icr4kgo7ghmp0f4v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F19j7icr4kgo7ghmp0f4v.png" alt="Counter"&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, if we change the following line in &lt;code&gt;App.tsx&lt;/code&gt; to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;The Count: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the entire page reloads, and the count is lost, before we see our change. This isn't ideal, because we might want to be styling or editing the logic for a specific state. Having to reset the state manually each time is a pain. This is where HMR is useful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up HMR
&lt;/h3&gt;

&lt;p&gt;We first need to install a package for hot module reloading in React.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save-dev react-hot-loader
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We only need this in development, hence &lt;code&gt;--save-dev&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We now need to modify the root component of our app, in &lt;code&gt;App.tsx&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;hot&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-hot-loader&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kr"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kr"&gt;module&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This only needs to be done to the top-level component, as we add more components they can be written normally.&lt;/p&gt;

&lt;p&gt;With this simple change, we now have HMR! In order to test this out, try incrementing the counter, and then changing the text in the component. The counter's state shouldn't change even as the text does. This is HMR in action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Repo
&lt;/h2&gt;

&lt;p&gt;I've hosted the code for this demo &lt;a href="https://github.com/cronokirby/parcel-ts-react-demo" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>hmr</category>
      <category>parcel</category>
    </item>
    <item>
      <title>Data Races vs Race Conditions</title>
      <dc:creator>Lúcás Meier</dc:creator>
      <pubDate>Mon, 01 Jul 2019 16:54:29 +0000</pubDate>
      <link>https://forem.com/cronokirby/data-races-vs-race-conditions-17bk</link>
      <guid>https://forem.com/cronokirby/data-races-vs-race-conditions-17bk</guid>
      <description>&lt;p&gt;This is a quick post about the difference between &lt;code&gt;Data Races&lt;/code&gt; and &lt;code&gt;Race Conditions&lt;/code&gt;, and how data structures or patterns providing freedom from data races can fail to provide race condition freedom.&lt;/p&gt;

&lt;p&gt;The examples will be given in &lt;code&gt;Go&lt;/code&gt;, since that's a language with a few&lt;br&gt;
of the concurrent constructs that come into play here, as well as the language that sparked this blog post in the first place.&lt;/p&gt;
&lt;h2&gt;
  
  
  Data Races
&lt;/h2&gt;

&lt;p&gt;I agree almost entirely with&lt;br&gt;
&lt;a href="https://doc.rust-lang.org/nomicon/races.html"&gt;rust's definition of data races&lt;/a&gt;. Under this definition, a data race is when one or more threads concurrently access a location in memory / variable, at least one of which is a write, and at least one of which is not synchronized with other threads.&lt;/p&gt;

&lt;p&gt;For example, multiple concurrent reads to an unsychronized variable are perfectly&lt;br&gt;
fine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&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;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread B: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread A: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&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;Even though the order of printing will vary from execution to execution, there are no data races since both threads are merely reading from the data.&lt;/p&gt;

&lt;p&gt;If we now have one of the threads access &lt;code&gt;a&lt;/code&gt; mutably, we introduce a data race:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread A: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can solve this by introducing a mutex to synchronize access to &lt;code&gt;a&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;sync&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Mutex&lt;/span&gt;
    &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
        &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread A: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unlock&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;Both threads are accessing &lt;code&gt;a&lt;/code&gt; at the same time, and one of them is writing, but since the access is synchronized, this is no longer a data race.&lt;/p&gt;

&lt;h2&gt;
  
  
  Race Conditions
&lt;/h2&gt;

&lt;p&gt;Race conditions stem from &lt;code&gt;non-determinism&lt;/code&gt; in concurrent programs. In theory any observable non-determinism from concurrency could be &lt;em&gt;considered&lt;/em&gt; a race condition, but in practice what constitutes a race condition depends on what properties we want our program to respect.&lt;/p&gt;

&lt;p&gt;Let's take the following program as an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&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;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread B"&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;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Thread A"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We'll see some sort of random interleaving of the two messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Thread A
Thread A
Thread B
Thread A
Thread B
Thread B
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This could be considered a race condition, if the exact order of the printing was a property we wanted our program to respect. We could use some form of synchronization to enforce that order of printing.&lt;/p&gt;

&lt;p&gt;In practice we wouldn't consider this a race condition even if the execution isn't deterministic, because this isn't a property we care about.&lt;/p&gt;

&lt;p&gt;In summary, a race condition is some violation of the properties our program should have arising from the concurrent execution of the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Race Conditions without Data Races
&lt;/h2&gt;

&lt;p&gt;The reason I decided to make this post was a discussion I had recently. Someone was claiming that using Go's channels prevents &lt;em&gt;race conditions&lt;/em&gt; because the operations are always thread safe.&lt;/p&gt;

&lt;p&gt;It is true that Go's channels are free from &lt;em&gt;data races&lt;/em&gt;, so long as memory isn't shared in other ways. That being, said it's pretty easy to write a program that has a race condition despite only using channels.&lt;/p&gt;

&lt;p&gt;In this example, we'll have a simple server responding to requests to get the value of an integer, and to set the value of an integer.&lt;/p&gt;

&lt;p&gt;The messages look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We'll use &lt;code&gt;0&lt;/code&gt; as the id for &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;1&lt;/code&gt; as the id for &lt;code&gt;set&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our server type will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;msgs&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;
  &lt;span class="n"&gt;resps&lt;/span&gt; &lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;newServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;msgs&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;resps&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;chan&lt;/span&gt; &lt;span class="kt"&gt;int&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="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;msgs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;resps&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have a channel to be able to send and receive messages for the server, as well as a channel for the responses to those messages.&lt;/p&gt;

&lt;p&gt;Our server will start in the background with the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;msgs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resps&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We respond to get requests by sending back the current state, and to set requests by setting the concurrent state. Since only one thread is in control of the state, interactions with the server are free from data races.&lt;/p&gt;

&lt;p&gt;The basic operations with our server look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;msgs&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resps&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;msgs&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now with those basic operations, we can define the following function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&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;This function simply increments the state.&lt;/p&gt;

&lt;p&gt;In our main function, we can do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;newServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;200&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will perform 200 increment operations, leaving the state at 200, as expected.&lt;/p&gt;

&lt;p&gt;But if we start sharing these operations between threads, we'll notice a race condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;newServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;increment&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;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We would expect to see 200, as before, but in practice we'll see a smaller number. This is a race condition. This happens because 2 threads can get the same value before setting the next one, and then both will set the same value, leading to 2 calls to increment only performing a single increment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple fixes are not enough
&lt;/h2&gt;

&lt;p&gt;What we want is an &lt;em&gt;atomic operation&lt;/em&gt;. In this case an atomic increment.&lt;br&gt;
An atomic increment would mean that each increment happens as a single step,and thus prevent two concurrent increments from only leading to a single operation.&lt;/p&gt;

&lt;p&gt;We could add such an operation to our server with an additional message,&lt;br&gt;
let's say with id &lt;code&gt;2&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The problem is that the following function still wouldn't be atomic:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;doubleIncrement&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;increment&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;No matter what set of atomic operations our server provides for its state, we can't simply perform multiple operations in sequence, in an unsynchronized manner, and expect the result to also be atomic.&lt;/p&gt;

&lt;p&gt;The full code for this example can be found here:&lt;br&gt;
&lt;a href="https://play.golang.org/p/995MLEiqIVV"&gt;https://play.golang.org/p/995MLEiqIVV&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Data races should not be conflated with race conditions. Just because&lt;br&gt;
a data structure provides data race free operations, doesn't mean that race conditions can't happen with that data structure. Furthermore, sequencing atomic operations does not yield an atomic operations.&lt;/p&gt;

&lt;p&gt;Designing concurrent programs without bugs is not trivial, and becomes&lt;br&gt;
even more complicated once you start working with multiple computers.&lt;/p&gt;

</description>
      <category>go</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
