During the MVP Summit I heard some statement that gave me shivers: "We need to replace the var as we want to mention the type explicitly". I don't even know where to start complaining about this statement...
Long story short this style guide from Microsoft should never have found its way out of Microsoft. Microsoft should admit now that using var is a best practice and everything that can be var should be var. There is no excuse for making code more cluttered and harder to read. Harder I wrote? Yes. I do not care if the result of calling a function is
Bar. I just want to know what and how result is used. For everything else I would need the IDE / extended knowledge anything.
The old argument is that if
Bar is stated then I know that without having an IDE. Come on, what a weak argument is that? Especially, since that still does not give me the knowledge what
Bar is. So as long as I have not memorized
Bar the information that result is either
Bar is useless to me. Furthermore, when result is used as an argument for another function, I would still need to know if that argument needs to be of type
Bar. Again, the knowledge I have (result being
Bar) is useless.
On the other hand, however, keeping types implicit (i.e., making use of the compilers type inference) will result in additional robustness regarding refactoring. It does not matter if result is
Bar, it just matters if it is compatible in further usage. So changing
Bar should make no difference if the usage is agnostic to it. If I use explicit typing I will hit problems that do not help me in making things compatible or solving incompatibilities. Instead, I need to change some characters (i.e., change lines of code) without any real world contribution. This is the definition of unnecessary clutter.
Microsoft I would appreciate if you could get over your style guide and change it for the sake of saving keystrokes in multiple scenarios. Either write nothing or state the right thing. Thanks!
Remark: The C# and VS team is well aware of this. They also believe in the freedom of choice. Therefore, we can now also set the implicit typing as default, which transforms the standard light bulb to fix implicit to explicit typing into the opposite direction (one fix to make all unnecessary explicit occurrences go away).