views:

136

answers:

5

how should one seperate words in package names

  1. com.stackoverflow.my_package (underscore)
  2. com.stackoverflow.my-package (hypens)
  3. com.stackoverflow.MyPackage (camel-case)

What is the general standard?

+15  A: 

All the three are not the conventions.

Use com.stackoverflow.mypackage

The package name do not follow camel casing or underscores or hypens

Bragboy
I partially agree - they are not 'wrong' according to the java naming conventions but they shouldn't be used in my opinion. (http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html)
Andreas_D
@Andreas_D : I agree, corrected.
Bragboy
A: 

There is none. But usually you don't want a package name to be more than one word long.

Pierre Gardin
A: 

I'd like to point out that a package name with a hypen in it is illegal anyway, it is not a valid Java identifier.

Go with com.stackoverflow.mypackage convention.

Bakkal
+1  A: 

The official naming conventions aren't that strict, they don't even 'forbid' camel case notation except for prefix (com in your example).

But I personally would avoid upper case letters and hyphenations, even numbers. I'd choose com.stackoverflow.mypackage like Bragboy suggested too.

(hyphenations '-' are not legal in package names)

EDIT

Interesting - the language specification has something to say about naming conventions too.

In Chapter 7.7 Unique Package Names we see examples with package names that consist of upper case letters (so CamelCase notation would be OK) and they suggest to replace hyphonation by an underscore ("mary-lou" -> "mary_lou") and prefix java keywords with an underscore ("com.example.enum" -> "com.example._enum")

Some more examples for upper case letters in package names can be found in chapter 6.8.1 Package Names.

Andreas_D
+4  A: 

Here's what the official naming conventions document prescribes:

Packages

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

Examples

  • com.sun.eng
  • com.apple.quicktime.v2
  • edu.cmu.cs.bovik.cheese

References


Note that in particular, anything following the top-level domain prefix isn't specified by the above document. The JLS also agrees with this by giving the following examples:

  • com.sun.sunsoft.DOE
  • gov.whitehouse.socks.mousefinder
  • com.JavaSoft.jag.Oak
  • org.npr.pledge.driver
  • uk.ac.city.rugby.game

The following excerpt is also relevant:

In some cases, the internet domain name may not be a valid package name. Here are some suggested conventions for dealing with these situations:

  • If the domain name contains a hyphen, or any other special character not allowed in an identifier, convert it into an underscore.
  • If any of the resulting package name components are keywords then append underscore to them.
  • If any of the resulting package name components start with a digit, or any other character that is not allowed as an initial character of an identifier, have an underscore prefixed to the component.

References

polygenelubricants
Chapter 7.7 even recommends using the underscore in package names!
Andreas_D
Why is this downvoted? I'm just quoting official docs, which prescribes what to do in general case (i.e. not just one man's opinion for a particular case).
polygenelubricants