Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices: Option Infer [closed]

Tags:

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.

like image 637
Dan Coates Avatar asked Mar 20 '09 20:03

Dan Coates


1 Answers

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.

like image 187
Charlie Flowers Avatar answered Sep 29 '22 05:09

Charlie Flowers