user160820
user160820

Reputation: 15200

Coldfusion Method Level scope definition

What is be the best way to define method level variables scope?

Some ColdFusion documents say it should be done like this:

 <cfset Var testVariable = "this is a local variable">

But others, do it like this:

<cfset LOCAL = StructNew() />
<cfset LOCAL.testVariable  = StructNew() />

Which way is better?

Upvotes: 3

Views: 130

Answers (1)

BennyB
BennyB

Reputation: 473

Either will work.

In CF9 (and I assume 10) var local = structNew() is unnecessary (though harmless and makes your code backward compatible) as there is within each function a local scope that contains any locally scoped values. Also with CF8 and earlier these variables will need to be the first thing declared in a function.

So to specifically answer your question I prefer the var local = structNew() (or just var local={}) because:

  • It means that locally scoped variables are obviously such (var scoping issues can be a pain to debug).
  • You can dump or examine the local "scope" in cf 8.

Upvotes: 4

Related Questions