在不同的包中有两个相同名称的类的错误做法?

时间:2022-09-08 16:54:22

We have split our application so that package A handles data from one external source and package B from another. In both cases we need to create a domain object and have a "Transformer" to do this.

我们已经分离了应用程序,使包A从另一个外部源和包B处理数据。在这两种情况下,我们都需要创建一个域对象并使用一个“转换器”来实现这一点。

So I have com.foo.bar.a.ThingTransformer and com.foo.bar.b.ThingTransformer

所以我有com.foo.bar.a。ThingTransformer和com.foo.bar.b.ThingTransformer

I suspect that this is poor practice, but want to see what the good people of SO think.

我怀疑这是不好的做法,但想看看好人是怎么想的。

5 个解决方案

#1


16  

I wouldn't go as far as saying that it's always a bad practice, but it's somewhat of a code smell.

我不会说这一直是一个坏习惯,但这有点像代码味道。

If both classes do different things, then why don't they have different names?

如果两个类做不同的事情,那么为什么它们没有不同的名称呢?

if both classes do the same thing, then why are there two classes?

如果两个类都做相同的事情,那么为什么有两个类呢?

From a practical standpoint it can become very annoying if those two classes ever need to be referenced in the same class: you'll have to use the FQN for one of those (it would probably be best to use it for both in this case, for clarity). If those two classes are in sufficiently distinct parts of the code that they won't be referenced from the same code, then the practical problem is not so bad.

从实际的角度来看,如果这两个类需要在同一个类中引用,就会变得非常烦人:您必须将FQN用于其中一个类(在这种情况下,最好将FQN用于这两个类,以便于清晰)。如果这两个类位于代码中足够不同的部分,因此不会从相同的代码中引用它们,那么实际的问题就不是那么糟糕了。

#2


7  

Not really poor practice, as in many domains have similar terminology, so you will end-up having same names. On the other hand if both are in same domain, but simply different implementations, you can (somehow) indicate the implementation specifics in the name.
The very ugly thing would be if you have to use both in same source file, in this case you have to use fully qualified name for at least one.

并不是很糟糕的实践,因为在许多领域中都有类似的术语,所以您最终将拥有相同的名称。另一方面,如果两者都是相同的域,但只是不同的实现,那么您可以(以某种方式)指出名称中的实现细节。如果您必须在同一个源文件中使用这两个名称,那么最糟糕的是,在这种情况下,您必须为至少一个名称使用完全限定名。

Examples:

例子:

java.util.List java.awt.List

java.util。java.awt.List列表

indicate implementation in the name:
java.util.ArrayList
java.util.LinkedList

用名称:java.util表示实现。ArrayList java.util.LinkedList

#3


5  

It's fine. This is precisely why, by design, different packages have different namespaces.

它很好。这正是为什么不同的包具有不同的名称空间。

#4


3  

Nothing wrong with that, since it's very unlikely you'll use both classes together in the same code. Duplicating the a/b distinction from the package in all class names would be worse.

这没有什么错,因为不太可能在相同的代码中同时使用这两个类。在所有类名中复制a/b的区别会更糟糕。

#5


1  

You have to decide if this is more helpful or more confusing. You can get the same problem with using similar names in the same package where the difference is not clear.

你必须决定这是更有用还是更混乱。在相同的包中使用相似的名称会遇到相同的问题,其中的差异并不明显。

An example of more-confusing-than-helpful is something like

一个更容易混淆而不是有益的例子是这样的

com.sun.corba.se.internal.Interceptors.PIORB extends
com.sun.corba.se.internal.POA.POAORB which extends
com.sun.corba.se.internal.iiop.ORB which extends    
com.sun.corba.se.impl.orb.ORBImpl which extends
com.sun.corba.se.spi.orb.ORB which extends    
com.sun.corba.se.org.omg.CORBA.ORB which extends
org.omg.CORBA_2_3.ORB which extends
org.omg.CORBA.ORB

#1


16  

I wouldn't go as far as saying that it's always a bad practice, but it's somewhat of a code smell.

我不会说这一直是一个坏习惯,但这有点像代码味道。

If both classes do different things, then why don't they have different names?

如果两个类做不同的事情,那么为什么它们没有不同的名称呢?

if both classes do the same thing, then why are there two classes?

如果两个类都做相同的事情,那么为什么有两个类呢?

From a practical standpoint it can become very annoying if those two classes ever need to be referenced in the same class: you'll have to use the FQN for one of those (it would probably be best to use it for both in this case, for clarity). If those two classes are in sufficiently distinct parts of the code that they won't be referenced from the same code, then the practical problem is not so bad.

从实际的角度来看,如果这两个类需要在同一个类中引用,就会变得非常烦人:您必须将FQN用于其中一个类(在这种情况下,最好将FQN用于这两个类,以便于清晰)。如果这两个类位于代码中足够不同的部分,因此不会从相同的代码中引用它们,那么实际的问题就不是那么糟糕了。

#2


7  

Not really poor practice, as in many domains have similar terminology, so you will end-up having same names. On the other hand if both are in same domain, but simply different implementations, you can (somehow) indicate the implementation specifics in the name.
The very ugly thing would be if you have to use both in same source file, in this case you have to use fully qualified name for at least one.

并不是很糟糕的实践,因为在许多领域中都有类似的术语,所以您最终将拥有相同的名称。另一方面,如果两者都是相同的域,但只是不同的实现,那么您可以(以某种方式)指出名称中的实现细节。如果您必须在同一个源文件中使用这两个名称,那么最糟糕的是,在这种情况下,您必须为至少一个名称使用完全限定名。

Examples:

例子:

java.util.List java.awt.List

java.util。java.awt.List列表

indicate implementation in the name:
java.util.ArrayList
java.util.LinkedList

用名称:java.util表示实现。ArrayList java.util.LinkedList

#3


5  

It's fine. This is precisely why, by design, different packages have different namespaces.

它很好。这正是为什么不同的包具有不同的名称空间。

#4


3  

Nothing wrong with that, since it's very unlikely you'll use both classes together in the same code. Duplicating the a/b distinction from the package in all class names would be worse.

这没有什么错,因为不太可能在相同的代码中同时使用这两个类。在所有类名中复制a/b的区别会更糟糕。

#5


1  

You have to decide if this is more helpful or more confusing. You can get the same problem with using similar names in the same package where the difference is not clear.

你必须决定这是更有用还是更混乱。在相同的包中使用相似的名称会遇到相同的问题,其中的差异并不明显。

An example of more-confusing-than-helpful is something like

一个更容易混淆而不是有益的例子是这样的

com.sun.corba.se.internal.Interceptors.PIORB extends
com.sun.corba.se.internal.POA.POAORB which extends
com.sun.corba.se.internal.iiop.ORB which extends    
com.sun.corba.se.impl.orb.ORBImpl which extends
com.sun.corba.se.spi.orb.ORB which extends    
com.sun.corba.se.org.omg.CORBA.ORB which extends
org.omg.CORBA_2_3.ORB which extends
org.omg.CORBA.ORB