'using' directive to alias namespaces and classes

  • Thread starter Thread starter emma middlebrook
  • Start date Start date
E

emma middlebrook

Hi

Am very curious. How, if at all, do people make use of the 'using'
directive to alias namespaces or classes to make their code more
readable (or otherwise e.g. more flexible/maintainable)? Are there any
guidelines/rules?

Is it bad to have a load of using directives at the top of a source
file (this is what I do - I totally 'flatten' out the class
hierarchy)? Is it better to alias a namespace or a class? I presume
fully qualifying everything i.e. avoiding 'using' altogether is silly
- am I right?

In C++, for instance, typedefs come very much in handy when dealing
with the standard library. I don't think that making an alias with the
'using' directive in C# is that analagous but wondering what real
experiences people have had.

Thanks very much

Emma Middlebrook
(e-mail address removed)
 
emma middlebrook said:
Am very curious. How, if at all, do people make use of the 'using'
directive to alias namespaces or classes to make their code more
readable (or otherwise e.g. more flexible/maintainable)? Are there any
guidelines/rules?

Personally, I don't use 'using' at all for aliasing - just for using
the namespaces. i.e. I have

using System;
using System.Data;

etc

but *not*

using list = System.Collections.ArrayList;
Is it bad to have a load of using directives at the top of a source
file (this is what I do - I totally 'flatten' out the class
hierarchy)? Is it better to alias a namespace or a class? I presume
fully qualifying everything i.e. avoiding 'using' altogether is silly
- am I right?

Well, I find it silly, yes.
In C++, for instance, typedefs come very much in handy when dealing
with the standard library. I don't think that making an alias with the
'using' directive in C# is that analagous but wondering what real
experiences people have had.

My own feeling is that so long as you avoid using class names which are
already in the standard library, you'll be fine with the above. You can
assume a certain amount of knowledge about the standard class library -
but using an alias (like the one above for "list") makes it much harder
for someone else to come along and read your code, as they'd constantly
have to be referring to the aliases.
 
Well,

Using or not using namespaces ( C# or C++ ) is really up to the coding style of the developer, personally, I prefer using namespaces and have long function calls rather then defining a 'using' directive and directly accessing the object, this gives the code readers ( among which is the developer of that code ) much more information of the object characteristics and behaviour. namespaces may also come handy to resolve similar object definitions, One would cerate a proprietary namespace for each object and access it using the namespace ( without the using directive ).

So.... Personally, I would recommend the usage of namespaces, using namespaces makes the code much clearer and understandable, as for C#, avoiding the usage of the 'using' directive may result 'really' long function calls so I would suggest compromise and use only the latter namespace e.g. I would rather use:
using System;
....
Threading.Mutex muxTemp = new...

then use
System.Threading.Mutex muxTemp = new...

Nadav.
 
Nadav said:
So.... Personally, I would recommend the usage of namespaces, using
namespaces makes the code much clearer and understandable, as for C#,
avoiding the usage of the 'using' directive may result 'really' long
function calls so I would suggest compromise and use only the latter
namespace e.g. I would rather use:
using System;
...
Threading.Mutex muxTemp = new...

then use
System.Threading.Mutex muxTemp = new...

You can't do that, however.

See http://blogs.msdn.com/csharpfaq/archive/2004/03/07/85570.aspx for
more information.
 
One small experience: I've use using aliases to change between float and
double (for purposes of weighing speed differences vs. acceptable accuracy
of calculation).

Otherwise, I've never known a good case for them (other than a pre-emptive
'find-and-replace' type construct).

Richard
 
Thanks for all your replies!

I'm with Jon Skeet on this one and, like him, I don't find the
aliasing as useful as I would if I were using typedefing in C/C++.

Nadav's idea is really good too however the link to the blog Jon
provided describes the history behind it's removal. I'm not too
bothered about collisions currently because the system I'm working
isn't huge and can always alias around the problem but I agree with
the point that you suddenly don't have a clue where this
'sub-namespace' might be in the overall hierarchy which hurts
readability a bit. I'd still like it on balance :-)

All in all thanks for the great responses and I did note Richard's
comment too - I recently did this for the same reasons but had the
type-that-could-be-switched wrapped in a class so that the 'using' was
hidden from clients.

Emma Middlebrook
(e-mail address removed)
 
Back
Top