>From my [private] point of view, the scoping of 100% variables is a 
>programming extremism.  When some scope is implied, there is no reason to 
>scope variables, unless it is absolutely necessary, like in CFQUERY-related 
>loops, or using functions with side effect(s).  Or if this is a policy of your 
>organization (strange places may have strange rules).
This is similar to requiring the use of parenthesizes in arithmetic expressions 
in all cases, instead of relying on defined functions precedence rules.  Or to 
prohibit the use of "a++", allowing only the use of "a=a+1" in C code for 
"readability" reasons.

Moreover, I always thought that for a default scope, there is no performance 
gain, if I use fully qualified references, because the default namespace is 
always checked first, regradless of what some people say that, if you don't use 
scoping, some overhead is aways involved.  So, I wrote a simple test below.  No 
functions, no CFCs, just plain page code:


<cfset a=1>
<cfset b=2>

<!--- This is how most people would write this --->
<cfset c=0>
<cfset stTime=GetTickCount()>
<cfloop index="i" from="1" to="100000">
     <cfset c=a+b>
</cfloop>
<cfoutput>No scope: c=#c#, time=#GetTickCount()-stTime#<br></cfoutput>

<!--- This suppose to be more "readable" and "safe" --->
<cfset c=0>
<cfset stTime=GetTickCount()>
<cfloop index="i" from="1" to="100000">
     <cfset variables.c=variables.a+variables.b>
</cfloop>
<cfoutput>Scope: c=#c#, time=#GetTickCount()-stTime#<br></cfoutput>


I would expect exactly the same or, at least, very close results.  But, results 
are so unexpected, that I was clicking the Refresh button of my browser for 10 
minutes, like crazy.  Non-scoped version ALWAYS runs 5-15 times FASTER than the 
scoped version!  This is on CF8, 32-bit, 4 CPU Dell Server, Windows 2003.  
Could somebody run this on CF9 32/64 bit?  Any ideas, how it can be?


Remembering scope precedence rules in CF8, I wrote another test for a function 
with a side effect.  Therefore, the explicit scoping of external references 
suppose to increase the performance.  Right?  So, this is the test:


<cffunction name="test1">
   <cfset var i=0>
   <cfset var stTime=GetTickCount()>
   <cfloop index="i" from="1" to="100000">
      <cfset variables.c=a+b>
   </cfloop>
   <cfoutput>Function no scope: #GetTickCount()-stTime#<br></cfoutput>
</cffunction>


<cffunction name="test2">
   <cfset var i=0>
   <cfset var stTime=GetTickCount()>
   <cfloop index="i" from="1" to="100000">
      <cfset variables.c=variables.a+variables.b>
   </cfloop>
   <cfoutput>Function scope: #GetTickCount()-stTime#<br></cfoutput>
</cffunction>


<cfset a=1>
<cfset b=2>

<cfset c=0>
<cfset test1()>
<cfoutput>c=#c#<br></cfoutput>

<cfset c=0>
<cfset test2()>
<cfoutput>c=#c#<br></cfoutput>


Non-scoped version ALWAYS runs 2-3 times FASTER than the scoped one.  So, at 
least on my server, the claim that scoping "not only increases readability, but 
also gives a performance gain" looks like "not entirely accurate" (c).

So, scope as less, as you can??!


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:344818
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

Reply via email to