I have a confession. I happen to be a fan of the var scope. I know, it makes me an odd duck. However, to my (warped) mind, it makes sense. If Adobe makes it an "option" in Scorpio or whatever ColdFusion 9 is called (Neo...Scorpio...Blackstone...how about something simple, like "Frank"?), I'm going to be one unhappy camper. There are already enough ColdFusion folks out there right now who don't yet know "this" vs. "variables" vs. "arguments" - let's not add "well, depending on your configuration, variables sometimes, var other times..."
So, why do I like VAR?
Reason One: Cohesion Warnings
I think it's because I come from a pretty cohesive school of thought - I don't think of CFCs as being anything like function libraries. Their natural interaction of the functions should be to share data, not isolate it.
To me, the more local variables I have, the more poorly designed that function becomes. That's not to say I don't wind up with five to ten local variables on a regular basis, but when I start to see that many declarations, I begin to notice that the method is taking on too many roles, and it's time for a bit of refactåoring. It's like having too many moving parts in once place.
Reason Two: Forces "Instance" Mentality
It's a bit like reason one, but I think having to think about "local" versus "instance" keeps people more in tuned with thing in terms of "objects" rather than "procedures." If, by default, you're thinking about the method's scope first and object's scope second, I think you're leaning towards functions as procedures rather than behaviors of the object.
Reason Three: ActionScript 3.0 and var is somewhat similar
In ActionScript 3.0, if you want a method to be local to a variable, you var it. For example, binding to the "bar" member of the following class could have some nasty, accidental results:
{
[Bindable]
public var bar:String = "I am the instance version of bar."
public function Foo() {
bar = "I am the value set in the constructor.";
}
}
Yep, just like CF, the "bar" assignment in the constructor would overwrite the class member bar (variables.bar!).
If that line is changed to this...
...you're back in business.
Conclusion
So, yeah, I like the var scope. It means I don't switch thought patterns between ColdFusion and Flex, and it forces me to think about the object, not the procedure. Yes, it's bitten me in the rear a few times (causing threading issues in Model-Glue causes a serious bad day out when people get latest from Subversion!) - but, in the end, it results it better designed code. And that saves me much more time than typing "v-a-r" now and then.
Comment 1 written by todd sharp on 5 February 2007, at 8:34 PM
The simple var declaration helps me keep the scope of the function in mind in relation to the behavior of the component.
If you can remember way back to when CFCs were new to you - it was very easy to think of them procedurally (which is why I used to end up with _many_ components) and miss out on the fact that the functions should act in support of the object itself so it was easy to miss why something simple like this was important. Does that make sense? As I said I'm thinking along your lines I just have trouble verbalizing some of this stuff some times. I guess that ability will come with more experience/a higher comfort level.
Comment 2 written by Russe on 5 February 2007, at 10:06 PM
Comment 3 written by Brandon Harper on 5 February 2007, at 10:46 PM
How exactly would removing the "var" declaration encourage sloppy development? I think the way the "var" scope currently works with variables should be in reverse-- if a variable needs to accessed outside of a given function, it should be set explicitly into a different scope rather than having to var every variable (or having put everthing a private struct).
Having to var variables in cffunction reminds me of what I hate about emissions testing. Emissions testing punishes 99% of the people who would probably never emissions problems with their car to catch the 1% that do. By the same token, most all of the variables you would declare in a function would never need to be accessed outside of that function.
I think explaining concurrency issues to a developer is much harder than explaining why when they set something without prepending a scope, another function couldn't access it. Requiring the var scope just makes ColdFusion harder for the average developer.
Comment 4 written by Steve Nelson on 5 February 2007, at 11:31 PM
http://www.webapper.net
The var keyword is syntax unlike any syntax in CFML. Var is ECMAscript, but CFML is NOT ECMAscript. The functionality is not wrong, the syntax is. IMO a cfset,cfquery,cffile etc variable should automatically be "var'd"and a private scope similar to THIS, but not public should be added for cfcs that would take the place of the current "un-var'd" variables.
Comment 5 written by Paul on 6 February 2007, at 2:47 AM
Sure, I wouldn't mind some kind of compiler/parser warning if I miss one (Mark Drew? Make it happen?)...but to be frank, I'm nearly perfect in all aspects of life. I haven't tripped or stumbled in almost 23 years. And I've only spilled my milk once. Once.
Comment 6 written by Matthew Lesko on 6 February 2007, at 5:54 AM
This end is achievable by declaring <cfset var local = structNew()/> at the beginning of <cffunction .../> declarations. Whereby setting <cfset local.VARIABLE_NAME = VALUE/> allows for local variable declaration inline, rather than grouping at the beginning of the function body.
Comment 7 written by Mark Mandel on 6 February 2007, at 8:19 AM
If I could call 'var' anywhere in my function, my life would be super smooth.
Comment 8 written by Phillip Duba on 6 February 2007, at 9:16 AM
@Mark - I agree with you too on this. That is probably the number one complaint i have with the var scope. i have to var an loop index at the top of the function no where near the loop itself? It doesn't make any sense.
I see it as a necessary evil but it could be implemented more smoothly. Joe, I do like your mentality reason and the fact that you can keep the same types of thought patterns working in Flex and CF because of this "feature".
Comment 9 written by todd sharp on 6 February 2007, at 9:27 AM
I used to agree with the VAR anywhere, but I like the organization of my VARs being at the top of the function body. Makes it more readable to me.
Comment 10 written by Steve Bryant on 6 February 2007, at 4:47 PM
I agree completely!
Mark,
I can certainly see how it would be nice to be able to "var" anywhere (though, like Todd, I like having them grouped at the top). Even granting that it would be nice, I can't think of any new feature that I would be willing to give up to get that.
Phil,
See the ongoing discussion on Webapper for why Steve Nelson's suggestion won't work:
http://www.webapper.net/index.cfm/2007/2/5/Why-you...
Comment 11 written by Rick Root on 7 February 2007, at 11:35 AM
If they do eliminate var scoping, I hope they automatically var scope everything, and add some kind of "global" scope instead or something. Ie, making all variables within CFC methods and UDFs var scoped by default, and non-scoped variables referenced to NOT look outside the current context as well ... like the old "Caller" scope in the world of custom tags, where custom tags didn't recognize variables outside themselves (except for scoped vars like application, session, client, cookie, etc)
At any rate, Joe, you're not an odd duck for liking the var scope.
Comment 12 written by Steve Nelson on 7 February 2007, at 11:48 AM
No where!
My ideal solution for this var "scope" is that un-scoped variables set inside of a cffunction are automatically var'd. But i recognize this won't ever happen and that it has the potential to screw up apps.
Comment 13 written by Rob Gonda on 7 February 2007, at 9:04 PM
Comment 14 written by Sam Farmer on 8 February 2007, at 9:13 AM
So we would declare var private = StructNew() at the top of the function.
Comment 15 written by Dale Fraser on 5 March 2007, at 5:57 PM
I would stear well clear of Private word as it is very likely to be used by Coldfusion at some point and your code would break.
Also a lot of people use local already so it is a generally accepted standard.
http://www.coldfusioncookbook.com/entry/118/How-do...?
Comment 16 written by Rick Root on 14 March 2007, at 2:26 PM
Comment 17 written by John Allen on 23 October 2007, at 11:04 PM
[Add Comment] [Subscribe to Comments]