<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Why you need to VAR-scope your variables</title>
	<atom:link href="http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/</link>
	<description>Web Application Engineers</description>
	<lastBuildDate>Fri, 12 Mar 2010 19:25:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Hamish Charles</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-458</link>
		<dc:creator>Hamish Charles</dc:creator>
		<pubDate>Wed, 07 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-458</guid>
		<description>Yes, I didn&#039;t say that explicitly James but the default would need to be false. Also it might be useful as a cf administrator setting, cfapplication setting, or cfsettings parameter, or all three.

Another idea occurred to me in bed last night as I was dropping off to sleep. To help us cope with the current situation and avoid having to go back to the top of the function everytime a new variable is required: 

Declare &lt;cfset var my = StructNew()&gt;. Then use this structure for any local simple variables or queries: &lt;cfquery name=&quot;my.qryNew&quot;...&gt;, &lt;cfloop index=&quot;my.i&quot;....&gt; etc.. You might still want to declare separate var scope variables for your return structure or other complex objects. 

This idea came out of what Jaime Metcher said above about the way Perl uses &#039;my&#039; as a way of declaring local variables.</description>
		<content:encoded><![CDATA[<p>Yes, I didn&#8217;t say that explicitly James but the default would need to be false. Also it might be useful as a cf administrator setting, cfapplication setting, or cfsettings parameter, or all three.</p>
<p>Another idea occurred to me in bed last night as I was dropping off to sleep. To help us cope with the current situation and avoid having to go back to the top of the function everytime a new variable is required: </p>
<p>Declare &lt;cfset var my = StructNew()&gt;. Then use this structure for any local simple variables or queries: &lt;cfquery name=&quot;my.qryNew&quot;&#8230;&gt;, &lt;cfloop index=&quot;my.i&quot;&#8230;.&gt; etc.. You might still want to declare separate var scope variables for your return structure or other complex objects. </p>
<p>This idea came out of what Jaime Metcher said above about the way Perl uses &#8216;my&#8217; as a way of declaring local variables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan G. Switzer, II</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-446</link>
		<dc:creator>Dan G. Switzer, II</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-446</guid>
		<description>@Steve:

You wrote:

&quot;See i think they can modify the var keyword without breaking everyone&#039;s code. If they basically deprecated the var keyword so it did nothing i.e.: &lt;cfset var myvariable=&quot;hello world&quot;&gt; is the EXACT same thing as &lt;cfset myvariable=&quot;hello world&quot;&gt; how many apps would that actually break? I&#039;d be willing to bet it would break exactly zero.&quot;

Actually, ever single CFC I&#039;ve ever written would break.

You see I use the &quot;instance&quot; struct variable as a way to declare private data for my CFCs. All my getters and setters have keys in the &quot;instance&quot; struct.

So, my code looks like:

&lt;cfcomponent&gt;
  &lt;cfset instance = structNew() /&gt;
  &lt;cfset instance.private = &quot;Dan Switzer&quot; /&gt;

  &lt;cffunction name=&quot;getPrivate&quot; returntype=&quot;string&quot;&gt;
    &lt;cfreturn instance.private /&gt;
  &lt;/cffunction&gt;

  &lt;cffunction name=&quot;setPrivate&quot; returntype=&quot;void&quot;&gt;
    &lt;cfargument name=&quot;arguments.input&quot; /&gt;
    &lt;cfset instance.private = arguments.input /&gt;
    &lt;cfreturn /&gt;
  &lt;/cffunction&gt;
&lt;/cfcomponent&gt;

If all variables suddenly become local variables only, I have no way to access the &quot;instance&quot; variable. I don&#039;t want to use the &quot;this&quot; scope, because that&#039;s a public property accessible from the pages that invoke the component. I want my data private and only accessible to my CFC logic.

So, while the &quot;var&quot; keyword definitely feels kludgey, it has its uses. Yes, they could have solved it a different way, but that road was crossed along time ago.

As Mark Mandel pointed out, I just wish I could &quot;var&quot; a variable anywhere w/in my function (like I can in most other scripting languages.) That certainly would help me to keep my bugs down from unscoped variables.</description>
		<content:encoded><![CDATA[<p>@Steve:</p>
<p>You wrote:</p>
<p>&quot;See i think they can modify the var keyword without breaking everyone&#8217;s code. If they basically deprecated the var keyword so it did nothing i.e.: &amp;lt;cfset var myvariable=&quot;hello world&quot;&amp;gt; is the EXACT same thing as &amp;lt;cfset myvariable=&quot;hello world&quot;&amp;gt; how many apps would that actually break? I&#8217;d be willing to bet it would break exactly zero.&quot;</p>
<p>Actually, ever single CFC I&#8217;ve ever written would break.</p>
<p>You see I use the &quot;instance&quot; struct variable as a way to declare private data for my CFCs. All my getters and setters have keys in the &quot;instance&quot; struct.</p>
<p>So, my code looks like:</p>
<p>&lt;cfcomponent&gt;<br />
  &lt;cfset instance = structNew() /&gt;<br />
  &lt;cfset instance.private = &quot;Dan Switzer&quot; /&gt;</p>
<p>  &lt;cffunction name=&quot;getPrivate&quot; returntype=&quot;string&quot;&gt;<br />
    &lt;cfreturn instance.private /&gt;<br />
  &lt;/cffunction&gt;</p>
<p>  &lt;cffunction name=&quot;setPrivate&quot; returntype=&quot;void&quot;&gt;<br />
    &lt;cfargument name=&quot;arguments.input&quot; /&gt;<br />
    &lt;cfset instance.private = arguments.input /&gt;<br />
    &lt;cfreturn /&gt;<br />
  &lt;/cffunction&gt;<br />
&lt;/cfcomponent&gt;</p>
<p>If all variables suddenly become local variables only, I have no way to access the &quot;instance&quot; variable. I don&#8217;t want to use the &quot;this&quot; scope, because that&#8217;s a public property accessible from the pages that invoke the component. I want my data private and only accessible to my CFC logic.</p>
<p>So, while the &quot;var&quot; keyword definitely feels kludgey, it has its uses. Yes, they could have solved it a different way, but that road was crossed along time ago.</p>
<p>As Mark Mandel pointed out, I just wish I could &quot;var&quot; a variable anywhere w/in my function (like I can in most other scripting languages.) That certainly would help me to keep my bugs down from unscoped variables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: robs</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-447</link>
		<dc:creator>robs</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-447</guid>
		<description>2nd world war wasn&#039;t the end either</description>
		<content:encoded><![CDATA[<p>2nd world war wasn&#8217;t the end either</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Nelson</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-448</link>
		<dc:creator>Steve Nelson</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-448</guid>
		<description>I don&#039;t think it would break your app Dan, assuming it&#039;s built in a smart way. That instance struct was defined outside of any single cffunction. Seems to me that it should be &#039;local&#039; to the entire CFC, not a single single function.

Whereas if you had done this:

&lt;cffunction name=&quot;getPrivate&quot; &gt;
&lt;cfset instance = structNew() /&gt;
&lt;cfset instance.private = &quot;Dan Switzer&quot; /&gt;
&lt;/cffunction&gt;

Then the instance variable *should* be local to that one function. As if you had typed: &lt;cfset var instance = structNew()/&gt;

Since we&#039;re just dreaming here, to make things even more fun...

&lt;cffunction name=&quot;getPrivate&quot; &gt;
&lt;cfset instance = structNew() /&gt;
&lt;cfset instance.private = &quot;Dan Switzer&quot; /&gt;
&lt;cfreturn this/&gt;
&lt;/cffunction&gt;

I think when a function returns THIS, then the local (function level) &quot;instance&quot; variable should become local (CFC level, still not public from the calling template). In other words when you return THIS it should work just like when you defined it before cffunctions.

Granted, this is all just a pipe dream and it&#039;s never going to happen.

Well... not with that attitude.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think it would break your app Dan, assuming it&#8217;s built in a smart way. That instance struct was defined outside of any single cffunction. Seems to me that it should be &#8216;local&#8217; to the entire CFC, not a single single function.</p>
<p>Whereas if you had done this:</p>
<p>&lt;cffunction name=&quot;getPrivate&quot; &gt;<br />
&lt;cfset instance = structNew() /&gt;<br />
&lt;cfset instance.private = &quot;Dan Switzer&quot; /&gt;<br />
&lt;/cffunction&gt;</p>
<p>Then the instance variable *should* be local to that one function. As if you had typed: &lt;cfset var instance = structNew()/&gt;</p>
<p>Since we&#8217;re just dreaming here, to make things even more fun&#8230;</p>
<p>&lt;cffunction name=&quot;getPrivate&quot; &gt;<br />
&lt;cfset instance = structNew() /&gt;<br />
&lt;cfset instance.private = &quot;Dan Switzer&quot; /&gt;<br />
&lt;cfreturn this/&gt;<br />
&lt;/cffunction&gt;</p>
<p>I think when a function returns THIS, then the local (function level) &quot;instance&quot; variable should become local (CFC level, still not public from the calling template). In other words when you return THIS it should work just like when you defined it before cffunctions.</p>
<p>Granted, this is all just a pipe dream and it&#8217;s never going to happen.</p>
<p>Well&#8230; not with that attitude.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan G. Switzer, II</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-449</link>
		<dc:creator>Dan G. Switzer, II</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-449</guid>
		<description>@Steve:

The problem, as I see it, is that the setter by default would set a local variable--not the global variable. How is CF supposed to know that you want to set the global and not a local variable? Anything you do to automate the process brings in scoping issues.

At least w/the var keyword you have explicit control over whether the variable is global or local to the current function.

I guess the thing that doesn&#039;t bother me is that most other scripting language would behave this way--which is probably why Macromedia choose this method. The problem is, CF isn&#039;t really like most other scripting languages.</description>
		<content:encoded><![CDATA[<p>@Steve:</p>
<p>The problem, as I see it, is that the setter by default would set a local variable&#8211;not the global variable. How is CF supposed to know that you want to set the global and not a local variable? Anything you do to automate the process brings in scoping issues.</p>
<p>At least w/the var keyword you have explicit control over whether the variable is global or local to the current function.</p>
<p>I guess the thing that doesn&#8217;t bother me is that most other scripting language would behave this way&#8211;which is probably why Macromedia choose this method. The problem is, CF isn&#8217;t really like most other scripting languages.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Nelson</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-450</link>
		<dc:creator>Steve Nelson</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-450</guid>
		<description>I&#039;m not sure why this would be difficult for Adobe to build.

A variable cfset above the functions is local to the CFC. A variable set inside of a function checks to see if it already exists at the CFC level, if so it updates the value and keeps it at that level. If it does not exist at the CFC level it automatically gets var&#039;d behind the scenes.

Is this really any more complex than a single (maybe a few) if statements? I can picture how to do the logic in my head using structkeyexists().

If CF *were* like other scripting languages why would we pay $1200 for a copy of it?</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure why this would be difficult for Adobe to build.</p>
<p>A variable cfset above the functions is local to the CFC. A variable set inside of a function checks to see if it already exists at the CFC level, if so it updates the value and keeps it at that level. If it does not exist at the CFC level it automatically gets var&#8217;d behind the scenes.</p>
<p>Is this really any more complex than a single (maybe a few) if statements? I can picture how to do the logic in my head using structkeyexists().</p>
<p>If CF *were* like other scripting languages why would we pay $1200 for a copy of it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Kotek</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-451</link>
		<dc:creator>Brian Kotek</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-451</guid>
		<description>Steve what you don&#039;t seem to be acknowledging here is that anyone who was already relying on the fact that unscoped variables go into the variables scope would have their apps explode. You do see this, right?</description>
		<content:encoded><![CDATA[<p>Steve what you don&#8217;t seem to be acknowledging here is that anyone who was already relying on the fact that unscoped variables go into the variables scope would have their apps explode. You do see this, right?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Bryant</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-452</link>
		<dc:creator>Steve Bryant</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-452</guid>
		<description>Nelson,

I think the approach that you are suggesting makes thing less consistent. It smacks of having the language make decisions/assumptions for me.

Not only that, but it could still break code. Many times variables-scoped variables are first set in an init() method. Now, mine are all explicitly put in variables scope for clarity, but there is bound to be plenty of code where that isn&#039;t the case.</description>
		<content:encoded><![CDATA[<p>Nelson,</p>
<p>I think the approach that you are suggesting makes thing less consistent. It smacks of having the language make decisions/assumptions for me.</p>
<p>Not only that, but it could still break code. Many times variables-scoped variables are first set in an init() method. Now, mine are all explicitly put in variables scope for clarity, but there is bound to be plenty of code where that isn&#8217;t the case.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Nelson</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-453</link>
		<dc:creator>Steve Nelson</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-453</guid>
		<description>Yes I understand that Brian. I was referring specifically to Dan&#039;s issue with setting an &quot;instance&quot; struct above the functions. 

Steve (Bryant), CF makes all sorts of decisions and assumptions for you. If I thought hard enough for 5 minutes I could probably list out 50 assumptions it makes for you.</description>
		<content:encoded><![CDATA[<p>Yes I understand that Brian. I was referring specifically to Dan&#8217;s issue with setting an &quot;instance&quot; struct above the functions. </p>
<p>Steve (Bryant), CF makes all sorts of decisions and assumptions for you. If I thought hard enough for 5 minutes I could probably list out 50 assumptions it makes for you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Kotek</title>
		<link>http://www.webapper.com/blog/index.php/2007/02/05/why-you-need-to-varscope-your-variables/comment-page-1/#comment-454</link>
		<dc:creator>Brian Kotek</dc:creator>
		<pubDate>Tue, 06 Feb 2007 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.webapper.net/blog/index.cfm/2007/6/29/Why-you-need-to-VARscope-your-variables#comment-454</guid>
		<description>OK cool Steve, your post just didn&#039;t seem to be taking that into account and I wanted to be sure. Thx.</description>
		<content:encoded><![CDATA[<p>OK cool Steve, your post just didn&#8217;t seem to be taking that into account and I wanted to be sure. Thx.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
