Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFUNITED 2008 (Washington, D.C.) with: Sean Corfield

IsNull() vs. IsDefined() For ColdFusion 9 Scope Detection

By Ben Nadel on
Tags: ColdFusion

I just spent an hour trying to debug a ColdFusion 9 problem when I realized that the root of the problem was the fact that ColdFusion 9's new IsNull() method and the previously existing IsDefined() method work very differently for scope detection. While the built-in ColdFusion scopes might not seem like something you ever need to test for, remember that the FORM scope doesn't actually exist during SOAP-based requests.

To see the difference in scope-detection using both IsNull() and IsDefined(), take a look at this demo:

  • <!---
  • Check to see if the URL and FORM scopes exist using
  • both the new isNull() and the old isDefined() methods.
  • --->
  • <cfset check = {} />
  • <!--- Check for URL scope. --->
  • <cfset check.isUrlNull = isNull( url ) />
  • <cfset check.isUrlDefined = isDefined( "url" ) />
  • <!--- Check for FORM scope. --->
  • <cfset check.isFormNull = isNull( form ) />
  • <cfset check.isFormDefined = isDefined( "form" ) />
  • <!--- Check for scope existence. --->
  • <cfdump
  • var="#check#"
  • label="In-Page Check"
  • />

Here, we are simply checking the URL and FORM scope existence using IsNull() and IsDefined(). When we run the above code, we get the following output:

IsNull() vs. IsDefined() For ColdFusion 9 Scope Detection. 

As you can see, all of them report back YES. Of course, a scope cannot be both NULL and Defined at the same time - that would be like dogs and cats living together - it just can't happen.

From what I have seen and heard before, I believe that IsNull() gets converted to StructKeyExists() at compile time; as such, it would make sense that it requires some sort of scoped-value testing. I think it's probably using whatever the local scope is at run time (ie. local? variables?). Anyway, as much as I love the ColdFusion 9's new IsNull() function, it looks like there are still a few valid times to use the IsDefined() function.

Reader Comments

I experienced this very thing today! Checking to see if a variable is defined in the URL or not, ended up using ISDEFINED() and it works perfectly for me.

BTW, my 3 cats and 2 dogs live just fine together! :)