包/命名空间和类具有相同的名称是一个好习惯吗?
我正在创建一个新的命名空间,其中一个类的最合适名称似乎与命名空间同名。这是一个好的做法吗?如果不是,还有什么替代方案?
例如:
com.person
|--- Person.(java/cs)
|--- PersonDetailChecker.(java/cs)
|--- PersonNameGenerator.(java/cs)
讨论同一问题的相关问题:
I am creating a new namespace and the most apt name for one of the class seems to be the same name as the namespace. Is this a good practice? If not, what is the alternative?
For example:
com.person
|--- Person.(java/cs)
|--- PersonDetailChecker.(java/cs)
|--- PersonNameGenerator.(java/cs)
Related questions discussing the same issue:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会尽量避免这种情况,因为它会让你的代码更难阅读
Eric Lippert 写了一篇关于此问题的文章,您可以在这里找到:
第 1 部分,
第 2 部分,
第 3 部分
第 4 部分
我自己也犯过几次这样的错误,这确实让我的一些代码变得更难阅读。
I would try to avoid that, since it can make your code harder to read
Eric Lippert has written an article about this, which you can find here:
part 1,
part 2,
part 3
part 4
I've made the mistake myself a few times, and it certainly made it harder to read some of my code.
在Java中做到这一点是没有问题的。
但这不是一个好的实践:因为好的实践意味着你应该这样做。 (这显然不是事实。)。
顺便提一句。在 Java 中,类名应该以大写字母开头,包应该只包含小写字母,因为它们永远不会真正相同——这是一个很好的做法。
已添加
经过一段时间的重新细化,我认为如果包和类具有相同的名称,则表明架构失败。理由是:任何事物都应该有其独特的存在理由,并且其名称应该表明理由。因此,如果你有一个包和一个同名的类,那么它存在的原因不是唯一的,或者它的名称是错误的。
It is no problem to do this in Java.
But it is NOT a good practice: because a good practice would mean, that you should do it. (And that is clearly not the fact.).
BTW. In Java, the class name should start with an upper case letter, and packages should only contain lower case, to they are never realy the same -- this is good practice.
Added
After rethining the for a while I think it is an indication of an Architectural failure if the package and class have the same name. The reason is: that everything should have its own unique reason to exist, and its name should indicate the reasons. So if you have a package and a class with the same name, then its reason of exisiting is not unique, or its names are bad.
在 Java 中,一个简单的事实是,你的包名应该全部小写,类名应该以大写字母开头,这样可以保证它们不会发生冲突。如果它们相同,那么您一定违反了这些准则之一,这是一个比您的特定准则更基本的风格问题。
In Java the simple fact that your package name should be all lowercase and your class name should begin with an uppercase letter guarantee that they won't clash. If they are the same, you must have violated one of these guidelines, which is a more fundamental stylistic problem than your specific one.
只要情况不同,就没有问题。然而,一旦您对命名空间和对象使用相同的标识符,您最终就必须完全限定对该对象的引用(这使得代码相当冗长且丑陋)。
As long as the case is different, no problem. However, as soon as you use the same exact identifier for a namespace and an object, you end up having to fully qualify your references to that object (which makes the code rather verbose and ugly IMHO).
我会不遗余力地使类名在合理的范围内唯一且不言自明,无论包如何。一个只有一个单词的类名是相当令人困惑的,除非它是一个众所周知的、普遍接受的名称,对于一个共同的概念来说。
嵌套类有点位于封闭类的名称空间中。人们可能会这样做
虽然我发现这很容易阅读,但我仍然坚持旧的风格
I would go out of my way to make class names unique, and self explanatory, within a reasonable scope, regardless of package. A class name with one word is quite confusing, unless it's a well known, generally accepted name, for a common concept.
A nested class is kind of in the name space of the enclosing class. People may do this
While I find this quite readable, I'd still stick to the old style