59

Say you're working on the core module of the foo project for BarBaz Incorporated. Your code fragment might look like this:

package com.barbaz.foo.core;

import com.barbaz.foo.util;

What would the convention be if your company's website was not barbaz.com, but instead bar-baz.com?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • So it turns out there is a small amount of Java happening on the other side of the building, although I didn't find this out until after I asked the question. Their approach, following this example's format, was `com.bb.foo.*;` which makes sense considering all the legacy systems are bbs, bbw, bbj, bbl and what not. Thanks for the input though! – corsiKa Sep 09 '10 at 16:42
  • I reopened this because after thinking about it, it isn't a duplicate of the other question. That question is what do you do about a space whereas this question is what do you do about a hyphen. – corsiKa Dec 11 '18 at 03:34

5 Answers5

87

The SUN-era Java Language Specification gives a suggested convention:

If the domain name contains a hyphen, or any other special character not allowed in an identifier (§3.8), convert it into an underscore.

But it's just a suggestion...

David Tonhofer
  • 14,559
  • 5
  • 55
  • 51
Steve Turner
  • 1
  • 2
  • 2
  • 22
    That's interesting, considering how most Java suggestions recommend against the use of underscores in most identifiers. – corsiKa Oct 19 '10 at 00:09
  • According to this, it's a REQUIREMENT, not a suggestion (JSR)? Isn't that correct? https://blog.eisele.net/2010/09/hyphen-ussage-in-package-names.html – atom88 Mar 11 '21 at 18:47
70

I just looked through my browser's history, and in the last 2 months I haven't visited a single domain with a hyphen. So the convention is to rename the company.

Alternatively, leave out the hyphen, because BazBaz won't ever include Baz=Baz's code in their own.

Amy B
  • 17,874
  • 12
  • 64
  • 83
18

I would personally just remove the hyphen. You could change it into an underscore, but that would look pretty ugly.

It's highly unlikely it's really going to clash with another company with the same name minus the hyphens. Even if both companies are tech companies, both using Java, what are the chances that anyone's going to be using code created by both of them in the same codebase?

To be honest, I wish that Java hadn't gone down this path in terms of conventions. I wonder how many directories called "com" or "org" exist with a single member - a subdirectory with a more meaningful name.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    So I ran into this the other day again (I'm actually not at the same place anymore) and I stumbled across an [Oracle tutorial with a handy chart at the bottom](http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html). According to the way back machine, it's [been around since late 2011](http://web.archive.org/web/20111117054510/http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html), a year after this question. The recommendations at the bottom sure do make me wonder! – corsiKa Oct 18 '13 at 16:53
  • 4
    @corsiKa: Ick. Looks horribly ugly to me! I can't remember the last time I saw an underscore in a package name, to be honest... – Jon Skeet Oct 18 '13 at 16:54
  • 4
    I work at a company that has a | character in it's official legal entity name. Literal hell. – Bassinator Jul 17 '18 at 16:48
  • My high school still has it in their domain name after more than 20 years. The irony is that its full name does not have the hyphen! – nsandersen Feb 01 '19 at 13:28
  • My packages just have the main domain name sel2in.translate ... or s2n.translate i mean unless there is a company called s2n who also do what i do...leave out the com – tgkprog Sep 17 '20 at 09:34
10

Just drop the hyphen. The package name doesn't need to match the website name at all. It is more important that there's consistency among packages produced by the company so they all use the same base package name.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • 3
    It is also used to avoid namespace collisions in libraries. If barbaz.com created a product using a library from bar-baz.com (who dropped their hyphen) there would possibly be conflicts. In practice this is so unlikely as to be laughable. – Bill K Sep 09 '10 at 16:35
  • 14
    If barbaz.com and bar-baz.com were both producing libraries, one of them would be suing the other. – Erick Robertson Sep 09 '10 at 17:00
  • Upon further consideration, I had to give the checkbox to Steve's suggestion. Check out the links in my comment on Jon Skeet's answer for why. – corsiKa Oct 18 '13 at 16:54
  • I cannot complain about that. – Erick Robertson Oct 18 '13 at 18:58
3

I work on lots of government stuff and we typically use the underscore so bar_baz.

Dave B
  • 236
  • 2
  • 2