> I have a fairly major concern with this.  Why is isDefined() returning
> true?  It should be returning false, as the scope "variables" has been
> specified as part of the whole variable name.

IsDefined returns true, because you're NOT specifying a variable in the
'variables' scope, you're specifying a variable that can be referenced by
"variables.test", which is subtly different.  CF will happily let you assign
a variable named "variables.test", within the 'variables' scope, as my code
demonstrates, and because 'variables' is an implicit scope, you can still
reference it as just "variables.test".

structKeyExists(variables, "test") does something like this:
1) if 'variables' is a scope name, store and go to 3
2) if 'variables' is present in any implicit scope, store and go to 3
3) if the stored item is not a scope or a struct throw an error
4) return whether the struct/scope has a "test" element

isDefined("variables.test"), on the other hand, does something roughly
approximating this:
1) structKeyExists(variables, "test")
2) structKeyExists(variables, "variables.test")
3) structKeyExists(variables.variables, "test")
4) structKeyExists(url, "variables.test")
5) structKeyExists(url.variables, "test")
6) structKeyExists(form, "variables.test")
7) structKeyExists(form.variables, "test")
8) structKeyExists(cgi, "variables.test")
9) structKeyExists(cgi.variables, "test")

I obviusly have no idea about the actual implementation of either function
(I don't have the CF source), but from what I've witnessed and discussed
with others (Sean, notably), both of those should be fairly accurate
representations of the algorithm used.  If they're totally off base, that's
my fault, not anyone else's, as it would have been me drawing the wrong
conclusions, not them lying to me.  

Cheers,
barneyb

> -----Original Message-----
> From: Stephen Moretti (cfmaster) [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, July 13, 2004 5:16 AM
> To: CF-Talk
> Subject: Re: StructKeyExists() vs IsDefined()
>
> Barney Boisvert wrote:
>
> > <cfset variables.variables.test = "" />
> > <cfoutput>
> > #isDefined("variables.test")#<br />
> > #structKeyExists(variables, "test")#
> > </cfoutput>
> >
> > isDefined() returns true, structKeyExists() returns false.
>
>
> I have a fairly major concern with this.  Why is isDefined()
> returning
> true?  It should be returning false, as the scope "variables"
> has been
> specified as part of the whole variable name.
>
> The whole point of scoping variables is to stop functions like
> isDefined() going off and doing its own thing.  Its should only be
> checking to see if test exists in the variables scope and not whether
> "variables.test" exists in any scope, including a "null" scope.
>
> This would means that scoping is not really taken into account by
> coldfusion, other by pure luck that functions like
> isDefined() look in
> the null scope first.
>
> If isDefined() is not taking scopes into account, does it keep going
> until its checked all scopes or does it stop searching when
> it hits the
> first occurance?
>
> If I'm using StructKeyExists() I need to check whether the
> struct that
> I'm checking in exists before I check for the keys existance as it
> throws an exception if the struct doesn't exist, so I would have;
>
> <cfif IsDefined("variables") AND
> StructKeyExists(variables.variables,"test")>
>
> or even;
>
> <cfif IsDefined("variables.variable") AND
> StructKeyExists(variables.variables,"test")>
>
> or;
>
> <cfif IsDefined("variables") AND
> StructKeyExists(variables,"variables")
> AND StructKeyExists(variables.variables,"test")>
>
> Ok - so this example is exacerbated by the improper use of a
> scope name
> as a variable name, but hopefully you see the point that because you
> have to use isDefined() to check to see if your struct exists
> before you
> go looking for keys in the struct you are already scanning
> through all
> the scopes to find your struct.  The above examples are probably made
> even worse by the fact that you should check to make sure that the
> variable you are about to treat as a struct is actually a
> struct and not
> a string, to avoid another exception. (Incidently, found an
> instance of
> a string being referenced as struct in the Spectra source)
>
> Anyway, all of this brings me back to :
> Given that I've specified a scoped variable, why is isDefined() still
> scanning when it should just be checking one instance? [One
> for Sean me
> thinks ;) ]
>
> Given all the additional work that surrounds using StructKeyExists(),
> does this function actually give you any benefit over
> isDefined() when
> used in the context of  "does this variable exist?"? I think
> the answer
> really is no.  When used in the context of "I know this
> variable exists
> and is a struct, does it have this key?" then obviously yes it does.
>
> > Do <cfset variables['foo.bar'] = 42 /> and then try
> > isDefined('foo.bar') vs structKeyExists(foo,'bar') (the latter will
> > fail *before* calling structKeyExists() because foo is not
> defined as
> > a variable).
>
> Ahem... isn't this wrong, just plain wrong.....
> I understand what is happening here, but it means that
> variables.foo.bar
> doesn't exist either, that "foo.bar" can only be referenced by
> variables['foo.bar'] and CF is letting people create variables with
> invalid characters in their names.  It all too scary to
> contemplate really.
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to