Question

Although the examples in this question are in Visual Basic.NET and Java, it's possible that the topic may also apply to other languages that implement type inference.

Up until now, I've only used type inference when using LINQ in VB.NET. However, I'm aware that type inference can be used in other parts of the language, as demonstrated below:

Dim i = 1    'i is inferred to be an Integer
Dim s = "Hoi"    's is inferred to be a string

Dim Temperatures(10) as Double

For T In Temperatures      'T is inferred to be a Double
    'Do something
Next

I can see how type inference reduces the amount of typing I would need to write a piece of code and can also make it quicker to change the type of a base variable (such as Temperatures(10) above) as you wouldn't need to change the types of all other variables that access it (such as T). However, I was concerned that the lack of an explicit declaration of type might make the code harder to read, say during a code inspection, as it may not be immediately obvious what type a variable is. For example, by looking at just the For loop above, you might correclty assume that T is some floating point value, but wouldn't be able to tell if it's single- or double-precision without looking back at the declaration of Temperatures. Depending on how the code is written, Temperatures could be declared much earlier and would thus require the reader to go back and find the declaration and then resume reading. Admittedly, when using a good IDE, this is not so much of an issue, as a quick hover of the mouse cursor over the variable name reveals the type.

Also, I would imagine that using type inference in some situations could introduce some bugs when attempting to change the code. Consider the following (admittedly contrived) example, assuming that the classes A and B do completely different things.

Class A
    Public Sub DoSomething()
    End Sub
End Class

Class B
    Public Sub DoSomething()
    End Sub
End Class

Dim ObjectList(10) As A

For item In ObjectList
    item.DoSomething()
End For

It is possible to change the type of ObjectList from A to B and the code would compile, but would function differently, which could manifest as a bug in other parts of the code.

With some examples of type inference, I would imagine that most people would agree that its use has no negative affect on readability or maintainability. For example, in Java 7, the following can be used:

ArrayList<String> a = new ArrayList<>();    // The constructor type is inferred from the declaration.

instead of

ArrayList<String> a = new ArrayList<String>();

I was curious to find out what people's thoughts are on this matter and if there are any recommendations on how extensively type inference should be used.

Amr

Was it helpful?

Solution

Personally I tend to use implicit type inference only in cases where the type is clear and where it makes the code easier to read

var dict = new Dictionary<string, List<TreeNode<int>>>();

is easier to read than

Dictionary<string, List<TreeNode<int>>> dict =
    new Dictionary<string, List<TreeNode<int>>>();

yet the type is clear.


Here it is not really clear what you get and I would specify the type explicitly:

var something = GetSomeThing();

Here it makes no sense to use var since it does not simplify anything

int i = 0;
double x = 0.0;
string s = "hello";

OTHER TIPS

I think in part it's a matter of coding style. I personally go with the the more verbose code given the choice. (Although when I move to a Java 7 project, I'll probably take advantage of the new syntax because everything is still there on one line.)

I also think that being explicit can avoid certain subtle bugs. Take your example:

For T In Temperatures      'T is inferred to be a Double
    'Do something
Next

If Do something was a numerical method that relied on T being a Double, then changing Temperatures to single precision could cause an algorithm to fail to converge, or to converge to an incorrect value.

I imagine that I could argue the other way—that being explicit about variable types can in certain cases generate a lot of trivial maintenance work that could have been avoided. Again, I think it's all a matter of style.

The use of Generics is to ensure type safe checking at compile time instead of handling it on run time. If you are using type safe correctly it will save you from run time exceptions as well as writing casting codes at the time of data retrieval.

JAva Doesn't use this sentax prior to jdk 1.7

ArrayList<String> a = new ArrayList<>(); 

Its

ArrayList<String> a = new ArrayList(); 

But in java the use of

ArrayList<String> a = new ArrayList(); and
ArrayList<String> a = new ArrayList<String>();

Both consider same because the reference variable holds the complile time information of type safe which is string you can not add anything else instead of string in it i.e

a.add(new Integer(5));

Its still type safe and you will get error that you can only insert Strings.

So you can't do what you are expecting to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top