ColdFusion Doesn't Need NULLs

I recently read Joe Rinehart's great blog on OO features in ColdFusion. His blog is, largely, a response to some discussions on the CFCDev list. In short, he feels that many of the OO features that some people are requesting be added to ColdFusion aren't a good idea.

The argument that I found the most interesting (on Joe's blog and the CFCDev list) was regarding NULLs.

The Problem

He defines the problem better than I can (reading his post first if you have time). Essentially, the problem is that having a returntype (say "numeric") for a method is helpful. With that help, however, comes a challenge. It isn't possible to return no value. With a returntype of "string", you could return an empty string. With a returntype such as "numeric", however, returning zero isn't the same. In fact, an empty string might not be the same as no value either.

Many (typed) languages (including SQL) handle this with a NULL. Basically, a NULL is an indication of no value at all.

The argument made by many is that NULL is needed so that we can take advantage of returntype and still return no value from a method.

My Proposal

I have run into this issue myself. Of course, adding a NULL to ColdFusion (or any typeless language) seems like a pretty major change.

As it happens, ColdFusion already has its own conception of this. A variable can be undefined (that is non-existent). A component with a returntype of "any" or "void" need not execute a cfreturn (and thereby return a value).

I would like an optional attribute (maybe "allowVoid"), which would allow a method with any other returntype to not execute a cfreturn. The attribute would have to default to false in order to maintain backward compatibility.

It is already possible to have the following code leave "foo" undefined (if myMethod doesn't execute a cfreturn). This change would only mean that myMethod could have a returntype of something other than "any" or "void" (if allowVoid is set to "true").

<cfset foo = comp.myMethod()>

This nice thing about this approach is that it plays nicely with how things work in ColdFusion already. It doesn't add a concept of NULL, which is foreign to ColdFusion. It does, however, add the ability to return nothing from a component without giving up the type-checking benefits of returntype.

I might also add that the problem already doesn't exist in cfargument as you can already set required="false" in cfargument.

Well, there you have it. If you have any thoughts on my proposal, I would love to hear them. If you have any thoughts on other OO features or anything else discussed in Joe's blog, please post them there to keep that discussion coherent.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Not a bad idea, though I think the use of "allowVoid" could be confusing, especially since you can set the return type to void.

people might think you need to set allowVoid to true if you want to set the returntype to void.

i think allowNull (but for the same effect) might be a better name. I think Ben Rogers, in the CFCDev list, had a similar approach:


I think it's a fine suggestion. However, I still think a function should
have to declare that it may return null values:

< cffunction name="getCustomer" returntype="Customer" allownull="yes">
...
< /cffunction>

AllowNull should be defaulted to "no". That would be backwards compatible
with the existing ColdFusion code which is blissfully unaware of null
values.


I hope the html encoding doesn't mess that part up :O)
# Posted By | 5/18/05 5:27 PM
There are lots of Java functions that return NULL. IsDefined() will return true but when the return value is accessed, BOOM. This can only be handled with a cftry/cfcatch, which is a PITA.
# Posted By jim | 3/19/06 6:59 PM
CF already has nulls. They are not represented by the literal 'null', rather as an empty string literal. These are not null strings however. A null string is still an object with all the members a String object has. However, in CF, the empty string literal does not represent an object of class String with String's members. Just try passing it to DateCompare--you'll throw a null pointer exception.
# Posted By Jon Wolski | 6/7/06 4:50 PM
Jon,

Those aren't nulls, they are empty strings. I can't tell the difference, for example, between a database field that has an empty string and one that has no value at all.

The problem is that I can store a null value in a numeric or boolean field, but I can't accept an empty string for an argument with type="numeric" or type="boolean". I can't return an empty string for a method with returntype="numeric" or returntype="boolean".
# Posted By | 6/7/06 10:38 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.