views:

164

answers:

8

I'd like to define an interface called Tag in a Java package I am working on, but am hesitant to use such an ordinary-sounding name because of the collision issue. (e.g. you can import only one class or interface with a particular name; if there are more than one that share the same name, you can use import for one of them, but the rest you have to explicitly refer to with the entire package name e.g. com.yoyodyne.games.outdoors.Tag)

I also don't really have a more detailed name for it (it's supposed to represent a tag like the tags in StackOverflow posts or other online websites); the closest I can think of is maybe TaxonomyTag.

Are there strategies for dealing with this? The only one I can think of is to define a static class (like Collections) that contains a public interface Tag, e.g. if I call it Taxonomy then I can import Taxonomy and refer to Tag as Taxonomy.Tag -- but that doesn't sound much more helpful.

edit: one widely-known example of this collision is ca.odell.glazedlists.matchers.Matcher and java.util.regex.Matcher which is a pain if you are trying to use regular expressions with the GlazedLists library.

+10  A: 

I don't see a problem with naming the class Tag. Your package name makes it universally unique and that is one of the purposes of packages - to resolve naming conflicts.

Even within the Java API there are multiple classes with the same name: java.util.Date, java.sql.Date for example. If you need both in your code then use the fully qualified name.

Vincent Ramdhanie
The Date example is actually a bad one since they aren't quite interchangable, but that's a whole different issue.I suspect there's a better name. What's it a Tag for? What's the domain? It's it a ConversationTag or ThreadTopicTag or some such? It seems like you've likely got a better name. If it's truly generic, something applicable in multiple apps, well, then so be it.
Chris Kessel
A: 

I wouldn't really be concerned with this.

What you should be concerned with is how well your class/interface name matches what the piece of code actually does. If Tag succinctly describes what the class/interface does and/or is meant to model, then I think it is a great name.

I can't really see the situation where you'd be using this Tag type in the same class along with other Tag types declared in different packages. But, if you have to, then it's not really that much of a pain to refer to the other Tag types by their fully qualified name.

I believe that how well you named something is greater than making things convenient.

matt b
+1  A: 

The best strategy is to write classes which do one thing well. These classes do need the minimum of imports, so you have the reduction of import statements.

I looked for standard Tag interfaces; found one in java.swing..html, another one deep in servlet API, and another in tapestry library. I am sure that your class should not directly use one of these (or similar APIs), so you may not be afraid of namespace pollution.

Other solution is to prefix tag with the object it will be used on. E.g. ArticleTag. But you must carefully choose the object name. Or, anyway, you can always refactor it later.

san
+1  A: 

How many people are going to be using this class? If it's meant to be a general purpose library, I would go with a less-generic name to avoid collisions. If it's just you, and you really don't bite the bullet and go with fully-qualified names for now.

If it becomes a problem before you release the package, just refactor it to a new name.

In similar situations I have found some alternate name for short class names because I hate using FQNs. Even something like JasonSTag can work as a temporary fix; just don't release it that way. Often halfway through implementation I'll find a better way to describe the class, something more descriptive than "Tag".

Alex Feinman
A: 

Generally the number of conflicts, even with "ordinary" sounding names, is low. I'd chose a meaningful name within the context of the package.

Do not do somethiong "silly" like prefix it with the company name, eg: YoYoDyneTag.

TofuBeer
A: 

It has gone out of style to use adjectives/adverbs as interface names recently, however, in your case it wouldn't sound that bad if you used 'Tagable' or 'TaxonomyTagable'.

Taggable would be the object of the tag (I'm using TaggedObject), the tag itself is what I'm looking for a name.
Jason S
A: 

This only tend to be a problem if you need to use more than one class with the same name in a single class. Examples: java.awt.List and java.util.List, java.util.Date and java.sql.Date.

If you stay away from those already used in the standard Java runtime you will most likely not have a problem.

Whatever you do - make the name you choose a good and descriptive one - this goes especially for those in a public API. You will live with them forever.

Thorbjørn Ravn Andersen
+1  A: 

Are you being lazy? If your class is using imports such that "Tag" could be misconstrued by someone reading your code, even momentarily, then it is worthwhile to think of a better name, despite the package naming convention. Don't underestimate the power of naming---or renaming as the class changes.

Glenn