One reason to use IsDefined over StructKeyExists is when you don't know if
one of the child structures exists.

For example, in our control app, we assign permissions to our users like so:

SESSION.p <-- struct, always exists
SESSION.p.nav <-- struct, exists if the user has at least one perm in this
set 
SESSION.p.nav.SOMEPERMISSION <!-- bool, only exists if this user has this
perm

So you can see that if we wanted to check if someone had SOMEPERMISSION we'd
have to do some dancing around to use StructKeyExists. Instead we just say
IsDefined('SESSION.p.nav.SOMEPERMISSION') and we're done.

Admittedly we could probably do this more efficiently by having all perms as
keys inside a perms struct, but cest la vie.


andy


-----Original Message-----
From: Russ Michaels [mailto:[email protected]] 
Sent: Wednesday, September 01, 2010 10:51 AM
To: cf-talk
Subject: Re: CFParam vs. IsDefined


defo better to make sure they are defined.
Also using isDefined() is also bad as this search every single scope for
your variable and is thus quite slow and can potentially cause timeouts.
Much better to use StructKeyExists() and only test the scope your variable
is in.

--
Russ Michaels
www.cfmldeveloper.com
Supporting the cf community since 1999
FREE ColdFusion/Railo hosting for developers.


On Wed, Sep 1, 2010 at 4:40 PM, DURETTE, STEVEN J (ATTASIAIT) <
[email protected]> wrote:

>
> Using your way ensures that any variables that you are using already 
> exist.
>
> I was always taught that I should define my variables before I use 
> them in code (my ancient programming college courses).  So, I tend to 
> follow your method.
>
> Also, from a security stand, you should already know what should be 
> coming into your page. If it isn't there then something went wrong. 
> And if you didn't define it and it is there you should be ignoring it.
>
> Steve
>
> -----Original Message-----
> From: Michael Grant [mailto:[email protected]]
> Sent: Wednesday, September 01, 2010 11:36 AM
> To: cf-talk
> Subject: CFParam vs. IsDefined
>
>
> I prefer to CFParam my vars with a default value of a zero len string 
> or a 0 for numeric values. Then I skip the isdefined and just test 
> against the value. Well recently someone I know said that it's better 
> to test if it's defined. Is there a pro or con to doing it my way vs. 
> IsDefined ?
>
> Thanks.
>
>
>
>
> 



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology-Michael-Dinowitz/dp/1430272155/?tag=houseoffusion
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:336717
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

Reply via email to