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,238 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 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools