Calendar
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Recent Entries No recent entries.
Recent Comments
Webapper Consulting and SeeFusion Readers' Choice Awards
Daryl said: Congrats on your nomination. The wealth of information on this site shows your dedication to CF and...
[more]
Subversion hook to verify unit testing?
Oluseyi said: While Subversion hooks are pretty powerful, Subversion really needs a *policy* system. I tried to wr...
[more]
My First Vista Blue Screen! w00t!
Nezzy said: good grief, it was my first blue screen too and I didn't even do anything! Just doing what I normall...
[more]
Shan's Simple Examples: File uploads with Flex and ColdFusion
Shan said: Did you add uploadUrl to both spots in your HTML code that embeds the flash? There's two spots if I ...
[more]
Shan's Simple Examples: File uploads with Flex and ColdFusion
Go-Gulf said: I'm trying to pass in the value for uploadDestination, so it is not hard coded, and since I'm a newb...
[more]
Archives By Subject
Business of Software (5) [RSS]
ColdFusion (326) [RSS]
Conferences (7) [RSS]
Databases (88) [RSS]
Flex & Flash (109) [RSS]
Fusebox (87) [RSS]
General Development (33) [RSS]
Google (9) [RSS]
Hardware (5) [RSS]
JVM & Java (132) [RSS]
Linux (20) [RSS]
Macintosh (1) [RSS]
Miscellaneous (254) [RSS]
Performance (8) [RSS]
SeeFusion (36) [RSS]
Shan's Simple Examples (7) [RSS]
User Interface (3) [RSS]
Windows (5) [RSS]
Archives By Poster
Daryl Banttari (11)
Nat Papovich (33)
Patrick Quinn (36)
Shannon Hicks (22)
Steve Nelson (25)
Tyson Vanek (3)
The REAL reason you need to var-scope your local CFC function variables
So, Nat makes a great point that vars not specifically placed in the var scope become CFC instance variables. To be honest, I didn't even realize this myself until he pointed it out. However, the REAL reason you should be explicitly var-scoping your local variables in a CFC method has to do with performance.
Attached to this post, you'll find a small test application (varScopeTestApp.zip). The ColdFusion logic within the test application is quite simple - loop a specified number of times and set a simple variable equal to the current value of the loop counter. The test application compares 6 different methods of executing the exact same ColdFusion logic, and then displays the results. All you need to do to run the application is extract all of the contained files into a single directory on your application server, and then call the index.cfm page in your web browser. You can use the intLoopCount URL parameter in order to increase the size of the loop and really emphasize the performance difference. I would recommend a number around 500000.
The test application attempts to execute this same code concept using 6 different ColdFusion approaches.
- As inline ColdFusion code written with CF tags
- As inline ColdFusion code written within a CFSCRIPT block
- As a call to a CFC function written with CF tags without explicitly var-scoped variables
- As a call to a CFC function written within a CFSCRIPT block without explicitly var-scoped variables
- As a call to a CFC function written with CF tags with explicitly var-scoped variables
- As a call to a CFC function written within a CFSCRIPT block with explicitly var-scoped variables
In order to ensure that the execution time of each method is measured without being affected by concurrent thread resource contention, you'll find that each of the 6 test cases executes within a named exclusive lock. This provides for the most accurate measure of the true performance of the code being executed.
You'll make some interesting discoveries upon firing up this test application and seeing the results for yourself. Most notable, in my opinion, is the performance bulk added when the code is executed from within a CFC function without proper local var scoping. Going one step further, it would also seem that more bulk is added to the CFSCRIPT function than to the CF tag function. In my own tests, I found the CFSCRIPT function without var-scoped variables to be about 30% slower than the CF tag counterpart. And both CFC functions without explicitly var-scoped variables end up being roughly 2-3 times slower than the inline code.
Sure, we all despise the var keyword and the way that it has been implemented. And yes, we all agree that Adobe should introduce a new argument for the CFFUNCTION tag that would allow us to control the behavior of unscoped variables set within a CFC function. But until that magical revision arrives, we'll just need to bite the bullet and give into the var keyword for the sake of general application performance.


btw, i started upgrading Michael's "VarScoper" tool to handle EVERY potential CF7 tag along with handle included files inside of a cfc.
Suddenly this tool is going to become required for every CF developer. LAME!
I guess count me out of the 'we' that despise the var keyword.
What we are averse to is boneheaded language vocabulary decisions which are neither consistent nor well thought-out. The var keyword was not consistent with many many years of CFML language additions. What would have been consistent is a new scope called "local", which when applied inside a method body, acquires the same attributes of var-keyworded variables.
The big problem is that there are serious consequences to not properly and consistently using the var keyword in your variable declarations. As Tyson has shown, and as I will show in a future post, you can get in deep trouble if you don't always use it. And there are many (tens of? hundreds of?) thousands of examples of improperly var'ed variables in production code. Steve mentioned the "VarScoper" tool, which will correct those oversights in your source code.
<cfset var.myLocalVar = "foo">
or, as Nat has suggested
<cfset local.myLocalVar = "foo">
Nobody here is saying we're averse to declaring or scoping variables. I guess we're just griping about the unusual way that it was implemented based on the existing syntax of the language.
Part of the specific reason I worked on the test application that accompanies this post was to establish a proof of concept for a client. A couple of the ColdFusion developers at this client sort of brushed me off a little when I mentioned that the vars in their application code should be properly declared and scoped. One of their arguments to me was that ColdFusion's natural scope precedence will quite often eventually find the var in whatever scope it exists - which is, to some extent, true.
However, I wanted to demonstrate to these developers why that approach simply wasn't good enough. Aside from the cross-scope tangles they were susceptible to, I also knew that the cost of searching for these unscoped/undeclared variables would add up in the performance category. And since the client called us in to assist them with general performance problems, it was a bit frustrating when they didn't want to take the advice I was giving them. But, when I showed them this test application harness, they could actually look at solid proof supporting my recommendation to adopt the best practice of always scoping and declaring their variables.
http://www.coldfusionmaster.com/varScopeTestApp.zi...
That is the correct URL for downloading the test application. The .ZIP was updated at that location with the proper CFC (test.cfc) used by the application. You can download the same file by clicking on the "Download" link supplied in the line of links following the original post. Either way, you should end up with the test application. If not, please let me know.
(and again later to set that to the app scope - )
Worked great after that - thanks!
You're absolutely right. There's an orphaned reference to another CFC I had been using in a slightly modified version of this test application. I have corrected the files in the .ZIP so this won't be an issue for other users.
Thanks for pointing it out. :)
I downloaded the test. For linux, I had to rename
application.cfm to Application.cfm.
I had to put
<cfsetting enablecfoutputonly="yes" />
in the Application.cfm so that those *terrible cfc-white- spaces* didn't spoil the test.
in all tests:
cfscript is faster inline and in var-scoped-cfc.
cftags are faster on unscoped-cfc
http://myflowerz.info: