0

Our company is deciding on a policy regarding the use of type inference in our C# and Visual Basic.NET code. During our discussions of what type inference is, one of our developers raised an objection based on an argument made by Andrew Troelson. I've tracked this argument down, and I'm presenting it here because I want to make sure we make an intelligent, rational decision before setting this policy "in stone".

On page 101 of his book, C# 6.0 and the .NET 4.6 Framework, Mr. Troelson says,

In fact, it could be argued that the only time you would make use of the var keyword is when defining data returned from a LINQ query. Remember, if you know you need an int, just declare an int! Overuse of implicit typing (via the var keyword) is considered by most developers to be poor style in production code.

This statement, frankly, surprised me, because it has been my experience that use of var has been embraced more than shunned by my coworkers across almost all the companies I have worked for since its introduction. However, my sample size is, admittedly, small.

The question, then, is this: Is the use of the var keyword and type inference considered bad form, or is it largely embraced?

Our desire is to establish a policy that is sensible and in line with modern best practices. Your help is appreciated.

4
  • 5
    "Our company is deciding on a policy regarding the use of type inference in our C# and Visual Basic.NET code." -> I'd stop arguing about this stuff right now. My personal opinion, based on my experience and some teams I've worked with on the last 10+ years is this is mainly a personal preference of the developer, and the code does not get better or worse by forcing this kind of rule. Bad developers will mistype returns because they don't understand typing, and good developers won't make this mistake because they're using var.
    – Machado
    Aug 23, 2017 at 19:58
  • 1
    To var is human. Aug 23, 2017 at 20:50
  • 1
    There is nothing wrong in giving recommendations how to use var in your company, for example for code reviews. But a "policy which is set in stone"? Gives me the impression of overregulating and micromanaging developers.
    – Doc Brown
    Aug 23, 2017 at 21:36

0

Browse other questions tagged or ask your own question.