Gareth Erskine-Jones said:
Hmmm. I don't use hungarian notation because firstly the type checking
provided by most modern compilers catch any errors, which alone I
think is worth avoiding ugly identifiers. Then the IDEs started
highlighting the errors immediately. Yes, I have to hover over
erroneous code to see what's wrong, but I don't find that to be
terribly burdensome.
I didn't say it was burdensome as implied by my supermarket quote its only a
little thing. However good practice is made up of the cumulative effects of
many little things. Consider that case where there isn't anything wrong but
you are simply reviewing someones code (that someone could be a 6 month
younger version of yourself) again you need to hover over the method to see
the type returned so you can mental match the variable with its type. Again
its only a little thing but on top of that little thing there is another
little thing of remembering that type it was when you stop hovering. Then
you might need to do it for another var (another couple of little things)
and then be able to piece together the exact interactions going on in the
code.
Frankly, using var when the coders knows the specific type the code needs
and the expression on the RHS isn't explicit about the type is lazy coding
and as I pointed can lead to _avoidable_ bugs. The coder should keep in
mind the needs of the human who has to read the code. A chunk of coding in
current developement (like most things) will get read far more often than it
gets written. Use var to shorten the code doesn't necessarily make it more
readable in fact it makes it less readable.
I guess it's a matter of taste. I find briefer code easier to read,
and certainly code which avoids duplication (as in the
List<int> i = new List<int>();
declarations) looks unpleasant to me.
I indicated that this was acceptable earlier in this thread. Since i can
only be a List<int> because the RHS explicitly indicates the type there is
no need to re-state it on the LHS. However my example describes a method
not a new expression. IOW, the above is not a mis-use of var.
Again, different strokes for
different folks - with your final example I don't find it particularly
burdensome for the compiler / IDE / Unit Tests to highlight the
problem - and I think the clarity of the code is well worth it.
You think using var has greater clarity than specifing the exact type
required by the code?? I suspect you are refering to your new expression
example above, in which case you are right but "using var" should be
qualified with "for the right reasons". One could use var whenever a
variable is defined and assigned in the same statement, this would be bad
thing, an abuse of the feature.
As I pointed out the compilier and IDE will not spot a logical error that
can result in the misuse of var. Your unit test is likely to find it but
even that isn't guaranteed. Its still lazy to rely on Unit Tests to find
_avoidable_ bugs.
To summarize, mis-use of var can lead to avoidable bugs and reduces the
clarity of the code. This isn't as subjective as you might like to think.