Blog

Importance of Var Scoping

Luis Majano September 24, 2010

Spread the word

Luis Majano

September 24, 2010

Spread the word


Share your thoughts

I can honestly say that there are always requests on the ColdBox lists or work that say, "Hey the application is taking my server memory, why does ColdBox do that!".  First of all, the ColdBox platform when placed in memory is less than 1kb of memory.  The rest that gets loaded into an application is because of your application code.  Second, the biggest culprit is the misuse or the not using of var scoped variables in event handlers or domain objects.

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?

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(); }

Recent Entries

qb 9.0.0 Released

qb 9.0.0 Released

A new major release of qb is now available on ForgeBox! This comes jam-packed with awesome features like new SQLite grammar support and SQLCommenter support to add contextual comments to your queries. Breaking changes include dropping support for older Adobe ColdFusion versions, splitting the uuid SchemaBuilder method, pagination changes when maxRows is 0 or lower, and changes to some default settings.

Eric Peterson
Eric Peterson
February 06, 2023
CBSecurity 3.x Released

CBSecurity 3.x Released

We are incredibly excited to release CBSecurity 3. This is a significant release with over six months of work invested in it. We have completely revamped our security module to make ColdBox applications secure, flexible, and ready for the upcoming ColdBox 7 release. The first major announcement for this release is that we have a brand-new logo!

Luis Majano
Luis Majano
January 30, 2023
CBWIRE 2.2 Released

CBWIRE 2.2 Released

We're excited to announce the release of CBWIRE 2.2. This release includes several added enhancements, such as new lifecycle hooks and simplified Turbo Drive integration. We changed the previous lifecycle hook of mount() to onMount() to be consistent, and several bugs were squashed.

Maria Jose Herrera
Maria Jose Herrera
January 09, 2023