Java 包命名方案:TLD 真的那么重要吗?

发布于 2024-10-20 12:30:20 字数 305 浏览 2 评论 0原文

是我个人如此,还是 Java 包命名约定中的 TLD 非常无关紧要?我的意思是,如果在没有 TLD 的情况下,com.example.packageorg.example.package 之间确实存在命名空间冲突,那么使用 TLD 无论如何您都会感到困惑。为什么要权衡命名空间冲突的混乱?当然,编译器发出错误/警告比它欢快地尝试编译一个有人导入了错误包的程序更好吗?

只是我对大会的两分钱。坦率地说,您实际上不会在 org.和com。无论如何,因为两个具有相同域但不同 tld 的公司不太可能编写两个同名的 java 包。

Is it just me, or is the TLD in the Java package naming convention pretty extraneous? I mean, if you -did- have namespace collision between com.example.package and org.example.package, without the TLD, then with the TLD you have confusion anyway. Why trade-off confusion for namespace collision? Surely the compiler spitting out an error/warning is better than it prancing merrily along trying to compile a program someone imported the wrong package for?

Just my two cents on the convention. Frankly you're not really going to have namespace collision between org. and com. anyway as it's pretty unlikely two identically-named java packages are going to be written by two companies with the same domain but different tlds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

故人爱我别走 2024-10-27 12:30:20

这并不是说 com.org. 对包名称很重要,只是“使用您公司的域名”是一个很好、简单的经验法则。

It isn't that the com. or org. is significant to the package name, just that "use your company's domain name" is a nice, simple rule of thumb.

久夏青 2024-10-27 12:30:20

有时它可能很重要,但大多数情况下它更多的是声望而不是实际的工程原因。它可以与画作一角画家的签名进行比较,上面写着“这是我做的。”,每当您表达您的担忧和/或意见时,就会引人注目。 。

举个例子,当 Apache 软件基金会从 JCP,每个人都知道这对他们的工作的直接影响,因为他们使用的所有外部第三方库都包含该域:拥有很多的人的 org.apache.* 导入知道他们应该真正倾听这一公告并开始密切关注它,因为它最终会影响他们。

同样,您可以利用公司的良好声誉为图书馆做广告;如果我要向您宣传来自某个随机方的一组集合库,哪个听起来更好,位于 common.collect 下的库或位于 com.google.common 下的库。收集

将域部分添加到包中与实际实践几乎没有任何关系,而是关于承担责任并为自己的身份和所做的事情感到自豪。这意味着开放、共享、成为社区的一部分、成为全球 1300 万左右软件开发人员中的

Sometimes it may be important, but mostly it's more about prestige than actual engineering reasons. It could be compared to the signature of the painter in the corner of the painting, something that says "I made this.", something that makes you notable whenever you're voicing your concerns and/or opinions.

As an example of this, when Apache Software Foundation resigned from JCP, everyone knew the immediate impact of this on their work since all the external 3rd party libraries they use contained the domain: People with lots of org.apache.* imports knew that they should really open an ear for the announcement and start following it closely since it WILL impact them eventually.

Likewise, you can use your company's good reputation to advertise the library; If i were to advertise you a set of collection libraries from some random party, which one would sound better, the one that's under common.collect or the one that's under com.google.common.collect?

Adding the domain part to packages has little to nothing to do with actual practises, it's about taking responsibility and being proud of what you are and what you do. It's about being open, it's about sharing, it's about being part of the community, it's about being you among the 13 million or so of all the software developers across the globe.

有深☉意 2024-10-27 12:30:20

我问了一个关于包名称的问题,Jon Skeet(无论那个人是谁)有一个很好的评论

Java 包中的连字符公司名称

说实话,我希望 Java 没有
沿着这条路走下去
惯例。我想知道有多少
名为“com”或“org”的目录
与单个成员一起存在 - a
更有意义的子目录
名称。

I asked a question about package names, and Jon Skeet (whoever that guy is) had a good comment to say

from: Hyphenated company name in Java packages

To be honest, I wish that Java hadn't
gone down this path in terms of
conventions. I wonder how many
directories called "com" or "org"
exist with a single member - a
subdirectory with a more meaningful
name.

一指流沙 2024-10-27 12:30:20

我在编写软件时会忽略它,但 TLD 确实有助于使软件包变得独一无二。

I just ignore it when coding software, but the TLD does help to make the package unique.

◇流星雨 2024-10-27 12:30:20

就像你说的,这只是一个约定。它的设计并不是完美的;域名可能易手,这也会导致冲突。

Like you said, it's just a convention. It's not designed to be perfect; domains can change hands and this would cause collisions as well.

远昼 2024-10-27 12:30:20

如果您的包不是库(或者它们只是您自己公司的库),那么它们永远不会出去,因此您不必遵循任何包协议。事实上,在独立应用程序上,我只使用一个简短的包名称并将所有内容粘贴在其中。如果不同程序使用相同的包声明,则可以简化对常用文件进行比较或校验的过程。这是我发布的一个问题:通用 Java 包名称

If your packages are not libraries (or they are libraries in your own company only) then they are never going to get out and therefore you do not have to follow any package protocol. In fact on standalone apps I just use a single short package name and stick everything inside of it. That simplifies diffing or checksumming commonly used files from separate programs if they use the same package declaration. Here is a question I posted on that: Generic Java Package Name

为你鎻心 2024-10-27 12:30:20

不。

证明:存在另一种非常流行的语言,它有很多库,都在简单的命名空间下。他们不存在名字冲突的问题。

No.

Proof: There exists another very popular language, with a lot of libraries, all under simple namespaces. And they don't have a problem with name clashing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文