Click here to Skip to main content
15,881,172 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.7K   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

 
GeneralMy vote of 1 Pin
the__turtle15-Jun-10 16:29
the__turtle15-Jun-10 16:29 
GeneralRe: My vote of 1 Pin
Charles Boyung16-Jun-10 4:22
Charles Boyung16-Jun-10 4:22 
GeneralRe: My vote of 1 Pin
the__turtle17-Jun-10 12:40
the__turtle17-Jun-10 12:40 
GeneralMy vote of 1 Pin
pintel15-Jun-10 11:43
pintel15-Jun-10 11:43 
GeneralRe: My vote of 1 Pin
Charles Boyung16-Jun-10 4:21
Charles Boyung16-Jun-10 4:21 
GeneralGet your facts straight Pin
Jason Hurdlow15-Jun-10 8:52
Jason Hurdlow15-Jun-10 8:52 
GeneralRe: Get your facts straight Pin
Charles Boyung15-Jun-10 9:55
Charles Boyung15-Jun-10 9:55 
GeneralRe: Get your facts straight Pin
SirTimothy29-Jun-10 14:27
SirTimothy29-Jun-10 14:27 
GeneralMy vote of 1 Pin
Jason Hurdlow15-Jun-10 8:22
Jason Hurdlow15-Jun-10 8:22 
GeneralMy vote of 1 Pin
Gary Fletcher15-Jun-10 7:57
Gary Fletcher15-Jun-10 7:57 
GeneralMy vote of 1 Pin
Robert Bissonnette15-Jun-10 5:24
Robert Bissonnette15-Jun-10 5:24 
GeneralStrongly Disagree Pin
inampaki15-Jun-10 5:10
inampaki15-Jun-10 5:10 
GeneralMy vote of 2 Pin
Ansgar Hellwig15-Jun-10 1:43
professionalAnsgar Hellwig15-Jun-10 1:43 
GeneralMy vote of 1 Pin
Graham Downs14-Jun-10 20:43
Graham Downs14-Jun-10 20:43 
GeneralMy vote of 1 Pin
Marc Brooks14-Jun-10 17:38
Marc Brooks14-Jun-10 17:38 
GeneralWhatever, dude. Pin
lancefisher14-Jun-10 13:53
lancefisher14-Jun-10 13:53 
GeneralMy vote of 1 Pin
User 467791614-Jun-10 13:51
User 467791614-Jun-10 13:51 
GeneralMy vote of 1 Pin
DannyVarod14-Jun-10 13:06
DannyVarod14-Jun-10 13:06 
GeneralMy vote of 1 Pin
cwilliam14-Jun-10 11:28
cwilliam14-Jun-10 11:28 
GeneralRe: My vote of 1 Pin
Charles Boyung14-Jun-10 14:23
Charles Boyung14-Jun-10 14:23 
GeneralRe: My vote of 1 Pin
cwilliam14-Jun-10 14:26
cwilliam14-Jun-10 14:26 
GeneralRe: My vote of 1 Pin
Andlah17-Jun-10 8:00
Andlah17-Jun-10 8:00 
GeneralMy vote of 1 Pin
Brandon Haase14-Jun-10 10:57
Brandon Haase14-Jun-10 10:57 
GeneralI feel your pain Pin
Steve Wellens14-Jun-10 9:28
Steve Wellens14-Jun-10 9:28 
GeneralMy vote of 2 Pin
Jon Comtois14-Jun-10 9:04
Jon Comtois14-Jun-10 9:04 

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.