(a) What cases should you var scope variables and (b) what cases should you not var scope in a ColdFusion components?
Variables and functions should be declared in the minimum scope from which all references to the identifier are still possible. When a larger scope than necessary is used, code becomes less readable, harder to maintain, and more likely to reference unintended variables (see DCL01-C.
The VARIABLES scope is the default scope in ColdFusion. i.e. if a variable is declared in . cfm and . cfc file without explicitly prefixing a scope, or without 'var' to a variable inside a function, ColdFusion assigns VARIABLES scope to that variable.
You should var scope your variables when you're implementing a function inside a CFC that is shared across multiple requests (i.e. Singleton, Service CFC's in Application scope)
You don't need to (yet still highly recommended to) var scope your variables if the CFC is instantiated every time, AND your method is not calling another method in the same CFC that may access the vars you've defined in the caller method. Such as a remote method that you called directly through web service or ajax, which doesn't call other methods that make use of vars you didn't var scope, or Controller in CFWheels.
"You should always define function-local variables using the var keyword." per CFC variables and scope doc http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=buildingComponents_29.html
You should var scope your variables any time you do not want the value of that variable to be affected by a) other requests accessing the same instance, or b) other methods within the same instance.
Henry is a great guy, but his statement that "You don't need to var scope your variables if the CFC is instantiated every time." is incorrect. :) [EDIT: Henry has since edited his answer] I wrote an example that illustrates this point in this blog entry:
http://daveshuck.com/2006/11/28/thread-safety-example-var-scope-your-loop-index-in-coldfusion-cfcs/
You can see that I created an infinite loop by counting up in one function and counting down in another. In this case it doesn't matter whether it is a singleton or multiple users requesting the same instance, but in a single request one function is overwriting the value in another function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With