Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java package naming. Underscores: A special case

Today I was naming a package in a project which would contain code related to a concept called an "access structure".

Now, naming this package "com.myemployer.project.component.accessstructures" seems unappealing and difficult to read because of the triple "S". (The higher level packages are not actually named "project" and "component").

I was tempted to use "...component.access_structures"

I couldn't find anything mentioned in the Java conventions on Oracle's site . And a brief web search brought up nothing.

What is the official convention for names like this?

like image 244
Brad Johnson Avatar asked Sep 09 '14 18:09

Brad Johnson


1 Answers

From Oracle Docs

Package names are written in all lower case 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.mypackage for a package named mypackage created by a programmer at example.com.

Name collisions that occur within a single company need to be handled by convention within that company, perhaps by including the region or the project name after the company name (for example, com.example.region.mypackage).

In some cases, the internet domain name may not be a valid package name. This can occur if the domain name contains a hyphen or other special character, if the package name begins with a digit or other character that is illegal to use as the beginning of a Java name, or if the package name contains a reserved Java keyword, such as "int". In this event, the suggested convention is to add an underscore

Although this text doesn't specify your exact case, it does say that for an invalid package name we should use an underscore. One could argue that accessStructures is how we would define a method in Java and thus naming a package like that could be confusing. Overall, it is really up to you.

If you want to keep with this convention, I believe you should name your package:

com.myemployer.project.component.access_structures

Also you can look up synonyms and find alternatives that would give less confusion. Some I quickly found:

  • accessframework
  • accessfactory
  • accessarch (accessarchitecture)
  • accessconstructs
like image 117
nem035 Avatar answered Oct 01 '22 17:10

nem035