On page 95 of the CSLA 4.0 Overview the following code example is offered:
var order = OrderEdit.GetOrder(42);
I'm curious as to why the var keyword is used there. I can understand the use of var when one is dealing with anonymous types but this use (which I'm begining to see all over the place) is puzzling to me.
Since "var" can be used as a shortcut for any type in .NET, it's becoming a favorite shortcut for programmers, especially in instances where the method call makes the return type obvious. If you ever use any of the IDE productivity tools (ReSharper, CodeRush, etc.) they will often suggest that you use "var" extensively. It's becoming a new coding standard in .NET.
I think it's a matter of: Why not use var? As I seem to remember Rocky commenting on this once, it's less typing so why not? Declaring the type doesn't usually add anything to the readability.
The resulting IL is the same. Var is not to confused with the old VB variant.
That's all fine. I asked because I couldn't see any real value to it outside of simplying typing less. I wanted to make sure I wasn't missing some hidden benefit.
In addition to typing less, I think there's a maintainability value.
For example, if you change the return type of a function, the use of var often means you don't need to manually correct the type of field where you store the function result. Although you may still need to adjust other code due to the type change, it does reduce the overall burden.
I also hold the personal belief that the compiler should do anything it is smart enough to do, so I don't have to track as much detail in my mind. If the compiler can infer the type of a field, why make me declare the type by hand? This is a broader and more philisophical benefit - and one that some people disagree with - and that's fine, there's room for different views on such topics.
But I've been a strong proponent of smart compilers and productivity tooling for around 20 years now, so I'm somewhat set in my ways :)
Copyright (c) Marimer LLC