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

ColdFusion 9

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.

henrylearn2rock said:
 
I've heard before that isNull() will be compiled down to use the same bytecode as structKeyExists(), any truth in this?
 
posted 156 days ago
Add Comment Reply to: this comment OR this thread
 
samfarmer said:
 
@Henry: Interesting. I don't know but I can see where that thinking comes from as they give very similar results. I guess some deeper tests could find out more.
 
posted 155 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Twitter
You should follow me on Twitter here
About Me
I am a 34-year old Web Developer specializing in ColdFusion. I live and work in downtown Washington, DC with my wife and two daughters. Read more About Me

2007 CFeMmy Best Newcommer winner
As voted on by fellow CF Bloggers.