Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cfc best practices 1

Status
Not open for further replies.

jimmyshoes

Programmer
Jun 1, 2008
132
GB
I read that when using cfcs in a coldfusion web application you should declare variables within a UDF with the local scope
For example
Code:
<cfset var someVar = "" >

However, is it ok to use the arguments scope throughout a UDF or should you include
Code:
<cfset var theVar = "" >
<cfset theVar = arguments.theVar />

 
you should declare variables within a UDF with the local scope

Yes, variables only used inside the function should be declared with VAR.

However, is it ok to use the arguments scope throughout a UDF

I do not think that one has an absolute yes or no answer. It really depends on the function and the desired outcome.

... or should you include

<cfset var theVar = "" >
<cfset theVar = arguments.theVar />

A prime example is the code above. Like in java, some objects are passed by reference. So the code above would essentially do nothing. Even if the function used the local variable, it is just a pointer to arguments.theVar. So in other words, the function is still using arguments.theVar.



----------------------------------
 
Do variables used to index a cfloop need to be local.
For example, should I do this
Code:
<cfset var inc = "" />

<cfloop index = "inc" from = "1" to = "10">
 
Yes. All variables used only within the function should be VAR'd. That includes query names.

<cfset var myQuery = ""/>

<cfquery name="myQuery" ...>
....
</cfquery>

----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top