What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 131 Stack Exchange communities.

It is getting ridiculous:

var input = "This is a  test";
share|improve this question

closed as off-topic by Laura Sep 29 at 21:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question pertains only to a specific site in the Stack Exchange Network. Questions on Meta Stack Exchange should pertain to our network or software that drives it as a whole, within the guidelines defined in the help center. You should ask this question on the meta site where your concern originated." – Laura
If this question can be reworded to fit the rules in the help center, please edit the question.

16  
I like var. If var could be boiled down into a syrup, i would pour it over my waffles and eat them in front of you, the resulting enjoyment plainly visible on my face. –  Shog9 Sep 2 '09 at 4:18
5  
Any particular reason you're against var? Or are you just an anti-varist? –  Jared Harley Sep 2 '09 at 4:23
2  
It is not meant for every instance declaration, just ones where you don't know or a type doesn't exist yet. It litters code and is confusing to other developers. –  Yuriy Faktorovich Sep 2 '09 at 4:52
1  
How is var input = "This is a test"; any more confusing than string input = "This is a test"; ? –  Matt Hamilton Sep 2 '09 at 6:00
    
@Matt, try that on c# 2.0. No one is attacking var, it a riot. But of course it can get confusing, and can be abused. –  Kobi Sep 2 '09 at 6:25
    
@Yuriy Faktorovich - perhaps you should edit the question, change it to a discussion about var use in SO. I agree there's an issue here, but you didn't put up any valid points. –  Kobi Sep 2 '09 at 7:02

3 Answers 3

up vote 15 down vote accepted

There's no doubt that var can be overused. However, it can also aid readability by emphasizing how a variable is used rather than what its exact type is, and by reducing duplication.

Eric Lippert wrote a great comment about this topic when he was tech reviewing C# in Depth.

var has another very handy property in places where space is constrained - it's almost always shorter than the type name. This can be important in books and also on Stack Overflow where line lengths are fairly short.

More importantly, however, I don't think it's up to any one person to dictate the coding style used for answers. Go ahead and avoid using var in your own answers. Criticise answers where the use of var really, really leads to confusion... but accept that it's a personal preference.

share|improve this answer
    
Excellent quote from Eric Lippert! –  Brian Rasmussen Sep 2 '09 at 7:06

Actually, I've noticed that too. Here's an extreme example:
http://stackoverflow.com/revisions/1327855/list

Someone edited another user's answer from:

Int32 count = Enumerable.Intersect(listA, listB).Count(word => word.Length > 3);

to:

var count = Enumerable.Intersect(listA, listB).Count(word => word.Length > 3);

I don't see any reason to do that - I wouldn't edit an answer to fit my coding standards.
As for weather var is good or not, and best practice: that belongs on SO, but it has its downsides. Personally, I try not expose too much of it to beginners (or it gets too confusing / magical), especially not instead of ints (as above) .

share|improve this answer
6  
int32? maybe it should have been an int64, or perhaps an int128 with blast processing! Kidding aside, I agree that editing in var into someone else's code isn't very cool. –  Jeff Atwood Sep 2 '09 at 8:30
var answer = "no";
share|improve this answer
3  
how about var answer = false; ? –  Jarrod Dixon Sep 2 '09 at 4:51
1  
Isaac Waller ♦ –  deleted Sep 2 '09 at 5:17
2  
How about var answer = processDiscussion()? I hope that's a IEnumerable<Conclusion>, but it could be "no", 1... –  Kobi Sep 2 '09 at 7:07
    
var answer = "false"; would be better. Confirms the OPs point ;-) –  Cory Charlton Feb 18 '10 at 20:33

Not the answer you're looking for? Browse other questions tagged .