Skip to main content
Inspiring
September 2, 2009
Question

Harm in not initializing "var" variables at beginning of CFC function?

  • September 2, 2009
  • 1 reply
  • 1442 views

I was wondering if there is any need at this point to init variables in the beginning of CFC's that use the var scope (including queries, structures, ect).

I'm talking about the syntax.....

cfset var thisVariable = "someValueOrNothing"

As opposed to declaring them on the fly like......

{Some code up here}

cfquery name="var.getSomething"

{Some code below}

    This topic has been closed for replies.

    1 reply

    ilssac
    Inspiring
    September 2, 2009

    Presuming you are talking about ColdFusion 6 through ColdFusion 8, then yes there can be great harm.

    If you do not declare the variable with the VAR keyword, which can only be done at the top of the function, then the variable is not LOCAL to the function, but rather it is GLOBAL to the component.

    That means any other function that uses the same variable name OR if this function is called multiple times, especially in a recursive manner, then these variables can have their values change in unexpected ways.  This can easily lead to complex and very hard to diagnose and debug problems.

    Now in your second example you actually use a structure "var".

    cfquery name="var.getSomething"

    It is a common pratice to VAR scope a local structure and then just add new keys to the structure dynamically as desired.  This works just fine.  The scope is commonly named 'local' but I know of nothing that prevents one from naming the scope 'var'.  So you could have code that looks like this.

    <cffunction....>

    <cfargument....>

    <cfset var var = structNew()>

    ....

    <cfquery name = "var.getSomething">

    ....

    <cffunction>

    I'm not sure that will work, but it might be worth a try.

    Now all of this may change in the next version of ColdFusion.  I have heard that ColdFusion 9 will automatically make variables local in functions, making the VAR keyword unnecessary I presume.

    Inspiring
    September 2, 2009

    From what you have said, to summarize,

    I do not have to use the declaration

    cfset var thisVariable = "test"

    as long as I reference the variable with the var scope when I created it, IE

    cfset var.thisVariable = 2 + 2>

    Another quesiton is, is it overkill to do BOTH? I dont see how it would hurt, if anything I would imaging explicitly stating the scope could speed it up.

    ilssac
    Inspiring
    September 2, 2009

    chazman113 wrote:

    From what you have said, to summarize,

    I do not have to use the declaration

    cfset var thisVariable = "test"

    as long as I reference the variable with the var scope when I created it, IE

    cfset var.thisVariable = 2 + 2>

    Another quesiton is, is it overkill to do BOTH? I dont see how it would hurt, if anything I would imaging explicitly stating the scope could speed it up.

    Not quite.  VAR is not a scope in the usual sense like the applicaiton, session or form scopes.  When you declare a variable with the VAR keyword, you acually reference it in the rest of the function with NO scope declaration.

    This would look like this.

    <cffunction...>

    <cfargument...>

    <cfset VAR thisVariable = "test">

    ....

    <cfset thisVariable = "something Different"> <!--- note that there is nothing in front of 'thisVariable'--->

    ...

    </cffunction>

    Some people like to declare a single local structure and then use that for all the local variables in the function.

    <cffunction...>

    <cfargument...>

    <cfset VAR local = structNew()>

    ....

    <cfset local.thisVariable = "something Different"> <!--- note that there is nothing in front of 'local'--->

    ...

    </cffunction>

    But there is no scope named "var" created by ColdFusion.  You could create one, I think, if you wanted.  The var keyword simple declares a variable as being local to the function.

    I do not yet know how this is changing in ColdFusion 9.