Click here to Skip to main content
Click here to Skip to main content

Can the C# ‘var’ keyword be misused?

By , 25 Jul 2011
 

More and more often I've been seeing C# code like this:

var Data = GetData();

What on earth does GetData() return? This code is not as maintainable as it could be and is not as maintainable as it should be.

Doesn't explicitly declaring the variable type make the code more readable, understandable, and ultimately more maintainable?

DataTable Data = GetData(); 

Ahhh, GetData() returns a DataTable.

I know that var has uses but I wish it would have been named something much longer because typing 'var' is too easy. Perhaps it should have been named AutomaticTypeVar or even AutoVar to reduce the lazy misuse.

Just my 2 cents.

 
[Update]
A user on the Asp.Net forums was kind enough to provide this quote and link:

"However, the use of var does have at least the potential to make your code more difficult to understand for other developers. For that reason, the C# documentation generally uses var only when it is required."

http://msdn.microsoft.com/en-us/library/bb384061.aspx[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
Member
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerYes, it can...memberzyrconium137 Jan '13 - 20:06 
While I do agree that the 'var' keyword can be abused, I think the example given is not as eloquent as it could be. I think the problem here is the name of the method which is to generic and does not provide enough information about the purpose of the method. You would have the same problem even if the variable would be explicitly typed and the assignment of the variable somewhere far away in the code, like:
 
DataTable table;
...
table = GetData();
 
You still would not be able to know the type of the variable just by looking at it.
 
PS: I am being redundant, I know...
GeneralRe: Q: How do you know "he" named the method GetData? A: You don...memberAdrian Cole17 Dec '11 - 20:22 
Q: How do you know "he" named the method GetData? A: You don't. For example, System.Windows.Forms.DataObject has a method called GetData.
GeneralRe: The name has nothing to do with it. The return type of GetDa...protectorHeath Stewart10 Jul '11 - 17:02 
The name has nothing to do with it. The return type of GetData() is important. If it just returns base types like Object or IEnumerable<Object>, etc., then the type is just that - a base type. IntelliSense won't give you much then. But if the declared return type of GetData() is more specific then var will be more useful.
 
Of course, if you just return something of a base type and - without casting - assign to a derivative type you won't be able to compile it, and even if you cast all objects returned by GetData() better cast correctly.
GeneralReason for my vote of 5 var was added to support Linq, where...memberPIEBALDconsult4 Jan '12 - 8:17 
Reason for my vote of 5
var was added to support Linq, where the developer has no way to specify the datatype, and should only be used in such cases -- which is never, because Linq shouldn't be used either.
GeneralRe: That's funny...but keep your day job. I'm not a big fan of ...memberSteve Wellens4 Jan '12 - 10:08 
That's funny...but keep your day job. I'm not a big fan of the LINQ language but I sure like the LINQ extensions:
 
http://www.codeproject.com/Articles/203624/Learning-to-Like-LINQ-or-Loving-the-LINQ-Loquaciou
 
What the heck? I can't format a link? Rats.
GeneralReason for my vote of 5 You are preaching to the choir here....memberMarcus Kramer1 Nov '11 - 7:09 
Reason for my vote of 5
You are preaching to the choir here. I agree completely. var is a really great tool in our toolbox, but you still need to use the right tool for the job are the right time.
GeneralI still don't see what the big deal is. Wanna know what Get...memberspring19751 Aug '11 - 10:19 
I still don't see what the big deal is. Wanna know what GetData returns? Go look at the method declaration.
 
I use var all of the time and have to go back to look at things long after I wrote it and this doesn't give me a problem. Do variable types seriously give people that much pause? Whether GetData returned a string or a DataTable can also easily be determined by looking at how the variable is used later on.
 
If you're editing code in textpad, that's your loss for not using the tools to make your life easier.
GeneralCore problem in "var" declarations is inability of someone w...memberKelqualyn26 Jul '11 - 0:17 
Core problem in "var" declarations is inability of someone who reads code to suppose type of variable.
If you cant suppose what exact type "GetData()" returns, it shows only one thing - you don't know what exactly this method does. If you don'know, don'even read next line until you will read code of "GetData()" or its specification. Any modification of code after "var" declaration is prohobited for you, until you will know.
Var declarations forces programers to inspect and understand code's invienronment and dependencies clearly before they will made changes.
 
Yes, it takes much time, but it is good.
Lasy programmer is not the one, who don't exactly specifies variable's type. Lasy programmer is the one, who tries to modify code without understanding of specifications and purpose of used methods.
 
Correct method naming is only one thing, that can help us to understand.
In most cases "how result was constructed" is more important knowledge then "what type of data does this method returns" and includes it.
GeneralReason for my vote of 5 I would totally agree with Steve: us...memberDrABELL25 Jul '11 - 17:37 
Reason for my vote of 5
I would totally agree with Steve: using var keyword should be allowed only in case of absolute necessity, otherwise use strong data typing and explicit type casting. The programing paradigm of modern days is "code for readability and clarity", thus avoiding any ambiguous coding technique is a must! 5*
GeneralWe have a simple rule for the use of var: it may ONLY be use...memberchrisbray18 Jul '11 - 11:51 
We have a simple rule for the use of var: it may ONLY be used if the data type is apparent from the code. Therefore this is permitted:
 
var user = new User();
 
This is not:
 
var user = GetUser();
 
Although you would think that both would have the same result, there is no guarantee with the second option as it could return anything. Relying on Intellisense is not permitted, because the code may not be read in something that supports it (e.g. browsers as mentioned before).

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 25 Jul 2011
Article Copyright 2009 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid