Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

But I have become passionate about making software.

In ColdFusion 9, what isNull() and what is it good for

February 24, 2010

ColdFusion 9 brings us a new function called isNull.  But, you may say ColdFusion doesn't support null's and you would still be mostly right.  As of ColdFusion 9 certain objects can be null.  Sound tricky?  Here are a couple handy uses for isNull.

Working with caches
When using CacheGet if the id you pass does not exist then the returned object will be null.  Its therefore best to check after getting the data:

myData = cacheGet( "coolCacheName" );
if ( isNull(myData) ) {
      //do something
}
 
Working with entityLoad or entityLoadByPK
When using either entityLoad with unique=true or entityLoadByPK which is required to change values of an existing object (in db terms: a row you want to do an update on) if the id you pass in does not return an object then you get a null object back.
 
a = entityLoad( "ART", {artID=3000}, true);
if ( ! isNull(a) ) {
      //some sort of logic or error handling
}


Checking to see if a variable exists
While not an object its possible to check for an undefined variable with isNull.  (Queue debate on which is quicker/better for determining if a variable exists isNull, isDefined or structKeyExists!)

variables.name = "Sam";
if ( isNull( variables.nane ) ) {
      //there's a typo!
}

isNull is a minor addition but one that proves quite useful when working with caching and/or ORM.


No Comments

Some comments have been lost over the years due to moving hosts.

More


More blog entries that I have written.

Code coloring by PRISM.