开发者

Generic Java Package Name

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 wh开发者_运维技巧en 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.


There is no standard generic package name. There are, however, naming conventions to name a package:

  • Package names are written in all lowercase to avoid conflict with the names of classes or interfaces.
  • Companies use their reversed Internet 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:

  • Naming a Package


"foo" is what I most often see used for such a purpose.


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.


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...


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 into net.georgebailey.util.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜