Skip to main content
Inspiring
June 3, 2008
Question

Why is this set? is it like cfparam for components?

  • June 3, 2008
  • 4 replies
  • 901 views
In the docs this line is used:
<cfset var empQuery="">
which seems to be creating a cfparam like variable in case the empQuery is not defined? Is that about right?
Why is this needed(is it even needed?)
Thanks

This topic has been closed for replies.

4 replies

BKBK
Community Expert
Community Expert
June 3, 2008
One other advantage of using var: since it is local to the function, empQuery will be marked for garbage collection once the function returns.

Inspiring
June 4, 2008
Thanks for all those replies really appreciated that makes sense-now back to the docs!
Participating Frequently
June 5, 2008
I've got a series on OOP with CF that addresses the questions you had and more.

Object Oriented Coldfusion : 1 : Intro to Object.cfc

Object Oriented Coldfusion : 4 : The var scope
Inspiring
June 3, 2008
Hydrowizard wrote:
> Thanks Ian for explaining that-last question what is 'initializes' in laymans terms? Cheers
>
> this all reminds me of the complexity of learning how to do cfif equivalents in cfscript!

In strongly typed languages like Java and C++, before one can use a
variable one must initialize it. I.E. one must name it; define the type
of variable, integer, string, float, etc., its scope, global, package,
local, etc.

ColdFusion is generally a loosely typed language and does not require
this rigid definition. But when one starts working with code that can
be used repeatedly in the same space, typing becomes more important. So
it is a best practice to type ones variables in a function to keep the
code easier to work with.
Inspiring
June 3, 2008
Thanks Ian for explaining that-last question what is 'initializes' in laymans terms? Cheers

this all reminds me of the complexity of learning how to do cfif equivalents in cfscript!
Inspiring
June 3, 2008
Hydrowizard wrote:
> In the docs this line is used:
> <cfset var empQuery="">
> which seems to be creating a cfparam like variable in case the empQuery is not
> defined? Is that about right?

not really, it scopes the variable to one local (the "var" bit) to that function
so it won't get mixed up w/other calls to that function/cfc which can happen
under a load. something like the query gets finished but in the span of time
before it's returned, another call is made to that function & maybe the
resultset is different (not in this case bu you see where this is going). the
first call to that function could return the 2nd call's results. scoping that
variable local prevents this from happening.
Inspiring
June 3, 2008
Thanks Paul so best practise means that I should use it. It is a bit like cfttransaction in the reasons whjy it is needed thanks again