l33t l33t - 3 months ago 15
Vb.net Question

Why use fully-qualified names?

Since the introduction of the var keyword, why would you want to use why use fully-qualified names?

var
effectively reduces the typing for variable declarations. Hence, it becomes possible to keep the full declaration without bloating the code. This makes me wonder, why would you even want to declare
namespace
usage?

// A lot of typing, but unambiguous.
System.Drawing.Point a = new System.Drawing.Point();

// using System.Drawing, Point possibly ambiguous.
Point b = new Point();

// using System.Drawing, Point possibly ambiguous.
var c = new Point();

// Less typing, still unambiguous.
var d = new System.Drawing.Point();


What is the best practice here?

Answer

This seems to be less about namespaces and more about the use of static typing inside code. The main argument I have heard for not using var extends to when the variable type isn't readily apparent by glancing at the declaration. Consider the following code:

var readyTasks = _config.GetTasks().Where(x => x.Ready);

How on earth are you going to know what readyTasks is? IEnumerable is a given, considering what Where() returns, but beyond that the only way to get the type out of this is to mouse over var and let Visual Studio ferret it out for you. This gets even more complicated as LINQ statements get longer, enumerables get thrust into logical structures like foreach loops, the list goes on and on and on.

Proponents of var (of which I am one), will counter that this is a tiny thing to reap the numerously fewer hassles that var will produce for you in the long run. Static type declarations have never been a consideration of programmer comfort, ever. They are for compilers first and foremost. Therefore, it does not seem to make sense to hold onto this artifact simply because you're used to it.