I have used Style Cop for making sure people follow set code standards before but then configured it to our needs. For a few days now I have used Style Cop with its default setting to try it out and I'm a bit puzzled by some things. For instance, if I declare a variable I really like to state what type it is.
int foo;
or
Bar foo = new Bar();
Style Cop wants you to use the var
keyword a lot.
I understand that you, the compiler and the IDE can figure out the type by the instantiation.
var foo = 1; // Makes foo and int
var foo = new Bar(); // Makes foo a Bar
When declaration and instantiation is done in one step it's fairly visible whats going on.
There are a few more things that I'm not quite comfortable with that Style Cop seems to like so I basically just wanna know how common is the coding style enforced by the standard Style Cop configuration, and where does it originate from?
Note: This usage below of var
is not valid as stated by hvd. Made me feel a bit better of var
straight away ;). I leave it in since it was in the original question though
But if you have some obscure thing like
var foo
... bunch of code ...
foo = getLatestData();
It's not obvious what foo
becomes. But looking at the declaration of foo
you won't get any smarter. Instead you have to look at the declaration of the getLatestData
function.
I feel that the var
approach clouds everything a bit. There is no strict control like you get if you declare that foo
is a int
. Then you know that foo
is always an int
. It doesn't depend what it get's its data from. Something like this could very well give you two different types of foo.
var foo;
...
if(status == 0) {
foo = getLatestData();
}
else {
foo = getOtherData();
}
There's nothing enforcing that the two functions have to return the same type and foo
can thus become different things. When you code everything yourself and it's contained like this it's probably easy to keep track of. But if it's an API or plugin library it would be a nightmare if external 3rd party developers cold dictate the type of foo
since you could not be sure what to do with it in that case.
You question has some misconceptions about var
which have been picked up in the comments and clarified in the linked MSDN reference.
As to the actual question, I would infer that the code style enforced by the default Style Cop rule set is at least as common as the usage of the default Style Cop rule set. This answer is a tautology of course but, correct for the remnant of the question.
If you have more concerns about the var
keyword you could consult many of the existing answers on the subject, here on Stack Overflow.