Question

I am using ColdFusion 9.0.1

I recently read that in ColdFusion 9, it is now advised to use isNull() and not use isDefined().

I didn't find much info on this at all around the web.

Is there any advantage to using one or the other in ColdFusion 9?

Was it helpful?

Solution

I know that in general people have been moving away from it for some time. The main use for it was to determine whether a variable existed in a particular scope, but structKeyExists() is more precise for that. IsDefined() works very hard to find any possible instance of the variable you've asked for. (Sean Corfield had a bit to say about structKeyExists vs. isDefined.)

I've not seen anywhere a recommendation to use isNull() instead of isDefined. Actually, I would have expected isNull to return an error if you give it an undefined variable, but apparently that works.

How do you do dynamic variables, though?

structKeyExists(form,"address_" & i)

You can try array notation...

isNull(form["address_" & i])

...but if i is undefined this throws an error.

OTHER TIPS

No, isDefined() is not deprecated and won't likely be deprecated.

IsNull() is for, as Shawn says, for working with NULLs returned from Java, most specifically for those returned from Hibernate (ORM integration).

StructKeyExist() is more precise than isDefined("") but there is, technically, nothing wrong with using isDefined() and I would question whether or not structKeyExist() would work in every situation. Is every variable within a struct or some sort? I am not sure.

I do not think you need to worry about isDefined() going away anytime soon.

I believe the real reason isNull() was added was to provide a more concrete way to test for Java-related NULLs that are returned from objects, services, etc--whereas isDefined() tests to see whether a variable exists or not.

Two separate functions, really.

Regarding isDefined(varName) my CLIENT.somevarname variable has a null value, and when I try to evaluate it

<cfif isDefined("somevarname") and val(somevarname)> </cfif>

.. it throws an error :

Element somevarnameis undefined in CLIENT

I use to handle it like this to prevent an error when conditionally comparing the value:

   len(trim(CLIENT.somevarname)) eq 0 

I switched to using isNull() and it works better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top