Henry
Henry

Reputation: 32885

Are Variables & This considered implicit scopes inside a CFC in CF2016?

What does it take, to take advantage of the performance gain from ColdFusion 2016's new setting searchImplicitScopes="false":

... bypasses searching for a variable in the implicit scopes thus finding the variables defined in the application faster.

Should we start scoping Variables & This scopes inside a CFC with searchImplicitScopes="false" in ColdFusion 2016?

I cannot find any documentation on what are considered implicit scopes in CF2016. I am pretty sure local and arguments scopes are fine inside a function, but what about frequently used scopes like variables and this in a CFC?

Upvotes: 3

Views: 545

Answers (2)

Henry
Henry

Reputation: 32885

Just installed CF 2016 Express and I can conclude that with searchImplicitScopes="false", the Variables scope is still being searched when a variable is not scoped, but not the This scope.

Upvotes: 1

Adrian J. Moreno
Adrian J. Moreno

Reputation: 14859

Scope all the things!!!

The variables scope inside a CFC is global to all functions inside the CFC.

The this scope inside a CFC is global to all functions inside the CFC and can also be referenced from the caller of the CFC.

If you do not scope a variable inside a CFC, it defaults to the variables scope.

<--- this_test.cfc --->
<cfcomponent>
    <cfset variables.foo = "This is my CFC global variable." />
    <cfset this.bar = "This variable is global to my CFC and can be referenced externally." />
    <cfset narf = "Global variable! Point!" />
    <cffunction name="getNarf" access="public">
        <cfreturn narf />
    </cffunction>
</cfcomponent>

Test call:

<cfset test = new this_test() />

<cfoutput> <li>#test.foo#</li> <li>#test.bar#</li> <li>#test.narf#</li> <li>#test.getNarf()#</li> </cfoutput>

  1. foo is in the variables scope, you'll get an error: Element FOO is undefined in TEST.
  2. bar is in the this scope, so you get the output for test.bar.
  3. narf is in the variables scope, you'll get an error: Element NARF is undefined in TEST.
  4. The only way to get the value of narf is to have a function return it.

Update

That searchImplicitScopes can turn off CF's scope search capability. So if you don't scope something, it won't look up the hierarchy of scopes. Question is, does an un-scoped variable end up in the variables scope by default? I would say, yes, as that's been the default since CFCs debuted.

Regardless of using that setting, I still say scope everything. The whole reason they created the implicit local scope was because:

  1. developers weren't used to var scoping function local variables
  2. developers that were, tended to do var local = structNew(), so they only had to var scope one variable.
  3. 2 also gave them the ability to return the collection of function local variables.
  4. As of CF 9, when var a = 0 became the same as local.a = 0, you could remove all instances of var local = structNew(), so long as you also "scoped" and referenced those private variables as local.a.

The bottom line

is that there is some performance overhead if CF has to look up variable scopes on every request. If you turned off that lookup using searchImplicitScopes=false, you should get some performance boost. But then, that should really rely on your application and average request load.

Upvotes: 3

Related Questions