Re: cffunction - initialize query as empty string or QueryNew
Woah woah woah. I want to be sure we are -very- clear here. Peter, you said, With CF9 you don't need the var keyword anymore, and if you don't need backwards compatible code it's (arguably) clearer to not use it at all. That is not true. You do need to var scope. What you don't need to do is use var statements to work with the var scope. You can use the local scope instead. So to be clear, these two options are ok. cfset var x = 1 cfset local.x = 1 On Wed, Aug 3, 2011 at 1:37 PM, Gerald Weir g.w...@projectsinknowledge.com wrote: Peter, great examples, really helpful. In a nutshell, if I get it, in CF9 you do not need to use: cfset var local = StructNew() / because you are going to define your vars as: local.myVar1 and local.myVar2 and the local structure is already defined by default. You refer to your vars throughout the cffunction as local.myVar. Hope that's right, Jerry ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346513 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: cffunction - initialize query as empty string or QueryNew
Sorry, yeah, that was worded badly. I should have prefixed that with If you *always* use the local scope, you don't need var... Annoyingly I can't go back and revise the message, and for some reason it got posted twice too. :/ ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346517 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: cffunction - initialize query as empty string or QueryNew
Thank you Ray and Peter for your answers/clarification on this question. It's clear now and every helpful. Jerry ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346518 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
cffunction - initialize query as empty string or QueryNew
Hello, We are discussing the proper way to initialize a query in a cffunction. Does it make a significant difference to use QueryNew rather than (empty string) as in the following: cfcomponent cffunction name=test access=public returntype=query output=false .this? cfset var myQuery = .or this? cfset var myQuery = QueryNew(col1,col2,VarChar,Time cfquery name=myQuery datasource=#myDB# ... /cfquery /cfcomponent I learned to use the first version var myQuery = from the CFMX Bible and the CF documentation but a colleague believes that this is a mistake and that using this approach creates the myQuery variable as a string and not as a query. Consequently, it is creating a variable of the wrong type. Any thoughts on this are welcome. Thanks, Jerry ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346480 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: cffunction - initialize query as empty string or QueryNew
creates the myQuery variable as a string and not as a query. It does. But as soon as your run the cfquery the myQuery variable is essentially becomes a query object. Does it make a significant difference to use QueryNew rather than (empty string) as in the following: For a function that simple, I am not sure I see the benefit of declaring a query object that just gets replaced in the very next line. ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346483 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: cffunction - initialize query as empty string or QueryNew
It doesn't matter - CFML is not like Java (where you must pre-define variables with strict types). In CFML, variables can change types at any time. If you're just var scoping a cfquery variable, it doesn't matter what you use. (I would guess using QueryNew might be ever so slightly slower - but I doubt it's significant, and wouldn't be surprised if the JVM optimized it away anyhow.) You can avoid the pointless debate by using: cfquery name=local.myQuery etc If you're not on CF9/equivalent then you need a cfset var local = StructNew() / immediately after any arguments. ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346488 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: cffunction - initialize query as empty string or QueryNew
Leigh, Peter, Thanks for your comments. All makes sense to me. If I can ask one follow-up on Peter's answer though. I've recently started to work on CF9 and was using my cffunction techniques from the CF8 days. i.e., cfset var local = StructNew() cfset local.myVar1 = cfset local.myVar2 = ...and I'm trying to make sure I'm doing the right thing on CF9 by doing the following instead: cfset var myVar1 = cfset var myVar2 = cfif myVar EQ .. Is that correct CF9 approach to scoping vars within the CFC/cffunction? Jerry ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346490 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: cffunction - initialize query as empty string or QueryNew
With CF9 you don't need the var keyword anymore, and if you don't need backwards compatible code it's (arguably) clearer to not use it at all. That means, do NOT use either of your examples, unless you _need_ a value in myvar1/myvar2 at the start. Perhaps a good way to explain it is to use some sample functions... this is just a bit of nonsense code, but hopefully demonstrates the different ways to do it... cffunction name=test_cf8_var --- [imagine arguments here] --- cfset var myVar1 = / cfset var myVar2 = / cfif Arguments.Something EQ whatever cfset myVar1 = wibble / /cfif cfloop index=myVar2 from=0 to=10 cfset myVar1 = Variables.Data[myVar2] / /cfloop cfif len(myVar1) cfreturn myVar1 / cfelse cfreturn burp / /cfif /cffunction cffunction name=test_cf8_local --- [imagine arguments here] --- cfset var local = StructNew() / cfif Arguments.Something EQ whatever cfset local.myVar1 = wibble / cfelse cfset local.myVar1 = / /cfif cfloop index=local.myVar2 from=0 to=10 cfset local.myVar1 = Variables.Data[local.myVar2] / /cfloop cfif len(local.myVar1) cfreturn local.myVar1 / cfelse cfreturn burp / /cfif /cffunction cffunction name=test_cf9 --- [imagine arguments here] --- cfif Arguments.Something EQ whatever cfset local.myVar1 = wibble / cfelse cfset local.myVar1 = / /cfif cfloop index=local.myVar2 from=0 to=10 cfset local.myVar1 = Variables.Data[local.myVar2] / /cfloop cfif len(local.myVar1) cfreturn local.myVar1 / cfelse cfreturn burp / /cfif /cffunction ...does that make sense? ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346491 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: cffunction - initialize query as empty string or QueryNew
With CF9 you don't need the var keyword anymore, and if you don't need backwards compatible code it's (arguably) clearer to not use it at all. That means, do NOT use either of your examples, unless you _need_ a value in myvar1/myvar2 at the start. Perhaps a good way to explain it is to use some sample functions... this is just a bit of nonsense code, but hopefully demonstrates the different ways to do it... cffunction name=test_cf8_var --- [imagine arguments here] --- cfset var myVar1 = / cfset var myVar2 = / cfif Arguments.Something EQ whatever cfset myVar1 = wibble / /cfif cfloop index=myVar2 from=0 to=10 cfset myVar1 = Variables.Data[myVar2] / /cfloop cfif len(myVar1) cfreturn myVar1 / cfelse cfreturn burp / /cfif /cffunction cffunction name=test_cf8_local --- [imagine arguments here] --- cfset var local = StructNew() / cfif Arguments.Something EQ whatever cfset local.myVar1 = wibble / cfelse cfset local.myVar1 = / /cfif cfloop index=local.myVar2 from=0 to=10 cfset local.myVar1 = Variables.Data[local.myVar2] / /cfloop cfif len(local.myVar1) cfreturn local.myVar1 / cfelse cfreturn burp / /cfif /cffunction cffunction name=test_cf9 --- [imagine arguments here] --- cfif Arguments.Something EQ whatever cfset local.myVar1 = wibble / cfelse cfset local.myVar1 = / /cfif cfloop index=local.myVar2 from=0 to=10 cfset local.myVar1 = Variables.Data[local.myVar2] / /cfloop cfif len(local.myVar1) cfreturn local.myVar1 / cfelse cfreturn burp / /cfif /cffunction ...does that make sense? ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346492 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm
Re: cffunction - initialize query as empty string or QueryNew
Peter, great examples, really helpful. In a nutshell, if I get it, in CF9 you do not need to use: cfset var local = StructNew() / because you are going to define your vars as: local.myVar1 and local.myVar2 and the local structure is already defined by default. You refer to your vars throughout the cffunction as local.myVar. Hope that's right, Jerry ~| Order the Adobe Coldfusion Anthology now! http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:346493 Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm