When I first started using C#, I was very much in the “always use explicit types” camp. Over time however, and perhaps due in part to my professional work requiring the use of javascript, I’ve grown used to defaulting to ‘var’. I will attempt to outline my own reasons for using var, and respond to some of the critiques above. This isn’t an attempt to convince anyone else to use ‘var’, just an explanation of the philosophy behind why I am okay using it. Hopefully I can shed some light for @Owen-Reynolds in giving some ‘why’ behind the ‘what’
Any decision on what syntax to use where is (or should be, in my humble opinion) based on three factors: ease of writing code, ease of reading code, and ease of modifying existing code. I’m setting aside for the moment the added constraints that operating within a larger company create, where any number of programmers may need to interact with the same piece of code over a long period of time.
I see others in this thread downplaying the importance of being able to write code quickly. As I have been programming for longer and longer, the way I look at code has changed. I don’t pay much conscious attention to what specific code I am writing – instead, I am largely focused on achieving the end goal I have in mind. The more the language fights me on syntax, the more I am writing code instead of making programs. ‘var’ simply enables me to do more of the latter.
However, programmers spend far more time reading code than writing it, and readability seems to be most people’s main gripe with using ‘var’. It’s not an unfair criticism, as ‘var’ does not provide any semantic meaning on its own. There is an important point hidden inside @Joe-Censored 's succinct critique of a poorly used ‘var’, though:
var terribleVariableName = TerribleMethodName(variableNotEvenSeenInTheCodeSoFar);
In this snippet, the ‘var’ is actually the least offender. Far worse are the terrible names. A type declaration happens only once, for any given variable. The variable name itself is used everywhere else. If I named all of my variables single characters, a
, b
, c
, etc., my code would be a nightmare to read whether I were to use explicit type declarations or not. I am a firm believer in self-documenting code, and the guideline that comments should endeavor to only explain ‘why’, not ‘what’. A large component of this is in effective variable names. In a mirror to my comments on writing code, the more necessarily verbose the language is, the more I am reading code instead of understanding intent. I find that good variable names go way further to that end than does explicit typing. You may respond, “why not do both, then”? Fair point, but I would argue that being redundant doesn’t necessarily help readability. If a variable looks like a Duck and Quacks() like a Duck, then you really don’t need to specifically label it as being a Duck. I find that a lot of my variables are assigning very obvious things with very obvious variable names, like declaring a Transform transform
or Rigidbody rigidbody
. I find this tedious.
‘var’ also has a hidden attribute that contributes towards readability that I haven’t seen touched on before; it is always 4 characters long, including the space separator. It puts variable names at a consistent depth on the screen and allows for easy visual scanning.
The utility for modifying existing code should be obvious – if you chance the implicit type of the underlying variable, you don’t have to change the declared type.
I don’t use ‘var’ universally though, since my loyalty is to maximizing the three factors first and foremost, and there are definitely places where ‘var’ is not as useful, or impossible to use. Separating declaration from assignment doesn’t allow for implicit typing at all, so that’s obviously out. I have gotten caught several times by the float/int convention, where I have assigned a 0 to a variable I want to be a float. For this reason I have become more explicit when declaring float/int types. This is made easier by the fact that ‘float’ and ‘int’ are both of similar length to ‘var’, and at least in my version of Visual Studio, highlighted in the same color.
But what do I know, I’m one of the weirdos who spaces all my parentheses out and puts brackets on same line
if( hv.x != 0 && hv.y != 0 ) {
movementQueued = true;
queuedMovement = hv;
queuedMovement.x = 0;
hv.y = 0;
}
if( animGraph.blockingMovement ) {
movementQueued = true;
queuedMovement = hv;
} else {
PerformMovement( hv );
}