C # controversy: When should you use Var?

Source: Internet
Author: User
Tags anonymous

C # 3 Adds the keyword "var". When the compiler can determine the type of a variable explicitly, it allows inference of the local type. However, there is some debate about when it should be used.

The company that developed the IDE tools ReSharper's Ilya Ryzhenkov summed up some of the benefits of using Var:

1. It helps to better name local variables.

2. It facilitates the design of better APIs.

3. It encourages initialization of variables.

4. It eliminates the confusion of the code.

5. It does not require a using indicator.

RSS Bandit Dare Obasanjo disagree with this. Since Var has had a detrimental effect on his Open-source project using ReSharper, he later issued a response to the Ryzhenkov view of the Bandit project. He responded:

Interestingly, all the "benefits" listed here are focused not only on formal improvements, but also on conflicting relationships. For example, Ryzhenkov claims that var favours "better naming local variables," which actually means forcing developers to use longer Hungarian-style variable names. The funny thing is that this long variable name can completely exacerbate code confusion, because such variable names are ubiquitous, and in contrast, displaying a single type name when declaring a variable keeps the code neat. The idea that Var favours "Designing a better API" is virtually the same. Because of this view, if developers are asked to use longer descriptive attribute names (for example, using Xmlnode.xmlnodename instead of xmlnode.name), they will achieve the goal of improvement. Perhaps someone should have told ReSharper that the way to encode type information into variable names is really bad, and that's why we prefer strongly typed programming languages like C #.

In addition, the idea of encouraging variable initialization is also somewhat inconceivable, because the C # compiler is mandatory for this. More importantly, before you use a variable, you typically need to initialize the variable to NULL, which is not supported by the VAR keyword.

A line in the official C # Language Reference confirms Dare's view:

Excessive use of Var makes the source code obscure and difficult to understand. VAR is recommended only when necessary, that is, when a variable is used to store an anonymous type or a collection of anonymous types.

Not everyone agrees with the complaint that VAR would degrade code readability. Arnon Rotem-gal-oz wrote:

For the readability of the code, I prefer to focus on more powerful methods, such as keeping the method short, meaningful and variable names, and supporting testing (which actually helps you understand how the code works ...). Not only that, if you really very much need code readability, the ReSharper tool can tell you its type when you move your mouse over the var keyword;

Chris Sutton seems to be further, implicitly pointing out that the type is irrelevant.

My advice, then, is to use Var only if you don't know the type. Here are my different opinions and usages. Take a look at the following code fragment:

var procs = from P in servicecontroller.getservices ()

where p.status = = servicecontrollerstatus.running

Select P;

Procs. ToList (). ForEach (p=> Console.WriteLine (p.servicename));

The type of procs is undoubtedly IEnumerable, but it has nothing to do with me. My first concern is that procs is a list, and each item in the list has a property servicename. The potential type is important to the compiler, and those who have to read the code are not compilers, are they?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.