<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Pear Programming</title>
    <description>Writings about softwares.
</description>
    <link>https://aaronackerman.com/</link>
    <atom:link href="https://aaronackerman.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Mon, 28 Jan 2019 16:32:19 +0000</pubDate>
    <lastBuildDate>Mon, 28 Jan 2019 16:32:19 +0000</lastBuildDate>
    <generator>Jekyll v3.7.4</generator>
    
      <item>
        <title>Hack the Gap 2019</title>
        <description>&lt;p&gt;I was able to volunteer for Hack the Gap 2019, January 26th-27th, and it was amazing. I can’t imagine how much work it is to put on an event like this and it went as smoothly as I could have imagined. I applaud all of the organizers for putting in so much time and effort to make this work.
I personally did very little. I saw a tweet go out a few weeks prior to the event asking for volunteers and mentors and I signed up to help.&lt;/p&gt;

&lt;p&gt;Initially I had a morning shift that was just 3 hours volunteering, but I asked for another shift to allow for time to mentor participants. On Saturday morning I was doing my best to greet people on the way into the building, “Hello, good morning! You can sign in at the desk and then take the elevator up to floor 5.” After my greeting duties I spent time helping the breakfast and coffee vendors bring everything up to the event floor and work with several others to setup breakfast for all of the participants. After breakfast setup, I waited to make sure all of the participants had been through the line before getting breakfast for myself and sitting down for introductions and announcements.&lt;/p&gt;

&lt;p&gt;Introductions and announcements led to raffles for prizes including books, memberships to coworking spaces, and tickets to a Dessa concert. After intros and raffles the organizers led into forming teams for hacking on projects. The team formation format was similar to the ‘Open Spaces’ format that I’ve participated in at DevOpsDays conferences, however ‘Open Spaces’ is focused on discussion and not hacking. Participants formed a line, and had 30 seconds to pitch the project that they wanted to work on with other people. Then there was a round of people showing interest in the different projects to filter out projects that wouldn’t have enough people or wouldn’t have a team with diverse skillsets to be able to build something in two days of work. The projects that had enough interest and a team with diverse skillsets formed up, were assigned an area for working together, and got to hacking. I was scheduled to volunteer Saturday morning from 7am to 10am, and it was about 10:15am when teams were getting settled into their areas. I left from my morning shift to spend some time at home.&lt;/p&gt;

&lt;p&gt;In the afternoon, I came back for my mentoring shift from 5pm to 10pm on Saturday. There was dinner shortly after I showed up, and I introduced myself to the other mentors and volunteers that were helping out on Saturday night. From the perspective of a mentor, things mostly ran themselves on Saturday night and the teams were mostly self-sufficient. The most common problems were teams having issues with Git force pushes, JavaScript syntax, and installing Node.js projects on Windows. I was able to help with about 4-5 small problems while I was there, which may not seem like a lot, but it speaks to the ability of the participants to work together to solve problems as a team.&lt;/p&gt;

&lt;p&gt;At 10pm I was exhausted and it was time for me to leave. I was busy on Sunday so I didn’t get to attend the demos in person, but I saw plenty of  posts online and the projects looked great. My congratulations go out to the all of the participants. I had a lot of fun meeting everyone and getting to help out in a small way and I would volunteer more of my time next year.&lt;/p&gt;
</description>
        <pubDate>Mon, 28 Jan 2019 00:00:00 +0000</pubDate>
        <link>https://aaronackerman.com/2019/01/28/hack-the-gap-2019.html</link>
        <guid isPermaLink="true">https://aaronackerman.com/2019/01/28/hack-the-gap-2019.html</guid>
        
        
      </item>
    
      <item>
        <title>Memorizing</title>
        <description>&lt;p&gt;Memorizing syntax and APIs will make you a faster software developer, but not better or smarter.&lt;/p&gt;
</description>
        <pubDate>Tue, 24 Apr 2018 00:00:00 +0000</pubDate>
        <link>https://aaronackerman.com/2018/04/24/memorizing.html</link>
        <guid isPermaLink="true">https://aaronackerman.com/2018/04/24/memorizing.html</guid>
        
        
      </item>
    
      <item>
        <title>Interview race conditions</title>
        <description>&lt;p&gt;I recently asked an interviewee a question about APIs. The question was along the lines of, “Explain how you would ensure that a signup API allows only unique emails.” The interviewee’s answer was essentially, “check if the email exists before allowing the user to signup.” In many cases this would be fine. The user would sign up and after the record is created the API would check if the users exists and prevent other users from signing up with a duplicate email.&lt;/p&gt;

&lt;p&gt;However, if there are multiple ways to sign up that duplication check has to be performed everywhere, if one is missed there could be duplicate emails in the database. Aside from this, to simply “check if the email exists before allowing the user to signup” allows for a race condition.&lt;/p&gt;

&lt;p&gt;It could be the case that the form was improperly implemented and allows the user to click sign up multiple times while the user is being signed up. In this case there could be multiple requests in-flight at the same time. Each request to the API could check whether the email for the user is unique, and each one receives the same answer, that the email doesn’t exist yet. At this point each request moves along with creating the user and now multiple copies of the same user exist in the database 😱.&lt;/p&gt;

&lt;p&gt;The interviewee wasn’t aware of unique database constraints in SQL databases. Adding a unique constraint to the column containing the users email will ensure that only unique emails can signup, it would also be prudent to store all of the emails in lowercase characters.&lt;/p&gt;

&lt;p&gt;These are subtle software problems, that assuredly exist in many different APIs.&lt;/p&gt;

&lt;p&gt;In this instance the solution that I outlined is a simple one and this problem can be avoided.&lt;/p&gt;
</description>
        <pubDate>Mon, 11 Sep 2017 00:00:00 +0000</pubDate>
        <link>https://aaronackerman.com/2017/09/11/simple-race-conditions.html</link>
        <guid isPermaLink="true">https://aaronackerman.com/2017/09/11/simple-race-conditions.html</guid>
        
        
      </item>
    
      <item>
        <title>React, Qualitatively Better</title>
        <description>&lt;p&gt;React is a pretty great JavaScript library, it’s not a full framework, but it’s an extremely coherent view layer for a web application.&lt;/p&gt;

&lt;p&gt;The Virtual DOM is an implementation detail, it allows React to re-render quickly, but this initially differentiating feature may turn into a checkmark if/when all frameworks and view layer libraries implement updates in this fashion. It doesn’t make React special in terms of the DOM. But in terms of providing an abstraction for rendering, the Virtual DOM is huge, React Native and using React to render into HTML5 canvas prove this completely.&lt;/p&gt;

&lt;p&gt;React also feels more like server-side rendering than traditional JavaScript client side rendering. It’s a very simple model to reason about, ignore the state of the DOM, keep the state in JavaScript, keeping state to a minimum and let React sort out writing to the DOM. A lot of problems come up when state is kept in the DOM in the form of data attributes and css classes, it forces a lot of manual bookkeeping to deal with updating the DOM, React allows developers to ignore an entire swath of problems.&lt;/p&gt;

&lt;p&gt;The API surface area of React is small in comparison to other libraries, it can take much less time to be effective using React. This is a really big deal. The surface area of other well-known JavaScript frameworks is often much larger and more extensive than React. But this smaller API surface doesn’t make React less powerful. Fewer features does not mean less powerful, it simply means the features must be pulled in from elsewhere, like utility libraries such as Lodash.&lt;/p&gt;

&lt;p&gt;JSX is simple sugar to allow consumers to write code that looks more like an HTML tag tree than the nested function calls that are output by the compiler. Every &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;div&amp;gt;&lt;/code&gt; is transformed into &lt;code class=&quot;highlighter-rouge&quot;&gt;React.createElement('div')&lt;/code&gt;. Functional composability is huge in React, refactoring a component into multiple components is usually very straight forward function extraction.&lt;/p&gt;

&lt;p&gt;Early warnings are also a pretty big deal when it comes to React. When I use &lt;code class=&quot;highlighter-rouge&quot;&gt;class=&lt;/code&gt; on a React component, console warning messages will be logged asking me if I really meant &lt;code class=&quot;highlighter-rouge&quot;&gt;className=&lt;/code&gt;, and it’s right, I did mean &lt;code class=&quot;highlighter-rouge&quot;&gt;className&lt;/code&gt; because of specific restrictions in React about HTML properties vs attributes. &lt;a href=&quot;https://github.com/rackt/react-a11y&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;react-a11y&lt;/code&gt;&lt;/a&gt; from &lt;a href=&quot;https://twitter.com/ryanflorence&quot;&gt;@ryanflorence&lt;/a&gt; provides messages in the same vein as the main React library offering details about accessibility problems with returned spec for the DOM.&lt;/p&gt;

&lt;p&gt;React.PropTypes offers development checking around properties passed through components. Warning in development when values passed through components do not match expectations. The checking is intended to be for development only so checking is only informational.&lt;/p&gt;

&lt;p&gt;The flexibility of React is understated or not well understood without a good familiarity with React. The interoperability with existing tools is extremely important. React can work with any major library out there, for better or for worse.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ryanflorence/reactconf-2015-HYPE/blob/master/demos/04-dom-lib-interop/app.js&quot;&gt;React with jQuery UI&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gist.github.com/ryanflorence/3c4597c1e95b3fd3c9e1&quot;&gt;React with Ember&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.thomasboyt.com/2013/12/17/using-reactjs-as-a-backbone-view.html&quot;&gt;React with Backbone&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://nicolashery.com/integrating-d3js-visualizations-in-a-react-app/&quot;&gt;React with D3&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/davidchang/ngReact&quot;&gt;React with Angular&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Izzimach/react-three&quot;&gt;React with three.js&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://engineering.flipboard.com/2015/02/mobile-web/&quot;&gt;React with Canvas&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=KVZ-P-ZI6W4&quot;&gt;React with Native 1&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=7rDsRXj9-cU&quot;&gt;React with Native 2&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The benefits of React are great, it has it’s downsides as many frameworks do. Performance for initial render will never be as good as hand writing DOM manipulation.&lt;/p&gt;

&lt;p&gt;React is greater than the sum of it’s parts. It helps to guide you into the pit of success.&lt;/p&gt;
</description>
        <pubDate>Sun, 01 Mar 2015 00:00:00 +0000</pubDate>
        <link>https://aaronackerman.com/2015/03/01/react-qualitatively-better.html</link>
        <guid isPermaLink="true">https://aaronackerman.com/2015/03/01/react-qualitatively-better.html</guid>
        
        
      </item>
    
      <item>
        <title>Pull Request Review</title>
        <description>&lt;p&gt;When developing software with other people, code review is important. It’s a sign that a project or part of a project is nearing completion or a bug is nearly fixed. Code review also allows feedback from other contributors. Opening a pull request can be a learning opportunity for both the implementer and the reviewers.&lt;/p&gt;

&lt;p&gt;In open source, a complex pull request should come with an upfront discussion one of the maintainers. The maintainers may have no interest in maintaining the changes you intend to make so it’s best to not waste valuable time if the work is unlikely to get merged. It’s worth the time to have a conversation.&lt;/p&gt;

&lt;p&gt;During code review, when I see syntax or a construct with which I am unfamiliar I make a point to ask a question, “Is this correct?”, “How does this syntax work?”, “Is there a clearer way to express this?”. Usually I can learn something from the answer. Avoid finger pointing and using the word ‘you’. “Why did you do it this way,” would be an example of a type of finger pointing to avoid.&lt;/p&gt;

&lt;p&gt;If I understand the implementation but I have an idea for how it could be expressed in clearer terms I will offer a suggestion for a fix including an implementation, possibly in pseudo-code.&lt;/p&gt;

&lt;p&gt;If indentation is off, I assume the author didn’t see the mistake, and comment where the fix should be made. I like to ask a question like “Would you mind changing the formatting to something like this?” and follow up with a suggested implementation. If three or four lines are hard to read due to formatting or a line is too long, responding with the exact suggested fix is much better than responding with “Fix formatting here.”&lt;/p&gt;

&lt;p&gt;I always hope that I don’t have to make nitpicking comments about formatting. I make a habit of reviewing diffs before committing, and checking if there are any obvious formatting mistakes. When I create a pull request I self-review all of the changes, and hope I can fix them before another person reviews, comments, and my inbox is flooded. The self-review is important to me because I will make changes over the course of several commits and all of the commits together may not be cohesive.&lt;/p&gt;

&lt;h2 id=&quot;the-learning-part&quot;&gt;The learning part&lt;/h2&gt;

&lt;p&gt;When reviewing pull requests, sometimes I will see this commit message, “Made changes based on PR feedback.” Those words are hard to read. They can convey several messages including, “I don’t agree with your feedback”, and “I don’t understand what you’re talking about but I’ll make the changes”. It’s very unfortunate for me when I see those words. It tells me the writer wasn’t able to create a meaningful commit message because they didn’t understand the changes or why they might be necessary. It also means I wasn’t able to teach them or they are unwilling to learn.&lt;/p&gt;

&lt;h2 id=&quot;humility&quot;&gt;Humility&lt;/h2&gt;

&lt;p&gt;Code review is about technical feedback and constructive criticism, does it work, can you understand it today, will the changes be easy to understand tomorrow.&lt;/p&gt;

&lt;h2 id=&quot;things-to-avoid-in-pull-requests&quot;&gt;Things to avoid in pull requests&lt;/h2&gt;

&lt;p&gt;These are simply my personal preferences, I like to stick to taking care of business.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;+1’s&lt;/li&gt;
  &lt;li&gt;Joke responses to feedback&lt;/li&gt;
  &lt;li&gt;Joke comments on changes&lt;/li&gt;
  &lt;li&gt;Ignoring feedback&lt;/li&gt;
  &lt;li&gt;Ignoring tests&lt;/li&gt;
  &lt;li&gt;Destructive criticism&lt;/li&gt;
  &lt;li&gt;Demands instead of questions&lt;/li&gt;
  &lt;li&gt;Finger pointing, using the word ‘you’&lt;/li&gt;
  &lt;li&gt;Taking feedback personally&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 02 Feb 2015 00:00:00 +0000</pubDate>
        <link>https://aaronackerman.com/2015/02/02/pull-request-review.html</link>
        <guid isPermaLink="true">https://aaronackerman.com/2015/02/02/pull-request-review.html</guid>
        
        
      </item>
    
      <item>
        <title>Understanding NaN Values</title>
        <description>&lt;p&gt;A co-worker recently had issues with &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values in JavaScript. &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values are &lt;a href=&quot;http://en.wikipedia.org/wiki/IEEE_floating_point&quot;&gt;IEEE 754 Floating-Point&lt;/a&gt; number values. These values result from an undefined or unrepresentable value in floating point calculations. &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values &lt;strong&gt;are&lt;/strong&gt; number values despite everything your brain might tell you when you read &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; as ‘Not a Number’. In the case of &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values it’s useful to forget all logic and intuition, and memorize the facts.&lt;/p&gt;

&lt;p&gt;JavaScript reports &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values as number types, this is correct per the ECMA-262 and IEEE 754 specs.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;k&quot;&gt;typeof&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// 'number'&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// '[object Number]'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In JavaScript a &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; value is the only type of value that is not reflexive. That’s one way to identify a &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; value. &lt;code class=&quot;highlighter-rouge&quot;&gt;x !== x&lt;/code&gt; where &lt;code class=&quot;highlighter-rouge&quot;&gt;x&lt;/code&gt; is a &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; value will evaluate to &lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt;. The global &lt;code class=&quot;highlighter-rouge&quot;&gt;isNaN&lt;/code&gt; method can be used to identify &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values. However, the &lt;code class=&quot;highlighter-rouge&quot;&gt;isNaN&lt;/code&gt; method doesn’t handle converting input to a number. It has a set of gotchas.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({});&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'NaN'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'str'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]);&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;When certain inputs are passed to the &lt;code class=&quot;highlighter-rouge&quot;&gt;isNaN&lt;/code&gt; method the behavior is less than ideal. Several false positives are returned. Both &lt;a href=&quot;http://underscorejs.org/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Underscore&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://lodash.com/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Lo-Dash&lt;/code&gt;&lt;/a&gt; handle the shortcomings of the &lt;code class=&quot;highlighter-rouge&quot;&gt;isNaN&lt;/code&gt; method by checking if the value is a number.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({});&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'NaN'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'str'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]);&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As written above &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values are number values so &lt;code class=&quot;highlighter-rouge&quot;&gt;_.isNumber(NaN)&lt;/code&gt; correctly returns &lt;code class=&quot;highlighter-rouge&quot;&gt;true&lt;/code&gt; for both &lt;a href=&quot;http://underscorejs.org/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Underscore&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://lodash.com/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Lo-Dash&lt;/code&gt;&lt;/a&gt;. Both libraries have had issues filed for incorrect behavior, but the behavior is correct and the answer is usually to point consumers to the &lt;code class=&quot;highlighter-rouge&quot;&gt;_.isFinite&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Issues also arise when dealing with builtin methods such as &lt;code class=&quot;highlighter-rouge&quot;&gt;Array.prototype.indexOf&lt;/code&gt;. &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values are unable to be found without special handling.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;indexOf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// -1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;a href=&quot;https://people.mozilla.org/~jorendorff/es6-draft.html&quot;&gt;ES6 draft&lt;/a&gt; specifies a solution to the global &lt;code class=&quot;highlighter-rouge&quot;&gt;isNaN&lt;/code&gt; method with a new &lt;code class=&quot;highlighter-rouge&quot;&gt;Number.isNaN&lt;/code&gt; method. &lt;code class=&quot;highlighter-rouge&quot;&gt;Number.isNaN&lt;/code&gt; returns the same results &lt;a href=&quot;http://underscorejs.org/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Underscore&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;https://lodash.com/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Lo-Dash&lt;/code&gt;&lt;/a&gt; return.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nb&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({});&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'NaN'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'str'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'1'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([]);&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;a href=&quot;https://people.mozilla.org/~jorendorff/es6-draft.html&quot;&gt;ES6 draft&lt;/a&gt; also specifies &lt;code class=&quot;highlighter-rouge&quot;&gt;Object.is&lt;/code&gt;, offering a better way to determine if two values are equal over the strict equality operator, triple equals. &lt;code class=&quot;highlighter-rouge&quot;&gt;Object.is&lt;/code&gt; works as the strict equality operator with two exceptions: &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values, and positive and negative zero.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;When my co-worker was having issues and asking question about &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values he called ‘wat’ on JavaScript. I took it upon myself to set the record straight, but realized everyone who encounters &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values must learn about these problems, hence this post. It’s important to understand this behavior is &lt;strong&gt;not&lt;/strong&gt; specific to JavaScript. It is specific to all languages that use &lt;a href=&quot;http://en.wikipedia.org/wiki/IEEE_floating_point&quot;&gt;IEEE 754 Floating-Point&lt;/a&gt; number calculations.&lt;/p&gt;

&lt;p&gt;Ruby has the following behavior with regards to &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;NAN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;is_a?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Float&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# true&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;NAN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;nan?&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;# true&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;nan?&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;# true&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;NAN&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Float&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;NAN&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# false&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;# false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Java has the following behavior with regards to &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kt&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;NaN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;isNaN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// true&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The behavior across Ruby, Java, and JavaScript is essentially the same. Every single consumer of these languages must eventually learn the rules of &lt;code class=&quot;highlighter-rouge&quot;&gt;NaN&lt;/code&gt; values. I hope consumers of these languages are now able to understand the issues at hand and deal with them thoughtfully.&lt;/p&gt;
</description>
        <pubDate>Wed, 07 Jan 2015 00:00:00 +0000</pubDate>
        <link>https://aaronackerman.com/2015/01/07/understanding-nan-values.html</link>
        <guid isPermaLink="true">https://aaronackerman.com/2015/01/07/understanding-nan-values.html</guid>
        
        
      </item>
    
  </channel>
</rss>
