0

I am starting to use the LOCAL scope in ColdFusion 9. I am trying to figure when to use it and when NOT to use it. Can you tell me the advantages/disadvantages of using the LOCAL scope either way? Should LOCAL be used on every page, or just in functions?

WHICH SHOULD BE USED?

<cfset ImageHeight = 36>  
<cfset ImageWidth = 42>  
<cfoutput>
<img src="" height="#ImageHeight#" width="#ImageWidth#">
</cfoutput>

OR

<cfset LOCAL.ImageHeight = 36>  
<cfset LOCAL.ImageWidth = 42>  
<cfoutput>
<img src="" height="#LOCAL.ImageHeight#" width="#LOCAL.ImageWidth#">
</cfoutput>
Evik James
  • 178
  • 6
  • Wait, does that mean you have to add six characters, five of which are ugly upper-case, to get any scoping (except global one) in ColdFusion? Wow. And people are bitching about PHP... –  May 03 '11 at 19:52
  • 1
    @delnan - the casing is a personal preference in coding style and is nothing to do with ColdFusion. The local scope, as Justice has already pointed out, is only usable in functions in ColdFusion 9. Access to other scopes in ColdFusion is not that different to PHP. – Stephen Moretti May 03 '11 at 20:57
  • I always use upper case for scoping. It's not required. Since I have nothing else in all caps, it makes the scope easier to read. – Evik James May 04 '11 at 18:20

1 Answers1

3

The local scope is defined only within functions.

function a() {
  //this way
  var myVariable = 0;
  //is the same as this way (in CF9)
  local.myVariable = 0;
}

Do not use the local scope outside functions.

Outside functions, variables default to the variables scope.

//this way
myVariable = 0;
//is the same as this way
variables.myVariable = 0;
yfeldblum
  • 1,542