When I first became engaged with the var keyword in C# there were lots of recommendations about using it onlyl for LINQ to SQL or other cases where it was essential. Now, over the last few weeks I'm seeing it on blogs, video tutorials and whatnot?
Is it now considered acceptable to code in this javascript-y kind of way? Or are these 'experts' leading us astray?
I find it mostly a matter of personal taste. I'm using Resharper myself which constantly nagged me about changing things to the var keyword so I turned that off. If I want to write var for something I want to be able to choose so myself. I try most of the
time still to use the class or interface for my variable but for Linq I usually use the var keyword as I don't want to think too much about what output type it'll generate.
Sometimes for very long statements or class names which I know can only be of one specific type I also use the var keyword as otherwise I end up with very long lines of code. As I said it's a matter of taste.
Grz, Kris.
Read my blog | Twitter Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Marked as answer by Nick_Uk on Mar 26, 2010 08:40 PM
it is upto you what you want to do after getting the result, Say for example
var myMostLovelyFriends = myFriendList.Where(f => f.IsAbove50 == true);
if(myMostLovelyFriends.Count() > 0)
{
// here u just wante to know whether you have such friends or not thats all
}
so var is suitable, typecast with List<Frirnd> is not mandatory.
- Rimbik
Budhi Hin Tanu Janike, Sumirau Pavan Kumar
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site and Silverlight site @ Silverlight version].
I find it mostly a matter of personal taste. I'm using Resharper myself which constantly nagged me about changing things to the var keyword so I turned that off.
I find it puzzling that more people don't complain to the producers of Resharper about this. Like any company, they can get on the wrong track about something - they are simply wrong to suggest that "var" should be preferred for all local variables. The
"var" keyword was introduced for cases where the variable type is not obvious, such as for LINQ queries - it was not intended to be used for all cases.
Try a maintaining a program where every type possible is replaced by var and see if you think it's easier, or harder, to understand and maintain the code.
Thanks for the replies. The last few posts were along the lines of what I was getting at. Although, as I say, I appreciate the input of all contributions.
From a best practices point of view is the angle I'm coming from. I'm seeing the var keyword used a lot - outside of LINQ to SQL. I take it this is going against the grain of coding best practices?
I guess someone needs to put the opposing view in.
Use var whenever you can! Why do I want to specify the type twice, once on the right hand side and then again on the left hand side? Doing things twice just make for a maintenance nightmare (to quote the general argument
against var).
No offence intended, but anyone who calls a method GetComplicatedThingsByTonsOfCriteria gets all the trouble they deserve unless it actually returns IEnumerable<ComplicatedThings> or similar. The method name should give me a good idea of the type being
returned. If I am uncertain then hovering over the var keyword in Visual Studio will tell me the type.
What sort of maintenance tasks does using var help? If you create a method ReturnStudentIds() then I would guess the return type is something like IEnumerable<int> or IEnumerable<Guid> or IEnumerable<string> (if you can't guess then you don't know your
codebase very well and you have much larger problems). Now you decide that you actuall want the students and not just the Ids so you rename the method to ReturnStudents() (that's a pretty easy refactor in VS). Modify the method as appropriate and change
the return type. Now in all your client code you have to fix the callers to handle the new return type. If I don't have to change anything to fix the type of the variable holding the returned value then that is just one less thing I have to do. Hooray!
What sort of maintence tasks does using var hurt? Ones where I have methods called GetData() or some other equally anonymous, non descriptive name. Eg the way to fix the code is to rename the method. Having to specify in every single piece of client code
what the return type of GetData is is just wallpapering over the real problem, the name was wrong in the first place. Someone made the maintenace problem by a poor choice of name, so fix the problem don't just create busy work.
Nick_Uk
Participant
800 Points
445 Posts
var keyword
Mar 26, 2010 07:35 AM|LINK
When I first became engaged with the var keyword in C# there were lots of recommendations about using it onlyl for LINQ to SQL or other cases where it was essential. Now, over the last few weeks I'm seeing it on blogs, video tutorials and whatnot?
Is it now considered acceptable to code in this javascript-y kind of way? Or are these 'experts' leading us astray?
Thanks
Nick
Site
Blog
DigiMortal
Contributor
5728 Points
954 Posts
MVP
Re: var keyword
Mar 26, 2010 08:26 AM|LINK
This is not JavaScript way because you still have strong types. There is no difference between
In both cases there is no way how to detect the type of variables.
If you want to find out more about var keyword and how to use it then I suggest you to read some of my writings:
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
ignatandrei
All-Star
137665 Points
22145 Posts
Moderator
MVP
Re: var keyword
Mar 26, 2010 08:28 AM|LINK
var in C# is not the same as var in Javascript
In C# the compiler emits code in compliance with the type - so it is strongly typed.
In Javascript no!
See http://msdn.microsoft.com/en-us/library/bb383973.aspx
XIII
All-Star
182787 Points
23484 Posts
ASPInsiders
Moderator
MVP
Re: var keyword
Mar 26, 2010 08:30 AM|LINK
Hi,
I find it mostly a matter of personal taste. I'm using Resharper myself which constantly nagged me about changing things to the var keyword so I turned that off. If I want to write var for something I want to be able to choose so myself. I try most of the time still to use the class or interface for my variable but for Linq I usually use the var keyword as I don't want to think too much about what output type it'll generate.
Sometimes for very long statements or class names which I know can only be of one specific type I also use the var keyword as otherwise I end up with very long lines of code. As I said it's a matter of taste.
Grz, Kris.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
Rimbik
Participant
1359 Points
382 Posts
Re: var keyword
Mar 26, 2010 10:45 AM|LINK
I assume when you speak about var it is in C# context.
var is introduced with LINQ like:
var myMostLovelyFriends = myFriendList.Where(f => f.IsAbove50 == true);
the above you can also achieve by
List<Frirnd> myMostLovelyFriends = myFriendList.Where(f => f.IsAbove50 == true).ToList();
it is upto you what you want to do after getting the result, Say for example
var myMostLovelyFriends = myFriendList.Where(f => f.IsAbove50 == true);
if(myMostLovelyFriends.Count() > 0)
{
// here u just wante to know whether you have such friends or not thats all
}
so var is suitable, typecast with List<Frirnd> is not mandatory.
- Rimbik
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site
and Silverlight site @ Silverlight version].
David Anton
Contributor
3704 Points
638 Posts
Re: var keyword
Mar 26, 2010 11:15 AM|LINK
I find it puzzling that more people don't complain to the producers of Resharper about this. Like any company, they can get on the wrong track about something - they are simply wrong to suggest that "var" should be preferred for all local variables. The "var" keyword was introduced for cases where the variable type is not obvious, such as for LINQ queries - it was not intended to be used for all cases.
http://www.tangiblesoftwaresolutions.com
Instant C# - VB to C# Converter
Instant VB - C# to VB Converter
SGWellens
All-Star
126033 Points
10311 Posts
Moderator
Re: var keyword
Mar 26, 2010 01:03 PM|LINK
I believe it can be misused by slovenly and lazy programmers:
http://weblogs.asp.net/stevewellens/archive/2009/11/19/can-the-c-var-keyword-be-misused.aspx
Try a maintaining a program where every type possible is replaced by var and see if you think it's easier, or harder, to understand and maintain the code.
My blog
Nick_Uk
Participant
800 Points
445 Posts
Re: var keyword
Mar 26, 2010 06:50 PM|LINK
Hi,
Thanks for the replies. The last few posts were along the lines of what I was getting at. Although, as I say, I appreciate the input of all contributions.
From a best practices point of view is the angle I'm coming from. I'm seeing the var keyword used a lot - outside of LINQ to SQL. I take it this is going against the grain of coding best practices?
Site
Blog
JeffreyABeck...
All-Star
16423 Points
3329 Posts
Re: var keyword
Mar 26, 2010 07:14 PM|LINK
I think the issue is still very much up in the air. My two cents: unless the type is easy to read from the right hand side of the equals, dont var
Fine:
var foos = new List<Foo>();
Not Fine:
var complexThings = _mySerivce.GetCompicatedThingsByTonsOfCriteria(...);
Warning: Code is often uncompiled and possibly started life written on the back of a napkin. Beware typos.
Paul Linton
Star
13571 Points
2571 Posts
Re: var keyword
Mar 27, 2010 01:21 AM|LINK
I guess someone needs to put the opposing view in.
Use var whenever you can! Why do I want to specify the type twice, once on the right hand side and then again on the left hand side? Doing things twice just make for a maintenance nightmare (to quote the general argument against var).
No offence intended, but anyone who calls a method GetComplicatedThingsByTonsOfCriteria gets all the trouble they deserve unless it actually returns IEnumerable<ComplicatedThings> or similar. The method name should give me a good idea of the type being returned. If I am uncertain then hovering over the var keyword in Visual Studio will tell me the type.
What sort of maintenance tasks does using var help? If you create a method ReturnStudentIds() then I would guess the return type is something like IEnumerable<int> or IEnumerable<Guid> or IEnumerable<string> (if you can't guess then you don't know your codebase very well and you have much larger problems). Now you decide that you actuall want the students and not just the Ids so you rename the method to ReturnStudents() (that's a pretty easy refactor in VS). Modify the method as appropriate and change the return type. Now in all your client code you have to fix the callers to handle the new return type. If I don't have to change anything to fix the type of the variable holding the returned value then that is just one less thing I have to do. Hooray!
What sort of maintence tasks does using var hurt? Ones where I have methods called GetData() or some other equally anonymous, non descriptive name. Eg the way to fix the code is to rename the method. Having to specify in every single piece of client code what the return type of GetData is is just wallpapering over the real problem, the name was wrong in the first place. Someone made the maintenace problem by a poor choice of name, so fix the problem don't just create busy work.
My 2c