Understanding 'var' in Plain English
I'm going to show you that using AND not using 'var' is about communicating clearly.
I'm going to show examples of cases where using 'var' makes the code easier to read, and other examples when using var makes things hard to understand.
More than that you'll see that how clear 'var' is depends a lot on what you name everything else in your code.
Take this example:
Jake said hello to Bill. He didn't like him so he turned and went the other way.
Who went the other way? Jake or Bill? In this case "Jake" and "Bill" are like the type name. And "He" and "him" are like the var keyword. In this case it might help to be more specific. The following for example is much clearer.
Jake said hello to Bill. Jake didn't like Bill so he turned and went the other way.
In this case being more specific made the sentence clearer. But that's not always going to be case. In some cases being specific makes it harder to read.
Bill likes books, so Bill went to the library and Bill took out a book that Bill has always liked.
In this case it would be easier to read the sentence if we used "he" and in some cases left out his name all together, this is the equivalent of using the var
keyword.
Bill likes books, so he went to the library and took out a book that he has always liked.
Those analogies cover the gist, but they don't tell the whole story. See in those examples there was only one way to refer to the person. Either with their name, for example Bill, or by a more general way, like "he" and "him". But we're only working with one word.
In the case of the code you have two "words", the type and the variable name.
Person p = GetPerson();
The question now becomes is there enough information there for you to easily determine what p
is? Would you still know what people is in this scenario:
var p = GetPerson();
How about this one:
var p = Get();
How about this one:
var person = Get();
Or this one:
var t = GetPerson();
Or this one:
var u = Person.Get();
Whether the keyword var
works in a given scenario depends a lot on the context of the code, like what the names of the variables, classes, and methods are, as well as the complexity of the code.
Personally I like to use the var
keyword it's more comprehensive to me. But I also tend to name my variables after the type so I'm not really losing any information.
That said sometimes I make exceptions, such is the nature of anything complex, and software is nothing if not complicated.
var
is there for good reason. And it's perfectly obvious what the type is even without hovering over it.var x = new Customer();
It's a Customer object. Simple :) – Attributivevar foo = bar.Coords;
? What isCoords
? An array of floats? Ints? A different object entirely? If you have to rely on your IDE to provide that clarity, then there is something seriously wrong with your coding standards. And god help any new programmers you bring into the fold. – Actinomorphicvar
was introduced to support anonymous types which (I may be wrong) I believe was largely to support LINQ. So it should be limited to these usages IMO for all the readability reasons stated here. C# is a strongly typed language and should be written to look like it when possible ... again IMO. – Prejudgevar
. I tend to write my code so that it's declarative and readable, so I never have any issue with a liberal use ofvar
. Swings and roundabouts, I guess :) – Attributive