<p>In a ColdFusion Component (CFC), is it necessary to use fully qualified names for variables-scoped variables?</p>

<p>Am I going to get myself into trouble if I change this:</p>

<pre><code>&lt;cfcomponent&gt;
    &lt;cfset variables.foo = "a private instance variable"&gt;

    &lt;cffunction name = "doSomething"&gt;
        &lt;cfset var bar = "a function local variable"&gt;
        &lt;cfreturn "I have #variables.foo# and #bar#."&gt;
    &lt;/cffunction&gt;
&lt;/cfcomponent&gt;
</code></pre>

<p>to this?</p>

<pre><code>&lt;cfcomponent&gt;
    &lt;cfset foo = "a private instance variable"&gt;

    &lt;cffunction name = "doSomething"&gt;
        &lt;cfset var bar = "a function local variable"&gt;
        &lt;cfreturn "I have #foo# and #bar#."&gt;
    &lt;/cffunction&gt;
&lt;/cfcomponent&gt;
</code></pre>

[{'answer_id': 59554, 'author': 'Soldarnal', 'author_id': 3420, 'author_profile': 'https://Stackoverflow.com/users/3420', 'pm_score': 4, 'selected': True, 'text': '<p>It won\'t matter to specify "variables" when you create the variable, because foo will be placed in the variables scope by default; but it will matter when you access the variable.</p>\n\n<pre><code>&lt;cfcomponent&gt;\n    &lt;cfset foo = "a private instance variable"&gt;\n\n    &lt;cffunction name="doSomething"&gt;\n        &lt;cfargument name="foo" required="yes"/&gt;\n        &lt;cfset var bar = "a function local variable"&gt;\n        &lt;cfreturn "I have #foo# and #bar#."&gt;\n    &lt;/cffunction&gt;\n\n    &lt;cffunction name="doAnotherThing"&gt;\n        &lt;cfargument name="foo" required="yes"/&gt;\n        &lt;cfset var bar = "a function local variable"&gt;\n        &lt;cfreturn "I have #variables.foo# and #bar#."&gt;\n    &lt;/cffunction&gt;\n\n&lt;/cfcomponent&gt;\n</code></pre>\n\n<p>doSomething("args") returns "I have <strong>args</strong> and a <strong>function local variable</strong>"</p>\n\n<p>doAnotherThing("args") returns "I have <strong>a private instance of a variable</strong> and a <strong>function local variable</strong>."</p>\n'}, {'answer_id': 59566, 'author': 'Adam Tuttle', 'author_id': 751, 'author_profile': 'https://Stackoverflow.com/users/751', 'pm_score': 2, 'selected': False, 'text': '<p>The short answer to your question is that no, you will probably not run into trouble attempting to do that. Outside the context of a UDF (even still inside a CFC), an un-scoped set statement implies the variables scope.</p>\n\n<p>In addition, in a CFC, the Variables scope is available to all of its functions; it is sort of the global scope within that CFC -- similar to the "this" scope, except variables scope is akin to "private" variables, whereas the this scope is akin to public variables.</p>\n\n<p>To test this, create test.cfc:</p>\n\n<pre><code>&lt;cfcomponent&gt;\n    &lt;cfset foo = "bar" /&gt;\n    &lt;cffunction name="dumpit" output="true"&gt;\n        &lt;cfdump var="#variables#" label="cfc variables scope"&gt;\n        &lt;cfdump var="#this#" label="cfc this scope"&gt;\n    &lt;/cffunction&gt;\n&lt;/cfcomponent&gt;\n</code></pre>\n\n<p>and a page to test it, test.cfm:</p>\n\n<pre><code>&lt;cfset createObject("component", "test").dumpit() /&gt;\n</code></pre>\n\n<p>And the results will be:</p>\n\n<p><img src="https://i.stack.imgur.com/LSoL6.png"/></p>\n\n<hr>\n\n<p>Now, to address another problem I see in your example code...</p>\n\n<p>In CF, all User Defined Functions have a special un-named scope commonly referred to as the "var" scope. If you do the following inside a UDF:</p>\n\n<pre><code>&lt;cfset foo = "bar" /&gt;\n</code></pre>\n\n<p>Then you are telling CF to put that variable into the var scope.</p>\n\n<p>To compound things a bit, you can run into problems (variable values changing when you weren\'t expecting them to) when you are <strong><em>not</em></strong> using the var scope in your inline UDFs.</p>\n\n<p>So the rule of thumb is to always, Always, ALWAYS, <strong><em>ALWAYS</em></strong> var-scope your function-internal variables (including query names). There is a tool called <a href="http://varscoper.riaforge.org/" rel="nofollow noreferrer">varScoper</a> that will assist you in finding variables that need to be var-scoped. Last I checked it wasn\'t perfect, but it\'s definitely a start.</p>\n\n<p>However, it is a <em>bad</em> idea to reference (display/use) variables without a scope (obviously excepting var-scoped variables, as you can\'t specify the scope to read from) in CFCs or even on your standard CFM pages. As of CF7, there were 9 scopes that were checked in a specific order when you read a variable without specifying the scope, first match wins. With CF8, there could be more scopes in that list, I haven\'t checked. When you do this, you run the risk of getting a value from one scope when you are expecting it from another; which is a nightmare to debug... I assure you. ;)</p>\n\n<p>So in short: <em>implying</em> a variable\'s scope (on set) is not a terrible idea (though I usually specify it anyway); but <em>inferring</em> variable\'s scope (on read) is asking for trouble.</p>\n'}, {'answer_id': 59891, 'author': 'Patrick McElhaney', 'author_id': 437, 'author_profile': 'https://Stackoverflow.com/users/437', 'pm_score': 0, 'selected': False, 'text': '<p>After reading your answers here\'s what I\'m thinking:</p>\n\n<p><strong>Yes, it\'s safe. In general, it\'s not necessary or useful to explicitly specify the variables scope. It just adds clutter to an already verbose language.</strong></p>\n\n<p>Granted, there is one minor exception, as <a href="https://stackoverflow.com/users/3420/soldarnal">Soldarnal</a> pointed out, where qualifying a variables-scoped variable is required. That is if you have a function local variable with the same name. (But you probably shouldn\'t do that anyway.)</p>\n'}, {'answer_id': 62989, 'author': 'Bill Rawlinson', 'author_id': 7329, 'author_profile': 'https://Stackoverflow.com/users/7329', 'pm_score': 1, 'selected': False, 'text': '<p>The simple answer to your question is:\n"NO, it isn\'t necessary"</p>\n\n<p>However, I think best practices would suggest that you do, in fact, use the variables indentifier when accessing those variables.  In my opinion anyone who comes upon your code in the future, and is looking in the middle of a function, will instantly know the scoping of the variable without having to scan the top of the function the local functions.</p>\n\n<p>In fact, I add a little extra verbosity to my CFC UDFs by creating one local struct:</p>\n\n<p>&lt;cfset var local = structNew() /&gt;</p>\n\n<p>Then I put all my local vars in that struct and reference them that way so my code will look something like this:</p>\n\n<p>&lt;cfset local.foo = variables.bar + 10 /&gt;</p>\n'}, {'answer_id': 65112, 'author': 'Dan Wilson', 'author_id': 8823, 'author_profile': 'https://Stackoverflow.com/users/8823', 'pm_score': 3, 'selected': False, 'text': "<p>Especially in CFCs, proper scoping is important. The extra 'verbosity' is worth the clarity. \nHaving variables slip out of their indended scope will cause severe problems and very hard to diagnose.</p>\n\n<p>Verbosity isn't always a bad thing. We name our functions and methods in descriptive manners like getAuthenticatedUser(), rather than gau(). Database columns and tables are best left descriptive like EmployeePayroll rather than empprl. Thus, being terse might be 'easier' when your short term memory is full of the project details, but being descriptive shows your intent and is helpful during the maintenance phase of an application, long after your short term memory has been filled with other stuff.</p>\n"}, {'answer_id': 99253, 'author': 'ethyreal', 'author_id': 18159, 'author_profile': 'https://Stackoverflow.com/users/18159', 'pm_score': 0, 'selected': False, 'text': "<p>Best practices aside, i believe it could also depend on how your going to access your cfc's i have not had any problems leaving them out when creating objects and accessing them from coldfusion.  However i think it might be needed when accessing and/or mapping them remotely via actionscript in flex/flash. </p>\n"}, {'answer_id': 128842, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ll say Yes. Is it explicitly necessary? Nope. Can you get away with not doing it? Sure. Are you asking for trouble? Absolutely. If you have the following inside a cffunction:</p>\n\n<pre><code>&lt;cfset foo = "bar" /&gt;\n</code></pre>\n\n<p>That will not place that variable in the function local var scope, it will place it in the CFC\'s global VARIABLES scope, meaning that it is available to every method of that CFC. There are times when you may want to do this, but most of the time you\'d be asking for a race condition.</p>\n\n<p>When any variable is being read by the server, if that variable is not explicity declared as part of a scope (REQUEST., SESSION., etc.) then ColdFusion will run ScopeCheck() to determine which scope the variable is in. Not only is this placing unnecessary overhead on your application server, it also introduces the ability for hijacking, whereby your variable is in one scope, but ScopeCheck() has found a variable of the same name higher in the precedence order.</p>\n\n<p>Always, always, ALWAYS, scope all variables. No matter how trivial. Even things like query names and looping indexes. Save yourself, and those that come behind you, from the pain.</p>\n'}, {'answer_id': 131137, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Here\'s a very good <a href="http://www.coldfusionjedi.com/downloads/cfcscopes.pdf" rel="nofollow noreferrer">CFC scope reference</a> from Raymond Camden.\nPersonally, I prefer to make a \'self\' hash to avoid all confusion (notice I don\'t use the \'variables\' scope in the functions):</p>\n\n<pre><code>&lt;cfcomponent&gt;\n  &lt;cfset variables.self = structNew()&gt;\n  &lt;cfscript&gt;\n    structInsert(variables.self, &lt;key&gt;, &lt;value&gt;);\n    ...\n  &lt;/cfscript&gt;\n\n  &lt;cffunction name="foo"&gt;\n    self.&lt;key&gt; = &lt;value&gt;\n    &lt;cfreturn self.&lt;key&gt; /&gt;\n  &lt;/cffunction&gt;\n\n  ...\n</code></pre>\n'}, {'answer_id': 148409, 'author': 'Matt Woodward', 'author_id': 3612, 'author_profile': 'https://Stackoverflow.com/users/3612', 'pm_score': 2, 'selected': False, 'text': "<p>Not explicitly scoping in the variables scope may work, but it's not a good idea, and honestly the only reason <em>not</em> to is out of laziness IMO. If you explicitly scope everything 1) you avoid potential issues, and 2) it makes the code easier to read because there's no question which scope things are in.</p>\n\n<p>To me it doesn't make the code more verbose (and certainly not unnecessarily verbose)--it's actually easier to read, avoids confusion, and avoids weird side effects that may crop up if you don't explicitly scope.</p>\n"}]
['https://Stackoverflow.com/questions/59390', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/437/']