Best Practices: Option Infer [closed]
Asked Answered
G

5

20

What do you feel are best practices for the use of Option Infer in your projects?

In Visual Studio 2008, Option Infer is a directive that allows the compiler to infer the datatype of a declared variable by looking at what is assigned to it.

This is a key feature in VS2008 and is used extensively with LINQ statements and queries. However, turning on Option Infer may create pitfalls for future maintenance programmers.

Gwenny answered 20/3, 2009 at 20:27 Comment(1)
Also see question: #194778Included
M
18

The type inference used by C# (and thus I presume other .net languages) is very precise (and excellent). The compiler will only allow the statement if the type is clear and unambiguous. Therefore, the outcome is not really a loss in precision ... it is merely that you're saving the developer from stating the type more than once. You're reducing duplication in the code.

(EDIT: Also, it is important to realize that the result is still strongly typed. The compiler knows, at compile time, exactly what type the variable is. There is nothing like a variant involved. If you type var x = 42; it simply figures out that x is an int because you put an int on the right-hand side, thus saving you some typing and duplication).

The only reason future maintenance programmers might not understand it is if they simply don't understand the language feature of type inference in the first place. But I think it is more sound to expect and require that maintenance programmers know the language features, than it is to avoid good language features out of fear that future programmers won't know them.

I guess if you're in a situation where you know that future programmers are junior and not very knowledgeable about the language, then maybe you would avoid some things. But that makes me wonder if you should consider some other language, or even a "platform" like Access which is a hybrid of "real programming" and something that a non-programmer can do some things with.

Minda answered 20/3, 2009 at 20:44 Comment(0)
G
9

Here's my recommendation:

If you have already set Option Explicit On and Option Strict On (at whatever level)

  1. Turn off Option Infer in the IDE and project properties
  2. Turn on Option Infer in code files when you need it. This ensures that code is easy to read, and when it is on, it alerts the reader to be on the lookout for its use and reminds them to hover over the var to see its type.

When Option Explicit is Off...

Turning Option Infer On allows old VB6 code full of variants to compile and run better since the compiler assigns a type to the vars at compile time, rather than allowing the var to be late bound. However, testing should be done to ensure that vars do not store multiple types during their lifecycle.

NOTE: This is not a substitute for a proper refactoring of VB6 code ported to dot NET. Variants are bad, children, mm'kay?

Gwenny answered 20/3, 2009 at 20:57 Comment(2)
Re "When Option Explicit is Off .... However, testing should be done to ensure that vars do not store multiple types during their lifecycle." Either the compiler will complain, or a type will be assigned that works, right? Under what conditions is further testing required? Can you give an example where multiple types are stored, yet the compiler neither complains, nor finds a suitable type to declare?Nosedive
Another question: Suppose Option Explicit is On, but Option Strict is Off - consequences if turn on Option Infer? (I am maintaining a project with hundreds of legacy files. Eventually I hope to turn on Option Strict, but there are quite a few places that would require code changes, so not doing that yet.)Nosedive
C
4

Most errors I see have to do with Strict and Explicit, but some occur with Infer. For VB I think this Option Strict On : Option Explicit On : Option Infer Off is the best place to start. It does make writing For Next a bit more cumbersome, but it does mean there will be no question about your intent.

Cini answered 21/3, 2009 at 13:14 Comment(0)
F
2

VB.Net type inference cannot be used in aspx pages in Partial Trust environment ...... As often, VB.Net default options make our life harder. In VB9 the useful Option Infer is turned off by default. We cannot write Option Infer On... We cannot write <%@Page ... infer="true"%> at the top of the page... We cannot write in web.config... Someone just forgot about VB.Net.

......

See here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=321981

Furl answered 20/1, 2010 at 7:16 Comment(2)
You can add /optioninfer to the Page's CompilerOptions, as in <%@ Page Language="VB" CompilerOptions="/optioninfer" %>Ailssa
@AMissico: Actually, in the link that Eric posted, you can see that using CompilerOptions will not work with Partial Trust level. Microsoft has posted an answer though, and recommends <providerOption name="OptionInfer" value="true"/> in web.config.Nievelt
C
-2

I believe it is a safe option because you cannot pass a "Var" type across method boundaries. I am not a big fan of VB or Javascript Variant types, but the Var in C# is quite handy. I would not turn the option off if you plan on using Linq.

--Matt

Condensate answered 20/3, 2009 at 20:44 Comment(4)
Option infer doesn't create variants specifically... especially if you have Option Explicit turned on (which you should, unless you can't). As I understand it (please correct if necessary), the variable is assigned a type by IDE / compiler based on its assignment and is never a variant.Gwenny
That's exactly what I was saying, the new Var type is not like the legacy Variant type. The compiler will let you use Var locally but not pass it across method boundaries or set as member variables so the inherent problems with Variant are not there when using Var with Linq.Condensate
This answer is not useful, and is confusing (at least as currently written). It should be deleted, since answers without such flaws have been provided by others. However, I decided to undo my -1, as it isn't so bad as to be worth discouraging the answerer further. (The existing -1 is sufficient to push it to bottom of list.)Nosedive
This answer is not useful at all, and is deceptive at best. It needs to be deleted. I'm not sure of this but I think deleting it would improve the author's reputation.Mohair

© 2022 - 2024 — McMap. All rights reserved.