Why is reverse DNS notation used for package naming?
Asked Answered
G

3

7

There is a technical reason for using the reverse DNS package notation? Or is it just a convention?

Gunflint answered 10/8, 2015 at 9:52 Comment(0)
V
7

Sun used to maintain a Java Coding Standards guide - you can find an archived copy on the Oracle site here - according to this site this has not been maintained since 1999: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

The part covering recommendation for package names does suggest to use domain names of the owning organization, top-level-domain first: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367

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.

I think this became the accepted standard for some time, even though it's not as strictly followed today. There's no reason why you can't devise a naming pattern for your project or organization that makes best sense to you - there's no technical reason why it has to follow this domain name pattern.

The typical pattern followed today is higher level groupings first (organizational or project name) followed by increasingly more specific groupings down to most specific functional or technical grouping in the name last.

Voltage answered 19/8, 2015 at 17:46 Comment(0)
P
13

I think it's now mostly just a convention but I'd say that it helps to organize things down abit.

Here are afew examples as to why I say so: com.vzy.gui.* and com.vzy.io.*

  • going through them we see, com and think, "oh hey, it's a company that made this"
  • then vzy and think, "the people that made this is vzy!"
  • then gui and io which leads us to see "this one works with GUIs and this other one works with IO, cool."

I think another good way to show this would be to switch it back and see how it would look...

import ArrayList.util.java
import BorderLayout.awt.java
import File.io.java
import JButton.swing.javax
import JFrame.swing.javax
import JLabel.swing.javax
import JOptionPane.swing.javax
import JPanel.swing.javax
import JScrollPane.swing.javax
import JSlider.swing.javax
import LinkedBlockingQueue.concurrent.util.java
import PrintWriter.io.java
import Scanner.util.java
import SwingUtilities.swing.javax

or

import java.awt.BorderLayout
import java.io.File
import java.io.PrintWriter
import java.util.ArrayList
import java.util.Scanner
import java.util.concurrent.LinkedBlockingQueue
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JSlider
import javax.swing.SwingUtilities
Posen answered 19/8, 2015 at 16:37 Comment(0)
V
7

Sun used to maintain a Java Coding Standards guide - you can find an archived copy on the Oracle site here - according to this site this has not been maintained since 1999: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

The part covering recommendation for package names does suggest to use domain names of the owning organization, top-level-domain first: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367

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.

I think this became the accepted standard for some time, even though it's not as strictly followed today. There's no reason why you can't devise a naming pattern for your project or organization that makes best sense to you - there's no technical reason why it has to follow this domain name pattern.

The typical pattern followed today is higher level groupings first (organizational or project name) followed by increasingly more specific groupings down to most specific functional or technical grouping in the name last.

Voltage answered 19/8, 2015 at 17:46 Comment(0)
F
4

There is no technical reason, only a convention to avoid conflicts. The following non-normative quote from the Java Language Specification:

Developers should take steps to avoid the possibility of two published packages having the same name by choosing unique package names for packages that are widely distributed...

You form a unique package name by first having (or belonging to an organization that has) an Internet domain name, such as oracle.com. You then reverse this name, component by component, to obtain, in this example, com.oracle, and use this as a prefix for your package names, using a convention developed within your organization to further administer package names. Such a convention might specify that certain package name components be division, department, project, machine, or login names.

...

The name of a package is not meant to imply where the package is stored on the Internet. The suggested convention for generating unique package names is merely a way to piggyback a package naming convention on top of an existing, widely known unique name registry instead of having to create a separate registry for package names.

The reason the package name is the reverse sequence of the domain names is simply because the name of a package is a fully qualified name. Every package contains a set of classes/interfaces and/or subpackages, and the name of a subpackage must be prefixed by its containing package to form the fully qualified name:

The fully qualified name of a named package that is a subpackage of another named package consists of the fully qualified name of the containing package, followed by ".", followed by the simple (member) name of the subpackage.

Firebox answered 19/8, 2015 at 16:50 Comment(2)
To use domain names as unique identifiers make sense, but why to reverse them? oracle.com and com.oracle are the same from this point of view.Gunflint
@Gunflint Because the package naming is hierarchical. A com.oracle.MyClass means MyClass is in a directory oracle contained in a directory com.Firebox

© 2022 - 2024 — McMap. All rights reserved.