mega_L wrote:
> As far as my knowledge goes, the BEST practise when
setting local variable
> inside a CFFUNCTION is to use the word "var" in front of
the variable name, for
> example: <CFSET var myVariable="whatever">
Yes, this is the best practice to make the variable local to
the
function. But you only need to use the var keyword once when
you define
the variable. And you must define the local variables with
the var
keyword at the very beginning of the function body. The only
content
allowed before this happens are the argument definitions,
i.e. the
<cfargument ...> tags.
If you have a variable that you don't need to use, you just
need to
define it with a default and|or dummy value in the beginning
of the
function. Then you can set it's value appropriately later in
the
function when it is required. This is often done with query
tag return
variables.
I.E.
<cffunction name="myQryFunction"...>
<cfargument name="someArgument"...>
<cfset var localQry = "">
...
<cfquery name="localQry"...>
SQL
</cfquery>
<cfreturn localQery>
</cffunction>
Because of this necessity to define all local variables at
the beginning
of the function, a lot of developers use a standard where
they define a
local structure. They can then just append new keys and
values to this
local structure throughout the function. Personally I don't
see much
advantage to doing this, but to each his|her own.
I.E.
<cffunction name="myQryFunction"...>
<cfargument name="someArgument"...>
<cfset var local = structNew()>
...
<cfset local.something = "FooBar">
<cfreturn local.something>
</cffunction>