I see a lot of C#-questions recently that make use of the var
keyword. For those not familiar with C#/var
(I don't know if other languages have similar features):
You declare something var
and the compiler (and IDE usually already as well) guesses the type you want and watches over that, so the code remains strongly typed.
var potato = functionThatReturnsAString();
//potato is a string now
Console.WriteLine(potato); //works
var cucumber = 52 * 128;
//cucumber is an int now
cucumber++; //works
var pepper = "green";
pepper++; //won't work as pepper is considererd a string.
The "great innovation" behind that is that you can rapid-fire var-typed variables and can decide upon usage what they shall be / don't need to explicitly change the type in the declaration when you change the type. Just assign an object of the new type instead and it will work.
I don't like it, but that discussion has already been had elsewhere (Stack Overflow).
However, a drawback for Code Review in particular is that it makes it occasionally hard to understand what is going on in the code. Take the first code block from this question:
foreach(var t in blenabled)
in connection with the strange naming, this gives us no idea of the type in the List. In that case it is clear from the following code that it is about bool
, but consider numeric types:
You see a line in the code that might cause a division by 0. If the variable in question is an integer type => exception is thrown, if it is float
(etc.) => value NAN is assigned.
From most standard operations you cannot tell at all which numeric type is being used. Or think about Int16
, Int32
, Int64
: If you cannot tell the specific type, you cannot safely identify risky casts, overflows etc., even if 99.9% of times it will be Int32
.
My suggestion:
The community should enforce that the var
keyword should only (if at all) be used in questions and answers if the resulting type is obvious.
var pear = myClass.someObj.Value; //NOPE!
var apple = myClass.ToString(); //OK.
That means especially when getting values from custom functions or Collections declared outside the code posted, the type should be stated. After all, it is not that hard to just type out the type ONCE per variable.
clarification
I do not wish these questions to be closed. I would like the community to be aware of this problem and include into their reviews / comments remarks how and why the usage of var
decreases understandability of code or hinders good reviews.
summary so far
https://codereview.meta.stackexchange.com/a/2356/50461
blenabled
is it a boolean list or is it a bit list? it should have been commented on the question for the OP to provide all the relevant code, which would include the declaration of the boolean list and how it is set. I would have asked for more code personally. \$\endgroup\$switch
on the elements of the List with the options true and false actually gave it away as bool (since you cannot equate whatever you want to each other, like in C). Upfront clrification would have made that a little easier indeed. \$\endgroup\$auto
. And in functional languages, type inference is even stronger, which can make code even more confusing. \$\endgroup\$//potato is a string now
is misleading; it seems to suggest that the call toWriteLine
has something to do withpotato
's type. The type ofpotato
is inferred (not guessed) from the type of the initializer expressionfunctionThatReturnsAString()
. \$\endgroup\$