Skip to content

Using scopes explicitly in code

jameskilford edited this page Apr 15, 2014 · 7 revisions

Railo recommendation

Railo has delivered very many presentations about performance and code readability. Here is what they recommend to any of their customers and programmers:

  • Scope everything BUT the closest scope
    • In Functions this is the local scope
    • In templates it is the variables scopt
    • The above two do not scope

This is a simple rule to follow and therefore it is easy to understand. In order to support this pattern in Railo, just turn off scope cascading and that will help you scope accordingly.

  • If you use the variables scope in Adobe ColdFusion even in Templates, this decreases the performance of variable accesses by a factor of 4!
    • This is because ACF assumes there might be a local variable called variables first and then assumes it is the variables scope

Performance

If you are NOT scoping your variables, and are worried about performance, this is a comparison between ACF and Railo and the access times for scoped vs. unscoped variables:

If you use

<cfoutput>#variables.a#</cfoutput>

in ACF it takses 459ms (in my example) if it is just

<cfoutput>#a#</cfoutput>

It takes 47ms

In general unscoped variables are slower (except for this example above which is incredibly different)

In Railo the difference is not too bad so it really doesn't matter, but as a general rule of thumb:

Scope everything, except the closest scope.

Clone this wiki locally