<meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/> <meta name='google-adsense-platform-domain' content='blogspot.com'/> <!-- data-ad-client=ca-pub-4320963827702032 --> <!-- --><style type="text/css">@import url(https://www.blogger.com/static/v1/v-css/navbar/3334278262-classic.css); div.b-mobile {display:none;} </style> </head><body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d7256432\x26blogName\x3dThe+Frustrated+Programmer\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLACK\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttps://frustratedprogrammer.blogspot.com/search\x26blogLocale\x3den_US\x26v\x3d2\x26homepageUrl\x3dhttp://frustratedprogrammer.blogspot.com/\x26vt\x3d4213664491834773269', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>
| Thursday, August 19, 2004

I was reading about Null Pointer Exceptions today, and finally read up on Nice. They had this to say about NullPointer exceptions:

To prevent [a NullPointerException], a reference must be tested before use. However it is easy to forget to do so. Furthermore, there are references that are never null, or rather that should never be. Testing such references clutters the code.


Nice provides the following solution:
Here obviously comes our motto: all this checking should be done automatically. This is exactly what Nice does. One documents whether a type can hold the null value by simply prefixing it with the ? symbol. Thus: ?String name; is the declaration of String variable that might be null. To get its length, one must write int len = (name == null) ? 0 : name.length();. Calling name.length() directly is a type error. It would only be possible if name was defined as String name = "Some non-null string";


I agree with Nice's statement of the problem, but I'd like to present a different solution. If the problem is that people forget to write null checks, why not force it with the language? Something like this:
The following Java code:

public execute( Form form ) {
String type = form.getType();
if( type != null ) {
doSomething( type );
} else {
throw new Exception("type is required");
}
}

Could be replaced with:

public execute( Form form ) {
form.getType() : String type {
doSomething( type );
} null {
throw new Exception("type is required");
}
}

The idea is this: If form.getType() returned null, the block of code following the method invocation would not execute. Otherwise, 'type' is assigned the return value from form.getType() and its scope is only visible in that block of code. After thinking about it a little I came up with the following rules:

  • Assignment can only be used for new (object instantiation) or primitives.

  • A method call can be suffixed with : [type] [variable name] { [statements] } Statements will only be executed if the return value is not null.

  • null could also be used as a keyword following this block of statments, similar to 'finally'. Statements following null will only be executed is the return value is null. This block is optional, similar to 'finally'.

  • Method calls with assigments can be grouped together and the block of code following them will only be execute if all return values are not null.

  • for and while loops could be modified to use a similar syntax.

  • null can still be returned from methods or passed to parameters.



Its far from perfect. Specifically, I don't know how to handle class and instance variables or parameters. But I think it might have some promise.