Java package naming. Underlines: special case

Today I named a package in a project that will contain code related to a concept called an "access structure".

Now, calling this package "com.myemployer.project.component.accessstructures" seems unattractive and hard 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 in the Java conventions on the Oracle site . And a short web search came up with nothing.

What is the official naming convention like this?

+3


source to share


3 answers


From Oracle Docs

Package names are written in all lowercase to avoid conflicts with class or interface names.

Companies use their internet reverse domain name to start their package names, like com.example.mypackage for a package named mypackage created by a programmer at example.com.

Name collisions that occur within the same company should be handled by agreement within that company, perhaps by including the region or 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 happen 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 Java reserved keyword such as "int". In this case, the proposed convention is to add an underscore

Although this text does not indicate your specific case, it says that we must use an underscore for an invalid package name. It could be argued that this accessStructures

is how we will define a method in Java and thus naming such a package can be confusing. All in all, it really depends on you.

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



com.myemployer.project.component.access_structures

      

You can also search for synonyms and find alternatives that give less confusion. Some I quickly found:

  • accessframework
  • accessfactory
  • accessarch (accessarchitecture)
  • accessconstructs
+3


source


I don't think there is a standard for this. People follow different naming conventions according to their usability and readability. But most programmers find naming camel case the least convenient. You can call it likeaccessStructure

Found Oracle Doc which recommends using the package name with all lowercase letters



Package names are written in lower case to avoid conflicts with class or interface names.

+1


source


According to the docs, you cannot use camelCase

for naming packages. It is good to use snake_case

for naming packages in some cases, but it is more suitable if you cannot use your domain correctly because of the hyphen or it starts with numbers. But this should be the exception to the rule rather than the rule.

If I were you, I would rephrase it. For example:accessstructures -> accesscore

0


source







All Articles