How should one separate words in package names? Which of the following are correct?
com.stackoverflow.my_package
(Snake Case
using underscore)com.stackoverflow.my-package
(Kebab Case
using hyphens)com.stackoverflow.myPackage
(Camel Case
)com.stackoverflow.MyPackage
(Pascal Case
)What is the general standard?
This question is related to
java
naming-conventions
package
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.
Concatenation of words in the package name is something most developers don't do.
You can use something like.
com.stackoverflow.mypackage
Refer JLS Name Declaration
All three are not the conventions.
Use com.stackoverflow.mypackage
.
The package names do not follow camel casing or underscores or hyphens package naming convention.
Also, Google Java Style Guide specifies exactly the same (i.e. com.stackoverflow.mypackage
) convention:
5.2.1 Package names
Package names are all lowercase, with consecutive words simply concatenated together (no underscores). For example,
com.example.deepspace
, notcom.example.deepSpace
orcom.example.deep_space
.— Google Java Style Guide: 5.2 Rules by identifier type: 5.2.1 Package names.
Anyone can use underscore _ (its Okay)
No one should use hypen - (its Bad practice)
No one should use capital letters inside package names (Bad practice)
NOTE: Here "Bad Practice" is meant for technically you are allowed to use that, but conventionally its not in good manners to write.
Source: Naming a Package(docs.oracle)
Underscores look ugly in package names. For what is worth, in case of names compound of three or more words I use initials (for example: com.company.app.ingresoegresofijo (ingreso/egreso fijo) -> com.company.app.iefijo
) and then document the package purpose in package-info.java
.
Source: Stackoverflow.com