null<\/code> returns with something meaningful.<\/p>\nNormally, I would remove null<\/code>s by searching for the offending return statement, fixing it to do something meaningful (and if I don’t know what meaningful means in this place, throw an exception), then find usages of that method in the workspace and remove any (now defunct) null checks.<\/p>\nEnter the checked exception as a refactoring tool<\/strong>!<\/p>\n
\npublic Result someMethod() {
\n Result result = someMethodIDontControl();
\n if(null == result) {return null;} \/\/ Actually, this is really crap because in reality, I'd just return result, but bear with me!
\n return result;
\n}
\n<\/code><\/p>\nbecomes<\/p>\n
\npublic Result someMethod() throws IUsedToReturnNullPleaseFixMe {
\n Result result = someMethodIDontControl();
\n if(null == result) {throw new IUsedToReturnNullPleaseFixMe();} \/\/ This is temporary. The right thing _might_ be an exception, but we'll work that out later
\n return result;
\n}
\n<\/code><\/p>\nNow we can lean on the compiler<\/em> to find all the places that call this method. We can strip out any null checks (that are now unnecessary), look to see if there is something meaningful that we should be doing, and then add throws IUsedToReturnNullPleaseFixMe<\/code> to the method we’ve just fixed. When we’ve propagated our exception to the top level, we’re done. What I would do at this point is probably delete the exception class, fix all the resulting compile errors by removing the throws clauses, until finally the last compilation error is the original throw new IUsedToReturnNullPleaseFixMe();<\/code> which I can now replace with something meaningful (e.g. throw a meaningful exception, return a meaningful NullResult, convert to a collection and return emptySet() etc)<\/p>\n","protected":false},"excerpt":{"rendered":"Those of you who have followed my blog, been subjected to it on a client site or have talked to me about programming will know that I have a few issues with checked exceptions. Well, this morning I had an idea of a potentially powerful use of checked exceptions (if only temporarily). The same people […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/272"}],"collection":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":3,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/272\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}