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

Posted December 5, 2009 at 5:20 PM by Ben Nadel

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

Sep 23, 2010 at 9:18 AM // reply »
1 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! :)


Sep 23, 2010 at 2:19 PM // reply »
11,246 Comments

@Cory,

3 cats and 2 dogs - that reminds me of my childhood :) I grew up with a ton of animals.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools