Click here to Skip to main content
15,887,214 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.9K   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: 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 
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 
Yes I did understand those were examples, but that was more or less demonstration that there is several ways to make your code bad. One is overuse of var and one is bad variable naming. And from those two bad variable naming is more confusing to me than wise use of var. I also think that demonstrating one bad practice and using other completely different in demonstration is not good way either. Be it just example or not.

Secondly if you think your not poor programmer and everybody who writes one or two or ten bad statements is, you might be in thinking wrongly. I know I write poor code sometimes, if that makes me poor programmer so be it that's far better than think I never write bad code. I've seen last couple of years some programmers that don't use var (some that don't event know what C# 3.0 has added to the language) but have written overlong functions still naming their variables wisely. So that equality of if you write some poor code all your code is poor is not either from the real world.

I still think in narrow scopes it's perfectly fine to use var to declare some types, like generics. Good programmers usually know when to use something and when not, and use language additions wisely. And don't think after reading some comments that somebody is poor programmer and somebody is not. From choosing poor or perfect, I'm more closer to poor. You're right about that, but still seeking to come better. You might be closer to perfect, depends how black and white you look this world.

Some would notice that my bad naming example/comment, is just part of the bad programming; not saying that you took it from production code Smile | :)

One function implementation you write might be perfect and next might be poor, depends who is reading code and the day and work load of the time you write it. That's why we review our code and that's the reality I live in.

modified on Wednesday, June 9, 2010 4:00 AM

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.