One place where I have used it in the past is when I have a deeply nested
structure. Something like this:

<cfif isDefined("application.cache.html.menus.#somemenuid#")>
<cfoutput>#application.cache.html.menus[somemenuid]#</cfoutput>
<cfelse>
Code to generate the menu goes here.
</cfif>

The alternative would be to do this:

<cfif structKeyExists(application,'cache')
      AND structKeyExists(application.cache,'html')
AND structKeyExists(application.cache.html,'menus')
AND structKeyExists(application.cache.html.menus,someMenuID)>
<cfoutput>#application.cache.html.menus[somemenuid]#</cfoutput>
<cfelse>
Code to generate the menu goes here.
</cfif>

I find the first one more readable and I'm guessing it is a bit faster too.

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 Dawson, Michael
>Sent: Monday, July 12, 2004 2:38 PM
>To: CF-Talk
>Subject: RE: StructKeyExists() vs IsDefined()
>
>Is there ever a good reason to use IsDefined rather than
>StruckKeyExists?
>
>  _____  
>
>From: Sean Corfield [mailto:[EMAIL PROTECTED]
>Sent: Monday, July 12, 2004 5:32 PM
>To: CF-Talk
>Subject: Re: StructKeyExists() vs IsDefined()
>
>
>> 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.
>
>I think you're misunderstanding what's been said: CF itself does the
>lookup on the first argument to structKeyExists() - whatever it finds,
>in whatever scope it finds it, is what is passed to structKeyExists()
>to look at.
>
>> <cfset variables.foo = structNew()>
>> <cfset variables.foo.test = "bar">
>> <cfoutput>#structKeyExists(foo,'test')#</cfoutput>
>> returns "YES", while this:
>
>CF looks up foo and finds it in variables scope and passes the
>'contents' of foo to structKeyExists(). structKeyExists() looks inside
>that and finds the key test.
>
>> <cfset variables.form  = structNew()>
>> <cfset variables.form.test = "bar">
>> <cfoutput>#structKeyExists(form,'test')#</cfoutput>
>> returns "NO"
>
>Right. CF looks up form and finds the form scope and then passes the
>'contents' of that to structKeyExists(). structKeyExists() looks
>inside that and does not find bar.
>
>> There is nothing in the documentation to support that this should be
>the
>> case.
>
>Sure there is but it's all about variable lookup in scopes and nothing
>to do with structKeyExists().
>  _____  
>
>
>
>
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings] [Donations and Support]

Reply via email to