Derik Whittaker



Wow, the hatred for ‘var’ lives on

Here we are almost 2 full years since the var keyword was released to the wild, and only a few short months (I hope) till the next release of the .net framework and still the hatred for the var keyword lives on.

The other day I woke up to find the following rant waiting for me in an IM window:
[21:46] VarHatingDude: God I hate the var keyword
[21:46] VarHatingDude: worst thing ever invented
[21:47] VarHatingDude: deferred type is bullshit
[21:47] VarHatingDude: I want to see my types defined in my class
[21:47] VarHatingDude: before I mouse over them
[21:47] VarHatingDude: I will never use that piece of shit
[21:47] VarHatingDude: it's called lazy programming

At first i just kinda dismissed this rant and moved on.  However, something about this made me think I should ponder the rant a bit more.  After giving this a bit more thought here is what I have to say to the ‘var hatters’ out there.

  • RE: Differed Type: Is not a ‘differed type’, but instead it is an inferred typed variable.  What does this mean?  (from MSDN) Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.

    What does this mean?  Basically I can use var on the left hand side and the compiler will infer its type based on the INITIAL context it was used.  Once its type has been inferred by the compiler it cannot be swapped or changed.
  • RE: Lazy Programming: I have to say I 100% disagree with this.  To me using var is more about clean reading and more concise code.  Take the following code
    Person person = new Person();
    The code above tells me in 3 places i am dealing with a person and this is a bit of overkill.  If I use var as below I am not repeating myself over and over again:
    var person = new Person();
  • Var allows for cleaner refactoring when you follow interface driven development.  Many times I have changed the interface that I return (not simply doing a rename) from an object/method and as long as the new interface contains the same signature there is no need to recompile or recode my changes.

If you still hate var after 2+ years of it being out check out the follow blogs for other reasons to like/use it.

But like everything else in programming it all boils down to personal taste at the end of the day.  However, if you do not like something that is cool, do your own thing your own way.

Posted 08-14-2009 2:41 PM by Derik Whittaker
Filed under:



Jordan Day wrote re: Wow, the hatred for ‘var’ lives on
on 08-14-2009 6:24 PM

I agree with most of what you've said here, but your Person person = new Person() example is slightly contrived (you say it "ttells me in 3 places i am dealing with a person"). You could name your variable whatever you want, so I don't think the fact you've chosen to name your variable Person should be part of your tally. A silly nitpick, I know.

Hudson Akridge wrote re: Wow, the hatred for ‘var’ lives on
on 08-14-2009 6:27 PM

Totally agreed man. vars are much cleaner, and you can still hover the mouse over them in VS and pick up what the type is if you're really interested.

It makes for quicker code writing, and cleaner code.

Jon wrote re: Wow, the hatred for ‘var’ lives on
on 08-14-2009 6:30 PM

Wow.  Worst thing every invented?  Lazy programming?  I can understand how var could be abused, but I agree that it most cases it makes code much easier to read (and possible in the case of anonymous types).  It sounds to me like Mr. VarHatingDude doesn't really understand what the var keyword means or what it is for (differed type instead of inferred type?).  Some people are afraid of new things and things they don't understand.  And sadly, some people would rather say that something is "the worst thing in the world" than take the time to figure out how it works.

KG2V wrote re: Wow, the hatred for ‘var’ lives on
on 08-14-2009 9:32 PM

dim Person as new person()


Tuna Toksoz wrote re: Wow, the hatred for ‘var’ lives on
on 08-15-2009 6:06 AM

OMG, there is a VB lover here! :)

I am using var's since the beginning :)

Krzysztof Koźmic wrote re: Wow, the hatred for ‘var’ lives on
on 08-15-2009 11:05 AM

I noticed there are roughly two reasons why people hate vars:

- they pay too much attention to what type a variable is, instead of _what_ it should do.

- they simply do not understand it, that's why they feel lost when dealing with vared code, hence the rant

Damien Guard wrote re: Wow, the hatred for ‘var’ lives on
on 08-15-2009 12:50 PM

If it's clear from the right hand side what the type is, use it, if it isn't don't.

I'd not use var in the following example:

Person selected = context.EmployeeSelector.Current;


Scott Bellware wrote re: Wow, the hatred for ‘var’ lives on
on 08-15-2009 5:53 PM

The very fact that we're talking about the var keyword in our dialogs about C# tell us that we're progressing toward an appreciation of less ceremony in C# code.  Nonetheless, 'var' is a vestige of ceremony.

I'd rather see the var keyword go away entirely.  Languages other than C# (etc) can deal perfectly well with:

person = new Person()

Scott Bellware wrote re: Wow, the hatred for ‘var’ lives on
on 08-15-2009 5:57 PM

PS: The var keyword in C# was released in 2005 with the C# 3 spec and the C# 3 language service for Visual Studio.  We're coming up on the 4th anniversary of var, not the 2nd.

James Gregory wrote re: Wow, the hatred for ‘var’ lives on
on 08-16-2009 5:42 AM

The first point rings very true. So many var haters I've met actually believe var is the same as a variant type from VB days; I try to explain that var is simply letting the compiler type out the type name instead of you, it still ends up being there it just saves you a few keystrokes.

People should only care if an object can do what they want it to, not how it does it; that applies to interface segregation all the way down to declaring variables of a fixed type.

I agree with Scott, why do we need var at all? Surely if a variable isn't declared yet and you want to use it, it should just be declared; why do we need to be explicit about it? Works for the dynamic languages, but I'm pretty sure there's nothing stopping a static language from doing it too (Boo comes to mind).

Martin Watts wrote re: Wow, the hatred for ‘var’ lives on
on 08-17-2009 2:36 AM

Personaly I am very careful with using var. I agree sometimes it improves readability. This is expecialy the case with linq, e.g.:

var customer = from c in CustomerController.GetCustomers where c.Gender == 'M' select c;

However, Derik's example of using var is, imho, a poor one:

var person = new Person();

The next step would be:

var person = PersonFactory.CreatePerson();

This should read:

IPerson person = PersonFactory.CreatePerson();

OJ wrote re: Wow, the hatred for ‘var’ lives on
on 08-17-2009 5:59 AM

@Scott: I'm really not a fan of languages which don't require some kind of declaration to indicate that the variable is to be used. It makes it easy for mistakes to happen. This kind of issue was rampant through VB code, and it was so bad that most of the time people turned on option strict which forced the use of the Dim keyword.

I'll take var over nothing any day of the week.

Dale Barnard wrote re: Wow, the hatred for ‘var’ lives on
on 08-17-2009 9:45 AM

If Microsoft had named var something different like 'def,' I doubt we would have var wars.

I find that when doing interface-based development, it is very easy to accidentally do compatible type casts where we do not want to. You might do

double x = window.GetZoomFactor(); // Returns int.

when you meant

int x = window.GetZoomFactor(); // Returns int.

If you had written

var x = window.GetZoomFactor(); // Returns int.

you would have increased the precision of your code.


Chris McKee wrote re: Wow, the hatred for ‘var’ lives on
on 08-18-2009 5:58 AM

Have to say I love VAR, more so when I'm working with annoying webservices, it makes life simpler and makes complete sense in a modern programming language.

People always complain when things change, hence the PHP group moves so damned slowly.

Michael Kjörling wrote re: Wow, the hatred for ‘var’ lives on
on 08-18-2009 9:23 AM

I agree that 'var' with simple types rarely adds much. However, if you are dealing with variables of a type that has a long name, perhaps a two-type generic in an assembly you don't have a using for, inside an expression, using 'var' can really help code readability. Which of the following is more readable and maintanable (assuming equally known context and that SomeFunc returns LongContrivedTypeName<OtherLongTypeName,EvenLongerAndMessierTypeName>)?

foreach(var item in SomeFunc())


// magic...



foreach(LongContrivedTypeName<OtherLongTypeName,EvenLongerAndMessierTypeName> item in SomeFunc())


// magic...


They are semantically identical, but assuming that you have some idea what the signature of SomeFunc is, the 'var' version is significantly more readable, IMO.

James wrote re: Wow, the hatred for ‘var’ lives on
on 08-22-2009 12:14 AM

I follow the convention of using var when it is obvious from the RHS what the type of the variable is going to be, and declaring its type if it's not.

var x = new X();


X x = Y.GetX();

Still increases readability a lot when working with generics.

Dave Schinkel wrote re: Wow, the hatred for ‘var’ lives on
on 08-24-2009 9:17 PM

I'm with Martin Watts per his example.  I think it's useful to see that it's an interface in that example

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)