House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

CFParam vs IsDefined

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Using your way ensures that any variables that you are using already
DURETTE, STEVEN J (ATTASIAIT)
09/01/10 11:40 A
defo better to make sure they are defined.
Russ Michaels
09/01/10 11:51 A
> <cfloop index="i" from="1" to="10000">
Sean Corfield
09/01/10 12:25 P
Russ,
Mark A. Kruger
09/01/10 11:57 A
> dot notation means nothing to isdefined()
John M Bliss
09/01/10 12:25 P
Nevermind. Misread your post.
John M Bliss
09/01/10 12:25 P
Hmmm, maybe they've changed the default.
Peter Boughton
09/02/10 06:33 A
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/01/2010 11:37 AM

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.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
DURETTE, STEVEN J (ATTASIAIT)
09/01/2010 11:40 AM

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 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.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Russ Michaels
09/01/2010 11:51 AM

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) < sd1985@att.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
John M Bliss
09/01/2010 11:54 AM

FWIW, the following two tickcounts are nearly identical on my localhost so the reason to use one over the other (cfparam versus isdefined/set) is likely not due to performance... <cfset starttick = getTickCount()> <cfloop index="i" from="1" to="10000"> <cfparam default="#i#" name="variables.var#i#"> </cfloop> <cfoutput>#Evaluate(getTickCount() - starttick)#</cfoutput> <cfset starttick = getTickCount()> <cfloop index="i" from="1" to="10000"> <cfif not IsDefined("variables.anothervar" & i)> <cfset SetVariable("variables.anothervar" & i, i)> </cfif> </cfloop> <cfoutput>#Evaluate(getTickCount() - starttick)#</cfoutput> ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Sean Corfield
09/01/2010 12:25 PM

> <cfloop index="i" from="1" to="10000"> ----- Excess quoted text cut - see Original Post for more ----- Try that with <cfset variables["anothervar" & i] = i /> - I suspect setVariable() is slowing that down. > <cfoutput>#Evaluate(getTickCount() - starttick)#</cfoutput> You don't need evaluate() here BTW. In general, micro-benchmarks like this aren't really useful. For example, you're generating lots of whitespace here so you'd do better to wrap the loops in <cfsilent> or write them in <cfscript>. There are all sorts of variable factors in code like this. -- Sean A Corfield -- (904) 302-SEAN Railo Technologies, Inc. -- http://getrailo.com/ An Architect's View -- http://corfield.org/ "If you're not annoying somebody, you're not really alive." -- Margaret Atwood

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Mark A. Kruger
09/01/2010 11:57 AM

Russ, You are correct about that - but it's not typically a performance problem in my view. More of a "buggy/security" type problem :) -Mark Mark A. Kruger, MCSE, CFG (402) 408-3733 ext 105 Skype: markakruger www.cfwebtools.com www.coldfusionmuse.com www.necfug.com 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) < sd1985@att.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Russ Michaels
09/01/2010 12:17 PM

it depends how many vars you have in each scope. If you have barely any then the search will be quick. If you have a complex app with hundreds of vars then the result will of course be different, especially if they are complex structs. There is also the fact that isdefined() will find the variable in ANY scope, which may not be the one you want. e.g. isDefined('myvar') would find variables.myvar cookie.myvar session.myvar etc so if you are actually looking for myvar only in the form scope then this could cause you issues, you also need to remember that dot notation means nothing to isdefined(), so isDefined('form.myvar') could also find session.form.myvar url.form.myvar cookie.form.var etc On Wed, Sep 1, 2010 at 4:56 PM, Mark A. Kruger <mkruger@cfwebtools.com>wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
John M Bliss
09/01/2010 12:25 PM

> dot notation means nothing to isdefined() Um, really?  The following returns "NO" for me: <cfset variables.myvar = 1> <cfoutput>#IsDefined("url.myvar")#</cfoutput> ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Russ Michaels
09/01/2010 12:45 PM

of course as you are checking for url.myvar which doesn't exist as you created variables.myvar not url.myvar. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
09/01/2010 12:17 PM

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 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) < sd1985@att.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Scott Brady
09/04/2010 07:38 AM

> 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. > > I'm not a fan of isDefined() [to put it mildly], but according to an engineer from Adobe at MAX in 2008, there's no performance difference between cfparam and isdefined (at least with CF9, if not earlier versions). I just think it's sloppy programming to have to use it. :) Scott -- ----------------------------------------- Scott Brady http://www.scottbrady.net/

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dave Watts
09/01/2010 11:47 AM

> 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 ? No, this is a matter of personal preference really. My preference tends to align with yours for the reasons that Steven mentioned, but that's more of a documentation issue than anything else. Dave Watts, CTO, Fig Leaf Software http://www.figleaf.com/ http://training.figleaf.com/ Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on GSA Schedule, and provides the highest caliber vendor-authorized instruction at our training centers, online, or onsite.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Roger Austin
09/01/2010 12:25 PM

---- Michael Grant <mgrant@modus.bz> wrote: > > 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 ? I would use cfparam out of convenience and flow. If you use the incoming parameter several times, you don't have to test it but once with cfparam. I'm lazy. -- LinkedIn: http://www.linkedin.com/pub/roger-austin/8/a4/60 Twitter:  http://twitter.com/RogerTheGeek Blog:     http://rogerthegeek.wordpress.com/ http://www.misshunt.com/ Home of the Clean/Dirty Magnet

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
09/01/2010 12:45 PM

>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 ? There is a difference between a value being undefined, and a value being zero. Consider the following (contrived) example: <cffunction name="convertToKelvin" returntype="Numeric" output="false">   <cfargument name="Celsius"   type="Numeric" default="0" />   <cfargument name="Farenheit" type="Numeric" default="0" />   <cfif Arguments.Celsius NEQ 0>     ...   <cfelseif Arguments.Farenheight NEQ 0>     ...     <cfelse>     <cfthrow message="Invalid arguments"/>   </cfif> </cffunction> vs <cffunction name="convertToKelvin" returntype="Numeric" output="false">   <cfargument name="Celsius"   type="Numeric" required="false" />   <cfargument name="Farenheit" type="Numeric" required="false" />   <cfif isDefined('Arguments.Celsius')>     ...   <cfelseif isDefined('Arguments.Farenheight')>     ...     <cfelse>     <cfthrow message="Invalid arguments"/>   </cfif> </cffunction> (Using cfargument/default is equiv to using cfparam/default.) Unless you can say with certainty that blank/zero is identically equivalent to undefined, you should always check for existance (using either isDefined or structKeyExists as preferred) instead of setting a default.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
David McGraw
09/01/2010 04:04 PM

The only general advantage I see is that you are reserving memory no matter what, even though it's not much, where as with the isDefined, your just checking, rather than reserving that memory. >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.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Rick Root
09/01/2010 05:57 PM

Personally I like to <cfparam> things that I know are supposed to exist, and I will go both ways on page variables that may or may not exist. I prefer to param it and then test for a valid value than to test for isDefined() Ie... <cfparam name="someid" default=""> <cfif someid eq "">  (or <cfif len(someid) eq 0>  )    Holy Crap!<cfabort> </cfif> Rick

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Casey Dougall
09/01/2010 06:40 PM

----- Excess quoted text cut - see Original Post for more ----- Yup, and you can throw all of them at the top of the script. I'm not saying there isn't times you need to use StructKeyExists. Readability is by far my biggest goal, the faster I can read though a script, the less hassles I have with it as a whole, and cfparam allows you to use a built in function to assist in setting up page variables. Not saying there arn't times you should use StuctKeyExists, but if it was a param to begin with, you already know the default value; That coupled with StructKeyExists is a pain to type!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Tony Bentley
09/01/2010 07:09 PM

A classic issue yesterday related to isDefined(): This worked perfectly:         <cfscript>             //pass boolean to determine if the given field is null in the stored proc (1=submitted, 2=finalized)             if(arguments.status eq 1){                 submitted = false;                 finalized = true;             }             else if(arguments.status eq 2){                 finalized = false;                 submitted = true;             }         </cfscript>         <cfstoredproc datasource="#this.datasource#" procedure="prc_updateReportStatus">             <cfprocparam cfsqltype="CF_SQL_INTEGER" value="#this.reportid#">             <cfprocparam cfsqltype="CF_SQL_INTEGER" value="#this.userid#" null="#submitted#">             <cfprocparam cfsqltype="CF_SQL_INTEGER" value="#this.userid#" null="#finalized#">             <cfprocresult name="qry">         </cfstoredproc> This did not:         <cfscript>             //pass boolean to determine if the given field is null in the stored proc (1=submitted, 2=finalized)             if(arguments.status eq 1){                 submitted = true;             }             else if(arguments.status eq 2){                 finalized = true;             }         </cfscript>         <cfstoredproc datasource="#this.datasource#" procedure="prc_updateReportStatus">             <cfprocparam cfsqltype="CF_SQL_INTEGER" value="#this.reportid#">             <cfprocparam cfsqltype="CF_SQL_INTEGER" value="#this.userid#" null="#isDefined('submitted')#">             <cfprocparam cfsqltype="CF_SQL_INTEGER" value="#this.userid#" null="#isDefined('finalized')#">             <cfprocresult name="qry">         </cfstoredproc> I ran a DB trace and both values were coming out as defined in the second code.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
09/02/2010 05:08 AM

> That coupled with StructKeyExists is a pain to type! Why on earth would you *type* it!? This is exactly why your IDE has Word Completion, Snippets and Templates! "Str" then <Alt-/> completes word to StructKeyExists (press <Alt-/> again to cycle through other commonly used words) "ske" then <Ctrl-J> snippet for StructKeyExists([cursor]) "skel" then <Ctrl-Space>,<Enter> to select a template: StructKeyExists([Struct],'[Value]') AND Len([Struct].[Value]) (All this stuff is one of the things I keep meaning to write a full blog article on... if anyone is interested let me know and I'll try getting to it sooner, and post details here once done.)

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
09/02/2010 05:11 AM

Oh, just to point out, ske and skel are custom ones I've created. They're not default commands. Shortcut keys may vary too.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andrew Scott
09/02/2010 05:37 AM

This must be a CFEclipse thing as CFBuilder is CTRL-SPACE Regards, Andrew Scott http://www.andyscott.id.au/ ----- Excess quoted text cut - see Original Post for more ----- to ----- Excess quoted text cut - see Original Post for more ----- article > on... if anyone is interested let me know and I'll try getting to it sooner, and ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
09/02/2010 06:04 AM

>This must be a CFEclipse thing as CFBuilder is CTRL-SPACE That's code completion. Word completion is faster (when you know what you want). These are all Eclipse things (and will exist in any other IDE worth using), so available for both CFEclipse and CFBuilder.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andrew Scott
09/02/2010 06:08 AM

Well that's interesting then because this <Alt-/> doesn't work in my CFBuilder installation. Regards, Andrew Scott http://www.andyscott.id.au/ > >This must be a CFEclipse thing as CFBuilder is CTRL-SPACE > > That's code completion. Word completion is faster (when you know what > you want). > > These are all Eclipse things (and will exist in any other IDE worth using), so > available for both CFEclipse and CFBuilder.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Peter Boughton
09/02/2010 06:33 AM

Hmmm, maybe they've changed the default. Goto Windows>Preferences and type "keys" in the filter box. That should bring the key binding panel, type "word" in that. Look for "Word Completion" option(s) and it'll list what the binding is. If it still doesn't work, check the "When" value - I've got one "Editing Text" and one "In Windows".

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Scott Brady
09/04/2010 07:39 AM

It's working for me in cftags, using CFEclipse on the Mac (where it appears to be CTRL-. [that's control and period] ). I've never seen that feature before, so I'm going to try to start using it. Scott On Thu, Sep 2, 2010 at 4:32 AM, Andrew Scott <andrews@andyscott.id.au>wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/02/2010 06:28 AM

I'm very interested. I just started using Aptana (coming from Homesite+) and I really really want to speed up my dev to where I was at with HS+. I'd love some more hotkey and snippet info. On Thu, Sep 2, 2010 at 5:00 AM, Peter Boughton <boughtonp@gmail.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Rick Root
09/02/2010 08:16 AM

On Thu, Sep 2, 2010 at 5:00 AM, Peter Boughton <boughtonp@gmail.com> wrote: > >> That coupled with StructKeyExists is a pain to type! > > Why on earth would you *type* it!? "vi" doesn't do code completion?   lol

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Steve Lichtenberg
09/02/2010 08:23 AM

Yes it does :-).  In .virc set up aliases for anuything you want.  use them in update mode not insert mode. --S   ^ On Thu, Sep 2, 2010 at 8:16 AM, Rick Root <rick.root@gmail.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Rick Root
09/02/2010 08:33 AM

On Thu, Sep 2, 2010 at 8:23 AM, Steve Lichtenberg <steve.lichtenberg@gmail.com> wrote: > > Yes it does :-).  In .virc set up aliases for anuything you want.  use them > in update mode not insert mode. touche'

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Claude_Schnéegans
09/02/2010 10:07 AM

  >>Also using isDefined() is also bad as this search every single scope for your variable CFPARAM will also search for the variable exactly the same way in order to create it if it does not exist. What's the big differrence? >> and can potentially cause timeouts. Wow! Where do you hide your variables so it takes so long to find them?


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

February 08, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29