{"id":27,"date":"2016-04-29T14:46:30","date_gmt":"2016-04-29T14:46:30","guid":{"rendered":"http:\/\/www.onux.com\/jspp\/blog\/?p=27"},"modified":"2016-04-29T14:46:30","modified_gmt":"2016-04-29T14:46:30","slug":"optional-types-null-without-nullpointerexception","status":"publish","type":"post","link":"https:\/\/www.onux.com\/jspp\/blog\/optional-types-null-without-nullpointerexception\/","title":{"rendered":"Optional Types: &#8216;null&#8217; without NullPointerException"},"content":{"rendered":"<p>Optional (nullable) types will be coming to JS++. This usually stokes fears of null pointer dereferencing. For example, in Java, this is known as a &#8220;NullPointerException&#8221;.<\/p>\n<p>In fact, Turing Award winner Tony Hoare calls it his billion-dollar mistake:<\/p>\n<blockquote>\n<p>I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn&#8217;t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.<\/p>\n<\/blockquote>\n<p>However, it&#8217;s possible to have <code>null<\/code>s in a language without ever having NullPointerExceptions.<\/p>\n<pre class=\"brush:jspp\">\r\nvoid doStuff(Foo foo) {\r\n    foo.bar(); \/\/ Error, potentially null\r\n}\r\n<\/pre>\n<p>In the above example, <code>foo<\/code> can be either an instance of <code>Foo<\/code> or <code>null<\/code> at runtime. This creates the potential for dereferencing a null pointer. This is an easy fix though &#8211; we just check that <code>foo<\/code> is <em>not<\/em> <code>null<\/code>:<\/p>\n<pre class=\"brush:jspp\">\r\nvoid doStuff(Foo foo) {\r\n    if (foo != null) {\r\n        foo.bar();\r\n    }\r\n}\r\n<\/pre>\n<p>By checking that <code>foo<\/code> is <em>not<\/em> <code>null<\/code>, we can guarantee <code>foo<\/code> is at least an instance of class <code>Foo<\/code> &#8211; thus making it very straightforward for us to check that the <code>bar<\/code> method exists. This is a process known as <em>type refinement<\/em>. What&#8217;s interesting is that the compiler is able to actually ensure you&#8217;ve made the check before you ever run your program. It achieves this by examining the control and data flow. This is cutting-edge research known as flow-sensitive type systems.<\/p>\n<p>In addition, by examining the control and data flow, if we can ascertain that <code>null<\/code> was <em>never<\/em> passed into the <code>doStuff<\/code> function, type refinement would not need to occur, and you would not <em>need<\/em> to check for <code>null<\/code>. Thus, this code &#8211; with no <code>null<\/code> check &#8211; will still pass a compile:<\/p>\n<pre class=\"brush:jspp\">\r\nvoid doStuff(Foo foo) {\r\n    foo.bar();\r\n}\r\n\r\ndoStuff(new Foo());\r\n<\/pre>\n<p>On the other hand, as soon as you introduce <code>null<\/code>, you need to check for <code>null<\/code> too:<\/p>\n<pre class=\"brush:jspp\">\r\n\/\/ Attempt 1\r\nvoid doStuff(Foo foo) {\r\n    foo.bar(); \/\/ Error, 'foo' can be 'null'\r\n}\r\n\r\ndoStuff(new Foo()); \/\/ ok\r\ndoStuff(null);      \/\/ now we need a null check\r\n<\/pre>\n<pre class=\"brush:jspp\">\r\n\/\/ Attempt 2\r\nvoid doStuff(Foo foo) {\r\n    if (foo != null) { \/\/ do the 'null' check so the code compiles\r\n        foo.bar();\r\n    }\r\n}\r\n\r\ndoStuff(new Foo()); \/\/ ok\r\ndoStuff(null);      \/\/ ok\r\n<\/pre>\n<p><code>null<\/code> is a first-class type in the JS++ type system so, combined with JS++&#8217;s sound type system, JS++ is able to categorically determine that you&#8217;ve checked <code>foo<\/code> is non-null without ever executing any code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Optional (nullable) types will be coming to JS++. This usually stokes fears of null pointer dereferencing. For example, in Java, this is known as a &#8220;NullPointerException&#8221;. In fact, Turing Award winner Tony Hoare calls it his billion-dollar mistake: I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At &hellip; <a href=\"https:\/\/www.onux.com\/jspp\/blog\/optional-types-null-without-nullpointerexception\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Optional Types: &#8216;null&#8217; without NullPointerException&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/27"}],"collection":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":11,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions\/38"}],"wp:attachment":[{"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onux.com\/jspp\/blog\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}