I love the VAR scope!
Posted by Joe Rinehart at 6:39 PM
17 comments - Categories:
ColdFusion MX
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.
todd sharp wrote on 02/05/07 8:34 PM
I couldn't agree more. Thanks for putting into words what I couldn't ;)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.