Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

Var is Bad

Rate me:
Please Sign up or sign in to vote.
2.82/5 (52 votes)
7 Jun 2010Ms-PL2 min read 65.4K   5   69
Var is Bad

C# 3.0 added LINQ, which is a great feature for working with data. Okay, so maybe Entity Framework is going to make it obsolete (depending on who you ask), but it still served a great purpose for a little while. However, to facilitate LINQ, Microsoft also introduced the bane of good programming – var. Var is a way to implicitly create variables of a type. The compiler figures out what the type is based on what you first assign to it. So in this case:

C#
var a = "Hello, World!";

a” will be of type String. And in this case:

C#
var b = new SqlCommand();

b” will be of type SqlCommand. What’s wrong with this, you ask? Isn’t it great because it leads to less typing? Yes, it is less typing, but that doesn’t make it a good thing. Take a look at this little example:

C#
var value1 = "Hello";
// Several lines of code
var value2 = value1;

What type is value2? A string. Pretty simple, right? Yes, as long as value2 is in close proximity to value1. Just imagine that value1 is defined at the very top of a very complex algorithm, and value2 is not defined until the very bottom, potentially several screens of code away. An extreme case, yes, but it does happen. Visual Studio can tell you what the type is if you hover over it, sure, but what happens when someone is trying to do a code review, not in Visual Studio? That person now has to go scanning through the prior code to figure out what the type of that variable is. Is it really that difficult to type "string value2" instead of "var value2"? No, it is not.

Var is a crutch. It is there for lazy developers. And it is really annoying because it seems like every person that blogs about C# uses it almost exclusively now in all of their code examples. Just look at any post on Scott Hanselman’s blog to see what I mean. Just because you can do something with your favorite programming language, it doesn’t mean you should. I can use “goto” in C# too, but you’re never going to catch me doing it. Var is the same sort of thing – a feature that has very limited required usage, so let’s keep it that way and only use it when required, not all over the place.

This article was originally posted at http://www.nexustechnologiesllc.com/blog/var-is-bad

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Nexus Technologies, LLC
United States United States
I have been working in the field of software development since 1999. With a degree in Computer Engineering from the Milwaukee School of Engineering, I try to provide a strong results-oriented approach to software development. I have worked with a variety of industries, including healthcare, magazine publishing and retail. After having worked for corporations of varying sizes for nearly ten years while also providing custom software solutions to individuals and small companies, I left the corporate world to provide expert, high-quality software solutions to a broader range of companies full-time. I am also a Certified Usability Analyst with Human Factors International, committed to providing the best possible experience to the users of your website or application.

Comments and Discussions

 
GeneralRe: My vote of 1 Pin
Darchangel14-Jun-10 9:12
Darchangel14-Jun-10 9:12 
GeneralI disagree Pin
Jcmorin14-Jun-10 7:56
Jcmorin14-Jun-10 7:56 
GeneralRe: I disagree Pin
Charles Boyung14-Jun-10 8:06
Charles Boyung14-Jun-10 8:06 
GeneralRe: I disagree Pin
RDABC14-Jun-10 8:26
RDABC14-Jun-10 8:26 
GeneralRe: I disagree Pin
JasonPSage14-Jun-10 8:51
JasonPSage14-Jun-10 8:51 
GeneralRe: I disagree Pin
Dave Shaw14-Jun-10 10:32
Dave Shaw14-Jun-10 10:32 
GeneralRe: I disagree Pin
JasonPSage14-Jun-10 11:40
JasonPSage14-Jun-10 11:40 
GeneralI've had this discussion a few months ago, but basicly I think that you might need a little more pragmatism Pin
Tom Janssens10-Jun-10 22:05
Tom Janssens10-Jun-10 22:05 
While your statement is true in some cases, IMHO it's perfectly viable to use
var customers = new List<Customer>();


I personally use it a lot, and never heard any negative remarks.

As mentioned in my article[^] and previous comments, it is all about knowing when to use a tool.

If you want to forbid the var keyword, because it can be abused, you might as well forbid all knives, or even hammers, and maybe airplanes as well, since they all can be abused for another purpose....

GeneralRe: I've had this discussion a few months ago, but basicly I think that you might need a little more pragmatism Pin
Charles Boyung11-Jun-10 4:05
Charles Boyung11-Jun-10 4:05 
GeneralRe: I've had this discussion a few months ago, but basicly I think that you might need a little more pragmatism Pin
JasonPSage14-Jun-10 11:47
JasonPSage14-Jun-10 11:47 
QuestionEqually bad as lazy variable naming? Pin
mazaaz8-Jun-10 2:44
mazaaz8-Jun-10 2:44 
AnswerRe: Equally bad as lazy variable naming? Pin
Charles Boyung8-Jun-10 3:14
Charles Boyung8-Jun-10 3:14 
GeneralRe: Equally bad as lazy variable naming? [modified] Pin
mazaaz8-Jun-10 21:40
mazaaz8-Jun-10 21:40 
QuestionDid you understand the use of var before writing this blog? Pin
Anurag Gandhi8-Jun-10 0:09
professionalAnurag Gandhi8-Jun-10 0:09 
AnswerRe: Did you understand the use of var before writing this blog? Pin
Charles Boyung8-Jun-10 3:09
Charles Boyung8-Jun-10 3:09 
GeneralRe: Did you understand the use of var before writing this blog? Pin
Jcmorin14-Jun-10 7:58
Jcmorin14-Jun-10 7:58 
GeneralRe: Did you understand the use of var before writing this blog? Pin
Alexander DiMauro14-Jun-10 9:34
Alexander DiMauro14-Jun-10 9:34 
GeneralRe: Did you understand the use of var before writing this blog? Pin
spencepk14-Jun-10 12:41
spencepk14-Jun-10 12:41 
GeneralGood point! Pin
venomation7-Jun-10 17:47
venomation7-Jun-10 17:47 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.