<story><title>The Logical Disaster of Null</title><url>https://rob.conery.io/2018/05/01/the-logical-disaster-of-null/</url></story><parent_chain><item><author>jasode</author><text>&lt;i&gt;&amp;gt;I’m not sure Null should have a place in programming. &lt;/i&gt;&lt;p&gt;The essay is conflating 2 different concepts of &amp;quot;null.&amp;quot;&lt;p&gt;The Tony Hoare quote is about null _references_ which is about &lt;i&gt;aliases&lt;/i&gt; to computer memory. (E.g. the dreaded NPE or null pointer exception.)   He&amp;#x27;s not talking about _logical_ nulls such as &amp;quot;tri-state booleans&amp;quot; or &amp;quot;missing data&amp;quot;.&lt;p&gt;The logic-variant of Null usage for &amp;quot;unknown&amp;quot; &amp;quot;missing&amp;quot; &amp;quot;invalid&amp;quot; values is unavoidable in programming. This is why that null concept shows up repeatedly in different forms such as NaN in floating point, NULL in SQL language, etc.  If you invented a theoretical language without logical Nulls, the users of your language would &lt;i&gt;reinvent nulls&lt;/i&gt; using worse techniques such as homemade structs with an extra boolean &amp;quot;HasValue&amp;quot; field.  E.g.:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  struct NullableInteger {
    int x;
    bool hasvalue;
  }
&lt;/code&gt;&lt;/pre&gt;
The &amp;quot;hasvalue&amp;quot; field becomes a &amp;quot;null by convention&amp;quot;.  Other programmers might not even code a verbose extra boolean variable and instead use (dangerous) sentinel values such as INT_MAX &amp;quot;32767&amp;quot; or negative value such as &amp;quot;-1&amp;quot; as the &amp;quot;pseudo null&amp;quot; to represent missing data.  A lot of old COBOL programs had 99999 as some out-of-range value to represent missing data.   We need nulls in programming languages because they are useful to model real-world (lack of) information.  All those other clunky techniques will reinvent the same kinds of &amp;quot;null&amp;quot; programming errors!&lt;p&gt;The orthogonal aspect is making the &lt;i&gt;type system&lt;/i&gt; more powerfully &lt;i&gt;aware&lt;/i&gt; of nulls so that the compiler sees that possible null conditions were not checked.  A compiler error forces the programmer to put in the &lt;i&gt;explicit&lt;/i&gt; defensive code to &lt;i&gt;handle the possibility of null values&lt;/i&gt;.</text></item></parent_chain><comment><author>paulajohnson</author><text>The point is that option types let you explicitly state which values are nullable, but null references mean that every value is potentially null even if it doesn&amp;#x27;t make sense.</text></comment>
<story><title>The Logical Disaster of Null</title><url>https://rob.conery.io/2018/05/01/the-logical-disaster-of-null/</url></story><parent_chain><item><author>jasode</author><text>&lt;i&gt;&amp;gt;I’m not sure Null should have a place in programming. &lt;/i&gt;&lt;p&gt;The essay is conflating 2 different concepts of &amp;quot;null.&amp;quot;&lt;p&gt;The Tony Hoare quote is about null _references_ which is about &lt;i&gt;aliases&lt;/i&gt; to computer memory. (E.g. the dreaded NPE or null pointer exception.)   He&amp;#x27;s not talking about _logical_ nulls such as &amp;quot;tri-state booleans&amp;quot; or &amp;quot;missing data&amp;quot;.&lt;p&gt;The logic-variant of Null usage for &amp;quot;unknown&amp;quot; &amp;quot;missing&amp;quot; &amp;quot;invalid&amp;quot; values is unavoidable in programming. This is why that null concept shows up repeatedly in different forms such as NaN in floating point, NULL in SQL language, etc.  If you invented a theoretical language without logical Nulls, the users of your language would &lt;i&gt;reinvent nulls&lt;/i&gt; using worse techniques such as homemade structs with an extra boolean &amp;quot;HasValue&amp;quot; field.  E.g.:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  struct NullableInteger {
    int x;
    bool hasvalue;
  }
&lt;/code&gt;&lt;/pre&gt;
The &amp;quot;hasvalue&amp;quot; field becomes a &amp;quot;null by convention&amp;quot;.  Other programmers might not even code a verbose extra boolean variable and instead use (dangerous) sentinel values such as INT_MAX &amp;quot;32767&amp;quot; or negative value such as &amp;quot;-1&amp;quot; as the &amp;quot;pseudo null&amp;quot; to represent missing data.  A lot of old COBOL programs had 99999 as some out-of-range value to represent missing data.   We need nulls in programming languages because they are useful to model real-world (lack of) information.  All those other clunky techniques will reinvent the same kinds of &amp;quot;null&amp;quot; programming errors!&lt;p&gt;The orthogonal aspect is making the &lt;i&gt;type system&lt;/i&gt; more powerfully &lt;i&gt;aware&lt;/i&gt; of nulls so that the compiler sees that possible null conditions were not checked.  A compiler error forces the programmer to put in the &lt;i&gt;explicit&lt;/i&gt; defensive code to &lt;i&gt;handle the possibility of null values&lt;/i&gt;.</text></item></parent_chain><comment><author>tathougies</author><text>&amp;gt;  If you invented a theoretical language without logical Nulls, the users of your language would reinvent nulls using worse techniques such as homemade structs with an extra boolean &amp;quot;HasValue&amp;quot; field.&lt;p&gt;Alternatively, you can only use languages which offer actual tagged unions, and avoid the ridiculousness of these &amp;#x27;hasvalue&amp;#x27; fields.</text></comment>