Question

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.

Was it helpful?

Solution

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.)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
scroll top