Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Jun 03, 2008 Jun 03, 2008
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

TOPICS
Getting started
748
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 03, 2008 Jun 03, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 03, 2008 Jun 03, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 03, 2008 Jun 03, 2008

It is more like a strongly typed language where one has to initialize
ones variables before one can use them. In CF this initializes the
variable so that it is local to the function, otherwise the variable
will be global and if one does not account for the global nature, very
hard to track bugs can be introduced into the code.

The best practice is to ALWAYS initialize local variable and to ALWAYS
scope variables so that is is explicit when one is using local and one
is using global 'variables' scoped variables.


<cffunction...>
...
<cfset var myLocalVar = ...><!--- initializes a local var --->
<cfset variables.myGlobalVar = ...> <!--- initializes a global var --->

<cfsomething myLocalVar ...><!--- uses the local var --->

<cfsomethingelse variables.myGlobalVar...><!--- uses the global var --->
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 03, 2008 Jun 03, 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!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 03, 2008 Jun 03, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2008 Jun 03, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 03, 2008 Jun 03, 2008
Thanks for all those replies really appreciated that makes sense-now back to the docs!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 04, 2008 Jun 04, 2008
LATEST
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources