7

If I'm creating a Java class for something that has a sequence of numbers in its name, e.g. “ISO 8859-1”, how should I name my class?

  • Iso88591 seems wrong, since there's no boundary between the numbers.

  • Iso8859_1 seems better to me, but according to Effective Java “… Constant fields constitute the only recommended use of underscores.” (It's also caught by Checkstyle's default behavior.)

  • I could also name it Iso8859Dash1, but that looks awful.

  • Is there anything in your software requirements that mandates following Checkstyle's or Effective Java's guidance? – Robert Harvey Aug 28 '17 at 18:07
  • Checkstyle, yes, that's what we're using. Effective Java, no, but I think it's a pretty good source of guidance, generally. – codebreaker Aug 28 '17 at 18:08
  • The only guidance from Oracle (and other seemingly reputable sources) is that the class name should start with a capital letter and be a noun. I would think that means you're free to decide on a convention that's best for you in this particular case. – Robert Harvey Aug 28 '17 at 18:09
  • Also, to be clear, did you just say that your software requirements mandate that you follow Checkstyle? – Robert Harvey Aug 28 '17 at 18:10
  • @RobertHarvey They don't mandate that we follow Checkstyle's default behavior. So we could definitely change our Checkstyle settings to allow underscores if that seems like the best option. – codebreaker Aug 28 '17 at 18:14
15

Guidelines are guidelines, not immutable laws of nature. You have found an excellent example of a sensible name that cannot be easily mapped into the normal Java naming conventions. So what should you do?

Make an exception.

Of all your suggestions, Iso8859_1 is the only name that clearly and unambiguously expresses what the class is about. The various coding guidelines are just heuristics to find clear names. Here they fail, and you would do well to ignore them.

Checkstyle is configurable. Either adapt the config to allow such names, or except the class declaration from the name check. Make the tool help you, instead of making yourself slave to an incompetent tool.

(Rationale: The default Java coding conventions suggest the use of camelCase to visually emphasize word boundaries within an identifier. Underscores are used as word boundaries in constants because constants should be all-uppercase. Here you need a boundary between digits. As ASCII digits don't have case you can't use camel case here, and by elimination the only remaining option to separate digits in a name is underscores.)

| improve this answer | |
  • Somewhere in Apple's guidelines they mention that words like ISO, HTTP, URL should be written either all uppercase or all lowercase, obviously following your usual rules. As if they were one letter – gnasher729 Aug 28 '17 at 18:59
  • @gnasher729 The Java coding conventions make no statements on acronyms, except that uncommon acronyms should rather be spelled out. The advantage of treating them as words is that word boundaries are more easily visible, e.g. HTTPServer vs. HttpServer. I'm personally a Kebap-case and otherwise snake_case fan which entirely avoids these problems :) – amon Aug 28 '17 at 19:06
0

Unless there is a possibility of confusion or collision, I wouldn't sweat this. For example, if there was going to potentially be a class representing an ISO 885-91 standard, you might have an issue. I would just drop the dash in this case.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.