通用 Java 包名称
当我创建一个独立的 Java 程序时,它只有一两个 java/class 文件,并且不与其他程序共享,我从不花时间在包名称。我通常只创建我的主类 main.Main
。
也许这违反了协议,但是当复制程序以满足另一个小的利基要求时(例如将制表符分隔为 csv 或递归地列出按修改日期排序的文件),我不会被迫重命名包名称或遭受痛苦当我已经知道我所在的程序时,包名称和路径很长。
此外,当小通用类(例如 Base64 转换)没有唯一的包名称时,更容易从一个程序复制到另一个程序。
那么,标准通用包名称是什么(或者有没有)
编辑:我想我会继续提到更多原因。当文件具有相同的包名称时,比较文件或查看它们是否相同会更容易。另外,当我需要包含一个库时,我会制作一个使用说明,其中包含 java -cp library:jarfile
而不是 java -jar
并且 cp 选项需要我设置主类名,所以我也喜欢把它写得简短一些。这样堆栈跟踪看起来也更好。
When I create a stand-alone Java program, which only has one or two java/class files and is not shared with other programs, I never take the time to say org.domain.scripts.purpose
in the package name. I typically just make my main class main.Main
.
Perhaps this is against protocol, but when duplicating a program in order to fill another small niche requirement (like converting tab delimited to csv or recursively listing files sorted by modify date), I will not be made to rename the package name or to suffer through long package names and paths when I already know what program I am in.
Also little generic classes (like Base64 conversion) are easier to copy from one program to another when they do not have unique package names.
So, what would be a standard generic package name (or is there one)
Edit: I suppose I will go ahead and mention some more reasons. It is easier to diff files or to see if they are identical when they have the same package name. Also, when I need a library to be included, I make a usage note that includes java -cp library:jarfile
instead of java -jar
and the cp option requires I set the main class name, so I like to have it short there too. Also the stack traces look better that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有标准的通用包名称。然而,命名包有一些命名约定:
小写以避免与
类或接口的名称。
域名来开始他们的包
名称 - 例如 com.example.orion
对于由创建的名为 orion 的包
example.com 的程序员。
参考资料:
There is no standard generic package name. There are, however, naming conventions to name a package:
lowercase to avoid conflict with the
names of classes or interfaces.
domain name to begin their package
names—for example, com.example.orion
for a package named orion created by
a programmer at example.com.
References:
“foo”是我最常看到的用于此类目的的名称。
"foo" is what I most often see used for such a purpose.
至于您列出的使用较长包名称的一些问题,即使从一个项目复制到另一个项目,大多数 IDE 也会为您处理所有这些问题。
As far as some of the problems you listed with longer package names, most IDEs handle all that for you even when copying from one project to another.
我认为最接近“标准通用”包名称的是默认包(或无包,如果您愿意的话)。当然,这是不好的做法,但我知道我们正在谈论微小的实验,而不是生产代码。
然而,默认包的主要缺点之一(除了缺少命名空间之外)是您无法从命名包导入默认包中的类。
因此,对于您的实验,如果您只想要单段包名称,请使用适合您的名称:
test
、pkg
、main
(如你提到的)或其他什么......I think the closest thing to a "standard generic" package name would be the default package (or the no-package, if you like). Of course this is bad practice, but I understand we're talking about tiny experiments and not production code.
However, one of the major drawbacks of the default package (besides being merely the lack of a namespace) is that you cannot import classes in the default package from a named package.
Therefore, for your experiments, if you just want single-segment package names, go with whatever suits you:
test
,pkg
,main
(as you mentioned) or whatever...只要您不使用另一个同样懒惰的开发人员编写的代码,这可能就没有问题:-) 猜猜如果编译器在默认包中发现两个名为
Base64
的类会发生什么...使用正确的包名称旨在避免名称冲突。大多数现代 IDE 都可以轻松地将类迁移到不同的包,因此我认为这不是问题。然而,一旦发生名称冲突,如果您需要返回并迁移 100 个旧项目中的类,就会出现问题。或者当你不这样做时它就会成为一个问题,然后你需要修复该类的所有不同版本中的错误......
所以如果你自己编写代码,最简单的选择是将其放入
包net.georgebailey
。然后,一般实用程序可以进入net.georgebailey.util
。That may be fine as long as you don't use code written by another similarly lazy developer :-) Guess what happens if the compiler finds two classes named
Base64
in the default package...Using proper package names is meant to avoid name conflicts. Most modern IDEs make migration of class(es) to a different package a breeze, so I don't think it is an issue. However, once you have a name clash, it becomes a problem if you need to go back and migrate the class in 100 older projects. Or it becomes a problem when you don't, and then you need to fix a bug in all the different versions of the class...
So if you write code for yourself, the simplest choice would be to put it into
package net.georgebailey
. General utilities can then go intonet.georgebailey.util
.