views:

377

answers:

2

Are there are any established naming or coding conventions for defining namespace or type aliases in C#?

For those who are unaware, the C# language has a feature where aliases can be defined local to a file for namespaces and types. This can be useful when there are naming conflicts with third party libraries as well as for shortening type names within your code. Below is an example of what it looks like.

using Forms = System.Windows.Forms;

Most of the examples that I've seen around the web tend to use unabbreviated capitalized names as aliases such as the alias Forms in the example above. In some places including the official MSDN page which explains example with an alias of colAlias for the namespace System.Collections. To make it more complicated there may be a preference for some to choose different guidelines depending on whether a namespace alias or a type alias is being defined.

In order to give some background for why I'm interested any guidelines with alias names I'll explain what I'm doing. In a recent project I began simplifying a pattern where I have several classes which inherit from a generic base class which accepts complex type arguments by using type aliases.

So using this technique the complex example below becomes the much more readable once type aliases are applied.

public class MyClass: MyGenericBaseClass<TripleLindyFancyAlgorithm<List<SomeValueType>>, List<SomeValueType>>
{
    public override List<SomeValueType> DoSomething(TripleLindyFancyAlgorithm<List<SomeValueType>> operation)
    {
        // ...
    }
 }

And below the must cleaner version using type aliases.

using Result = List<SomeValueType>;
using Algorithm = TripleLindyFancyAlgorithm<List<SomeValueType>>; // Note: cannot reference an alias within an alias definition!

public class MyClass: MyGenericBaseClass<Algorithm, Result>
{
    public override Result DoSomething(Algorithm operation)
    {
        // ...
    }
 }

Although this looks much more simplistic, it's easy to forget that an alias such as Result is actually just an alias for List and that there is no actual type called Result. In order to separate the concepts visually I'm considering following some prefixing convention similar to the use of underscore '_' before private members in order to help distinguish type aliases from actual types. Before I do so however I want to make sure that I'm not reinventing the wheel since maybe there already more established conventions out there.

+2  A: 

Personally, I would only use it to keep intelli-sense clean.

using StringBuilder = System.Text.StringBuilder;

If you rename types you open up Pandora's box for maintenance programmers.

ChaosPandion
While I agree that using this technique in cases where it isn't warranted would very bad for maintenance, I think the original complex example without any improvements is probably even more of a concern.
jpierson
I may be crazy, but I think it is more readable when it is so explicit.
ChaosPandion
ChaosPandion, maybe your right. The main point however is to discuss naming conventions although I think it is important to consider coding guidelines with respect to the use of aliases.
jpierson
+4  A: 

I would only use aliases in the case of namespace conflict (i.e. only if I had to).

For me at least, any other use is just confusing and a distraction.

Jay Riggs
But you have to admit that in the example I gave, if you had to write 100+ plus similar classes which all had the same pattern that the type aliasing feature does dramatically improve the readability. Although I agree it can be confusing, I think in this type of example it is actually just as much if not more confusing to leave the complex example as is.
jpierson