Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coldfusion Method Level scope definition

Tags:

coldfusion

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?

like image 890
user160820 Avatar asked Feb 05 '26 13:02

user160820


1 Answers

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.
like image 68
BennyB Avatar answered Feb 09 '26 06:02

BennyB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!