Hmm...

That's pretty odd looking code.

It looks from that like structKeyExists() will check if the structure is the
name of a scope and not bother to check if it is the name of a structure in
one of the implicit scopes.

That would make it slightly faster those situations where the name of the
struct is the same as the name of a scope and the key doesn't exist, but no
faster in any other case. It also means that structKeyExists() and
isDefined() behave differently which most likely means that there is a bunch
of code out there with nasty bugs waiting to bite someone when they least
expect it.

I would suggest that the behaviour of structKeyExists is buggy because this:

<cfset variables.foo = structNew()>
<cfset variables.foo.test = "bar">

<cfoutput>#structKeyExists(foo,'test')#</cfoutput>

returns "YES", while this:

<cfset variables.form  = structNew()>
<cfset variables.form.test = "bar">

<cfoutput>#structKeyExists(form,'test')#</cfoutput>

returns "NO"

There is nothing in the documentation to support that this should be the
case.

Regardless of what you think of the wisdom of creating a variable with the
same name as a scope (I personally think it's very bad) the behaviour is
likely to lead to bugs IMO.

Personally I think ColdFusion should not permit the creation of a variable
in an implicit scope with the same name as a scope.

That would sort out the issue once and for all. Not to mention sorting out
the weirdness that you can get if you do this:

<cfset request = structNew()>

I've seen more than one question on mailing lists asking why the request
scope is misbehaving in custom tags because of that.

Spike

--------------------------------------------
Stephen Milligan
Code poet for hire
http://www.spike.org.uk

Do you cfeclipse? http://cfeclipse.tigris.org


>-----Original Message-----
>From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED] On Behalf Of Barney Boisvert
>Sent: Monday, July 12, 2004 1:59 PM
>To: CF-Talk
>Subject: RE: StructKeyExists() vs IsDefined()
>
><cfset variables.variables.test = "" />
><cfoutput>
>#isDefined("variables.test")#<br />
>#structKeyExists(variables, "test")#
></cfoutput>
>
>isDefined() returns true, structKeyExists() returns false.
>
>Cheers,
>barneyb
>
>> -----Original Message-----
>> From: Spike [mailto:[EMAIL PROTECTED]
>> Sent: Monday, July 12, 2004 2:41 PM
>> To: CF-Talk
>> Subject: RE: StructKeyExists() vs IsDefined()
>>
>> >What you're testing is whether there's a 'foo' field in some
>> >'test' scope
>> >somewhere.  
>>
>> Correct
>>
>> structKeyExists is using scope escalation to find
>> >"test" in the
>> >form scope (after checking variables, and maybe URL).
>>
>> Also correct
>>
>>  This is as you'd
>> >expect, because you're dereferencing a variable (test), and
>> >that behaves
>> >exactly as if you'd dereferenced it in any other context.  
>>
>> Still correct
>>
>> If
>> >you want to
>> >test for the 'foo' field in the 'form.test' scope, you have to do
>> >structKeyExists(form.test, 'foo'), or if you were really
>> >looking for the
>> >variables scope, then structKeyExists(variables.test, 'foo').
>>
>> Agreed.
>>
>> Now please explain what it is that isDefined() does differently to
>> structKeyExists() with regard to looking in implicit scopes.
>>
>> Your earlier post implied that it was behaving differently.
>> The point of my
>> code was to show that it doesn't.
>>
>> Spike
>>
>>
>>
>
>
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to