This is KEY! Event handlers in ColdBox are cached by default so no unnecessary object creations occur. Therefore, your code within the event handler functions MUST be thread safe and var scoped. This is essential and key for ANY framework or ANY ColdFusion Component declaration, ALWAYS ALWAYS var scope. I cannot stress the importance of having to var scope your ColdFusion component method calls. I see this time and time again and we need to educated ourselves about it and also create a habit of ALWAYS var scoping our variables within function declarations. What do you think?
Blog
This is KEY! Event handlers in ColdBox are cached by default so no unnecessary object creations occur. Therefore, your code within the event handler functions MUST be thread safe and var scoped. This is essential and key for ANY framework or ANY ColdFusion Component declaration, ALWAYS ALWAYS var scope. I cannot stress the importance of having to var scope your ColdFusion component method calls. I see this time and time again and we need to educated ourselves about it and also create a habit of ALWAYS var scoping our variables within function declarations. What do you think?
Recent Entries
BoxLang 1.14.0 : BoxLang Ranges Part 1: First-Class Intervals, Zero Compromises
There is a moment in every language's evolution when a convenience syntax grows up and becomes something worth thinking about. For BoxLang, that moment arrived with ranges. We had always wanted to be able to do this in our CFML apps, and we finally can
BoxLang 1.14.0 : Local Template Classes - Define Classes Right Where You Need Them
BoxLang 1.14 ships with one of the most developer-friendly OOP features we've built yet: local template classes. If you've ever created a throwaway .bx file just to hold a five-line helper class, this one's for you.
MatchBox and WebAssembly: Running BoxLang in the Browser and at the Edge
The MatchBox open beta is live at https://boxlang.ortusbooks.com/boxlang-framework/matchbox, and it brings something genuinely new to the BoxLang ecosystem: a path into WebAssembly.
That means BoxLang code can now move into browser applications, static-site deployments, edge runtimes, and WASI-style containers - without requiring a JVM. The feature is still beta, but the core direction is already useful: write BoxLang, compile it with MatchBox, and ship the generated WASM artifact to wherever a small portable runtime makes sense.
Add Your Comment
(3)
Sep 24, 2010 17:49:38 UTC
by Adrian J. Moreno
A few links with more information on var scoping:
My blog:
http://www.iknowkungfoo.com/blog/index.cfm/2007/8/22/Object-Oriented-Coldfusion--4--The-var-scope
Dave Shuck:
http://daveshuck.instantspot.com/blog/2006/11/28/Thread-safety-example--Var-scope-your-loop-index-in-ColdFusion-CFCs
Tyson Vanek:
http://www.webapper.net/index.cfm/2007/2/9/The-REAL-reason-you-need-to-varscope-your-local-CFC-function-variables
Sep 30, 2010 10:28:54 UTC
by Allen
Not to be trite but the var keyword is a keyword, not a scope. Most of know that but I'd hate to see a newbie get confused.
Do you have some example code for a handler or controller that would help illustrate this?
And is there any reason if we're running CF9 why we shouldn't use the local scope in these situations?
TIA!
Sep 30, 2010 10:35:10 UTC
by Luis Majano
@Allen,
In cf9 you don't have to var scope if you use the local scope as that puts the variables in the local function scope. To me that is a matter of preference, like what color you like? They both achieve the same results.
Some example handler code of NOT var scoping:
function doThis(event){
rc = event.getCollection();
rc.data = service.getData();
}