Feb 5 2007

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:

public class Foo
{
[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...

var bar = "I am the value set in the constructor.";

...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.

Comments

todd sharp

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.
Russe

Russe wrote on 02/05/07 10:06 PM

It would be a shame to see the VAR scope go. It would do nothing but further encourage sloppy development which is already all too easy.
Brandon Harper

Brandon Harper wrote on 02/05/07 10:46 PM

@Russe
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.
Steve Nelson

Steve Nelson wrote on 02/05/07 11:31 PM

Absolutely Brandon. That's what i've been bitching about all day:
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.
Paul

Paul wrote on 02/06/07 2:47 AM

Deja vu all over again. Var != ECMAScript. It is common in untyped/loosetyped/typed languages alike - variant datatypes aside. I like the var scope. It's clear, and it's not hard to do, either.

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.
Matthew Lesko

Matthew Lesko wrote on 02/06/07 5:54 AM

I'm agreement with Brandon on this one. To put it another way: maintainability increases with the ability to define variables in close proximity to their purpose; and, "var" works counter to this principle.

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.
Mark Mandel

Mark Mandel wrote on 02/06/07 8:19 AM

The only thing that irritates me about the var scope is that I'm forced to declare it at the top of a function.

If I could call 'var' anywhere in my function, my life would be super smooth.
Phillip Duba

Phillip Duba wrote on 02/06/07 9:16 AM

@Steve - that is a great point, I wouldn't mind anything being set without a scope in a cffunction to automatically be var'd. Where's that feature request link ...

@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".
todd sharp

todd sharp wrote on 02/06/07 9:27 AM

The only problem with the 'local' scope trick is that 'local' is a reserved word in QofQ - so trying to query local.myQuery will throw an error (ask Brian Kotek about that one).

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.
Steve Bryant

Steve Bryant wrote on 02/06/07 4:47 PM

Joe,

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-need-to-VARscope-your-variables
Rick Root

Rick Root wrote on 02/07/07 11:35 AM

I completely agree on this topic, I don't see how people can *NOT* use the var scope when they're doing CFC programming or even basic UDFs. You're just asking for trouble when you don't var scope things.

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.
Steve Nelson

Steve Nelson wrote on 02/07/07 11:48 AM

Rick my initial suggestion when i started this debate the other day is not to elimiate the var "scope". On the contrary. The var "scope" should remain. It's the var "syntax" that i'd like to see modified. Where else in ColdFusion do we have a value-less attribute?

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.
Rob Gonda

Rob Gonda wrote on 02/07/07 9:04 PM

The var scope is great, but forcing you to declare it at the top of your function is retarded. It should stay, but allow you to initialize a local var anywhere in your code, just like AS3
Sam Farmer

Sam Farmer wrote on 02/08/07 9:13 AM

@Lesko, I like your local idea and we have added it our standards document. Though I took into consideration Todd Sharp's comment on local being a reserved work and changed the structure name to be private.

So we would declare var private = StructNew() at the top of the function.
Dale Fraser

Dale Fraser wrote on 03/05/07 5:57 PM

Local is generally accepted rather than Private

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-I-avoid-forgetting-to-declare-local-variables?
Rick Root

Rick Root wrote on 03/14/07 2:26 PM

It would be pretty cool if they'd automatically "top load" var scoped variables so you could declare them whenever you wanted in a method.... it'd be nice to put the variable declarations where they are needed in the code...
John Allen

John Allen wrote on 10/23/07 11:04 PM

I to like the var scope for "Cohesion Warnings" and "Forces 'Instance' Mentality"! Esp in duck typed languages.

Write your comment



(it will not be displayed)