> -----Original Message-----
> From: Johnny Le [mailto:[EMAIL PROTECTED]
> Sent: Sunday, March 27, 2005 3:41 PM
> To: CF-Talk
> Subject: Re: Best practice question?
>
> In CFC, I always do this:
>
> <cffunction name = "something">
> <cfargument name="some" required="true" type="string"/>
> <cfset var few = arguments.some/>
> <cfset few = 10/>
> </cffunction>
In my opinion:
One of the main reasons to scope is to be able to easily track the source of
a variable. Setting your arguments to function local like this eliminates
that - you don't know what the source of the variable is.
> If we should always scope our variables. Should I do this?
>
> <cfset var variables.few = arguments.some/>
> <cfset variables.few = 10/>
Well - no, that won't work really.
"Variables" is the scope name for the private CFC scope. It might
technically work the way you have it but at best you'd be creating a
function local "variables" container that would be completely different from
the actual CFC variables container.
> The reason I re-set arguments.some to few is because I want to type 3
> letters instead of 14, but if I should always scope my variables, then
> there is no point of resetting it.
In general I would recommend just using the arguments prefix - as a CFC you
should get used to verbosity (and clarity). ;^)
But if you really wanted to do this you might consider something like this:
<cffunction name = "something">
<cfargument name="some" required="true" type="string"/>
<cfset var args = arguments />
</cffunction>
You should now have a function local reference to the arguments scope in the
args container. You can now use "args" in every way that you'd use
"arguments".
Easy peasy.
Another trick I like is to do this:
<cffunction name = "something">
<cfset var local = structNew() />
</cffunction>
You can then easily set function local variables anyplace in the function
(or at the top) by adding them to the "local" struct which becomes,
essentially, a pseudo-scope.
Putting all that together might look like this:
<cffunction name = "something">
<cfargument name="MaxCount" required="true" type="numeric" />
<cfset var args = arguments />
<cfset var local = structNew() />
<cfloop from="1" to="#args.MaxCount#" index="local.Cnt">
<cfoutput>#local.Cnt#<br></cfoutput>
</cfloop>
</cffunction>
It's a simple example but it's very clear where all the values came from and
how each should be used.
Jim Davis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble
Ticket application
http://www.houseoffusion.com/banners/view.cfm?bannerid=48
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:200143
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54