Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Var is Bad

, 7 Jun 2010 Ms-PL
Rate this:
Please Sign up or sign in to vote.
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:

var a = “Hello, World!”;

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

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:

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.

License

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

Share

About the Author

Charles Boyung
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 2 PinmemberCharles Boyung14-Jun-10 10:14 
GeneralRe: My vote of 2 PinmemberJon Comtois14-Jun-10 10:28 
GeneralI think it would help if you renamed this blog. Pinmembermbcrump14-Jun-10 9:48 
GeneralI totally agree! Pinmemberado.net14-Jun-10 9:39 
GeneralMy vote of 1 PinmemberDarchangel14-Jun-10 9:11 
GeneralRe: My vote of 1 PinmemberCharles Boyung14-Jun-10 9:47 
GeneralRe: My vote of 1 PinmemberDarchangel14-Jun-10 10:12 
GeneralI disagree PinmemberJcmorin14-Jun-10 8:56 
GeneralRe: I disagree PinmemberCharles Boyung14-Jun-10 9:06 
GeneralRe: I disagree PinmemberRDABC14-Jun-10 9:26 
GeneralRe: I disagree PinmemberJasonPSage14-Jun-10 9:51 
GeneralRe: I disagree PinmemberDave Shaw14-Jun-10 11:32 
GeneralRe: I disagree PinmemberJasonPSage14-Jun-10 12:40 
GeneralI've had this discussion a few months ago, but basicly I think that you might need a little more pragmatism PinmemberTom Janssens10-Jun-10 23: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 PinmemberCharles Boyung11-Jun-10 5:05 
GeneralRe: I've had this discussion a few months ago, but basicly I think that you might need a little more pragmatism PinmemberJasonPSage14-Jun-10 12:47 
QuestionEqually bad as lazy variable naming? Pinmembermazaaz8-Jun-10 3:44 
AnswerRe: Equally bad as lazy variable naming? PinmemberCharles Boyung8-Jun-10 4:14 
GeneralRe: Equally bad as lazy variable naming? [modified] Pinmembermazaaz8-Jun-10 22:40 
QuestionDid you understand the use of var before writing this blog? PinmemberAnurag Gandhi8-Jun-10 1:09 
AnswerRe: Did you understand the use of var before writing this blog? PinmemberCharles Boyung8-Jun-10 4:09 
GeneralRe: Did you understand the use of var before writing this blog? PinmemberJcmorin14-Jun-10 8:58 
GeneralRe: Did you understand the use of var before writing this blog? PinmemberAlexander DiMauro14-Jun-10 10:34 
GeneralRe: Did you understand the use of var before writing this blog? Pinmemberspencepk14-Jun-10 13:41 
GeneralGood point! Pinmembervenomation7-Jun-10 18:47 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 7 Jun 2010
Article Copyright 2010 by Charles Boyung
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid