Identical class names in different namespaces

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 23:25:10

问题


I have two different namespaces, with lots of classes with the same name. I believe some code will make it easier to understand:

namespace Print.Pdl.PostScript.Operators
{
    public abstract class BaseOperator : IOperator
    {
         // ...
    }
}

namespace Print.Pdl.Pcl6.Operators
{
    public abstract class BaseOperator : IOperator
    {
         // ...
    }
}

The basic implementation is the same, as PostScript and PCL have similar constructs. So, both namespaces end up having identical names to several classes.

I am tempted to do the following...

namespace Print.Pdl.PostScript.Operators
{
    public abstract class BasePsOperator : IPsOperator
    {
         // ...
    }
}

namespace Print.Pdl.Pcl6.Operators
{
    public abstract class BasePclOperator : IPclOperator
    {
         // ...
    }
}

... but, IMHO, it kind of defeats the purpose, as there is a redundancy in the identification. Why should I prefix/change the classes names, if the namespace already creates a logical barrier?

So, what you people think? Should I keep the identical names, as they are in different namespaces, or should I prefix/change the classes names to make it easier to identify the source, and avoid conflicts if someone wants to use both namespaces together?

Thanks!


回答1:


Nolde,

I don't think you should sacrifice the architecture in favor of readability. I believe its more intuitive if you keep the same class names as this makes it simpler if you are switching from PCL to PostScript and vice-versa.

If you have to use both classes in the same code file, create an alias for the namespace. It will be very clear to read:

using Pcl = Print.Pdl.Pcl6.Operators;
using PostScript = Print.Pdl.PostScript.Operators;
...
// use PCL
Pcl.BaseOperator.DoSomething();
// Use PostScript
PostScript.BaseOperator.DoSomething();

Thanks, Luciano Bargmann




回答2:


It's a tricky question IMO. To answer it well, you need to know how often people are likely to use both namespaces at once, and how annoyed they get at having to prefix everything with a namespace.

I personally tend to lean towards the "different names". I think of the namespaces as a mechanism to limit the set of names visible in the code, and a safeguard against the unlikely event that names clash even within this reduced set. So keeping the clash "unlikely" is important. Therefore, I personally wouldn't design with a clash on purpose.

Especially since in your case the difference is so small: BaseOperator is only a tad shorter than BasePsOperator.




回答3:


If there's no functional or interface difference between the two proposed base classes then maybe create another, general namespace with the BaseOperator in it - and just define it once.




回答4:


My opinion is you should use a naming scheme reflecting specialization.

I mean you don't need to think about if it's a prefix, suffix or any other. Just write names that may clearly identify classes.

Honestly I believe namespacing wouldn't repleace a right class naming scheme, because namespaces are an organizational thing while classes are part of what your program does.

So, at the end of the day, I'd choice your second option: specialized naming for namespaces and classes.




回答5:


Different namespaces are just that... Different, name, spaces so you shouldn't worry at all (generally) about duplicate names across namespace boundaries.

However, you should be conscious of usage. e.g. you are creating a layered app with many different layers (in different namespaces), you would not want to duplicate class names that might be shared across layers.

If you feel it is sensible to use duplicate naming between namespaces, you can always use the handy using statements for renaming in consuming classes e.g.

you have two User classes :

Data.Entities.User;
App.Core.User;

And want to use them in the same place...

You can use a simple using statement such as

using HttpUser = App.Core.User; 

To alias one of them and so avoiding full qualification, and avoiding any confusion.




回答6:


Developers maintaining the code after you will appreciate it if you try and avoid duplicating class names. It just makes it hard to tell at a glance what class is being used.



来源:https://stackoverflow.com/questions/5717278/identical-class-names-in-different-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!