Many text editors and IDEs can reveal the type of an expression in real time. I'm surprised the author doesn't mention this.
I also make the point that often I’m reading code on GitHub or outside an IDE, which does not have such features.
Inferred types feels like an invention for people who don’t like IDEs, that somehow forces everyone _else_ to use an IDE.
Dictionary<string, KeyValuePair<int, string>> contrivedExample = new Dictionary<string, KeyValuePair<int, string>>();
And DictionaryOfStringsWithKeyValuePairs contrivedExample = new DictionaryOfStringsWithKeyValuePairs();
And var contrivedExample = new Dictionary<string, KeyValuePair<int, string>>();
I'd definitely prefer the latter.-- Edit: rephrased to be less presumptuous
Dictionary<string, KeyValuePair<int, string>> contrivedExample = new Dictionary<>();
Which seems ok to me? And we don't enter the slippery slope of allowing variables without types, and hoping people just use it where appropriate. for (var x : someMap.entrySet()) {
String key = x.getKey();
Entity value = x.getValue();
...
}
Here, the reader just needs look at the next two lines to understand the type of x. In the article, the key and value types were long and complicated, and they asked us to refactor. But in the above example, the types are very simple, yet still it's better than for (Map.Entry<String, Entity> x : someMap.entrySet()) {
String key = x.getKey();
Entity value = x.getValue();
...
}
I feel in this example, the type in front of x doesn't help me, as a reader.