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 »
10,640 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »