IsNull() vs. IsDefined() For ColdFusion 9 Scope Detection
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:
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.
Want to use code from this post? Check out the license.
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! :)
@Cory,
3 cats and 2 dogs - that reminds me of my childhood :) I grew up with a ton of animals.