views:

364

answers:

7

For example, referencing something as System.Data.Datagrid as opposed to just Datagrid. Please provide examples and explanation. Thanks.

+2  A: 

You're being very explicit about the type you're referencing, and that is a benefit. Although, in the very same process you're giving up code clarity, which clearly is a downside in my case, as I want code to be readable and understandable. I go for the short version unless I have a conflict in different namespaces which can only be solved with the explicit referencing to classes.. Unless I make an alias for it with the keyword using:

using Datagrid = System.Data.Datagrid;
Statement
Yeah, I guess I don't need the explicit declaration when I can just hover over the item and see the fully-qualified name, thanks to VS's convenient tool tips (and Eclipse, etc).
MrBoJangles
+1  A: 

I don't think there is really a downside, just readability vs actual time spent coding. In general if you don't have namespaces with ambiguous object I don't think it's really needed. Another thing to consider is level of use. If you have one method that uses reflection and you are alright with typeing System.Reflection 10 times, then it's not a big deal but if you plan on using a namespace alot then I would recommend an include.

Quintin Robinson
That, and I do whatever ReSharper tells me to do. I'm only half-joking.
MrBoJangles
LOL =PWell ReSharper is a pretty slick addin and alot of people like it, so I wouldn't discount it's advice.
Quintin Robinson
+1  A: 

Actually the full path is global::System.Data.DataGrid. The point of using a more qualified path is to avoid having to use additional using statements, especially if the introduction of another using will cause problems with type resolution. More fully qualified identifiers exist so that you can be explicit when you need to be explicit, but if the class's namespace is clear, then the DataGrid version is clearer to many.

Orion Adrian
+1  A: 

Depending on your situation, extra qualifiers will generate a warning (if this is what you mean by redundant). If you then treat warnings as errors, that's a pretty serious downside.

I've run into this with GCC for example.

struct A {
    int A::b; // warning!
}
bkane
Oh, nice scenario. I don't think I've come across this situation.
MrBoJangles
+1  A: 

The benefit is that you don't need to add an import for everything you use, especially if it's the only thing you use from a particular namespace, it also prevents collisions.

The downside, of course, is that the code balloons out in size and gets harder to read the more you use specific qualifiers.

Personally I tend to use imports for most things unless I know for sure I will only be using something from a particular namespace once or twice, so it won't impact the readability of my code.

Daniel Bruce
Do you really think that adding 1 using statement is a downside versus having to type out the fully qualified name?
Statement
+2  A: 

I generally use the shortest form available in order to keep the code as clean and readable as possible. That's what using directives are for, after all, and tooltips in the VS editor give you instant detail on the provenance of a type.

I also tend to use a namespace tag for RCWs in a COM interop layer, to call out those variables explicitly in the code (they may need special attention on lifecycle and collection), eg

using _Interop = Some.Interop.Namespace;
Wayne
+2  A: 

In terms of performance there is no upside/downside. Everything is resolved at compile time and the generated MSIL is identical whether you use fully-qualified names or not.

The reason why its use is prevalent in the .NET world is because of auto-generated code, such as designer markup. In that case it would be better to fully-qualify names like class names because of possible conflicts with other classes you may have in your code.

If you have a tool like ReSharper, it will actually tell you what fully-qualified references you have are unnecessary (e.g. by graying them out) so you can lop them off. If you frequently cut-paste code across your various code bases, it would be a must to fully qualify them. (then again, why would you want to do cut-paste all the time; it's a bad form of code reuse!)

cruizer
Resharper is cool.
MrBoJangles