The war of the var keyword continues
I don't know how many times I have read articles about the var keyword, and how "dangures" it is to the readability of your code. Yesterday Steve Wellens wrote about how misused the keyword is. I have to say that I couldn't disagree more.
First of all his example isn't a real world example:
var Data = GetData();
You need to be able to name your methods and variables better then that. But even in this example, I would use var. Even if it isn't clear what the return type is. How often has it actually helped you to see what the method returns from the type declaration? I can say that it doesn't help me at all. After all, the important thing isn't the type it self, but what methods and properties it contains. In my opinion, if you need to see the type from the declaration to have more readable code, you are doing something wrong.
Another common question about the keyword is (as for all new things in any language) "how well does it preform?". Well, the answer is very simple: exactly the same as if you where to explicitly set the type. The var keyword isn't a runtime feature, it's resolved at compile time. So the compiled code is the exact same.
And then we have another very common misunderstanding. That this code:
ICar car = new SportsCar();
is better then this:
var car = new SportsCar();
It isn't, it's actually worse if anything. You will still have a dependencie on the SportsCar class in this method while you are hiding functionality on the class. The reason why we program to interfaces instead of implementation is to avoid dependencies, and this code doesn't help us with that. The car object here can still be used in any function that takes a ICar. If you want to avoid a dependencie on the SportsCar class, then you should call a method to get it. And that method should return a ICar, and in that case the var keyword is the way to go when calling the method.
Yes, naming classes right is just as important as naming methods and variables. But in a different context. The use of the var keyword will gain you the ability for easier refactoring, a little bit faster coding (in general) and you will probably think twice when you name your variables and that is a good thing as it will help any other developer who is reading you code.
So, in my opinion, the var keyword can not be misused. I use it everywhere, and I feel my code is more maintainable and readable then with explicit typing.