views:

291

answers:

6

Hello!

NOTE: This is not the popular interface naming question about using or not using "I" at the beginning.

I encounter often the problem to name an interface, which indicates a belonging or property of a class.
(Please see following list)

Let's brainstorm, what kinds of interfaces are there?

  • Indicate the "kind" of a class
    DataStructure, Number, Thing

  • Indicate the "profession" of a class
    Comparator, Executor, Listener

  • Indicate a possible action performed with a class
    Comparable, Executable, Closeable

The above are all clear to anyone, but let's get to my problem:

  • Indicate a belonging or property of a class
    HasListener, LinksToRoot, BelongsToParent, KnowsSibling, ContainsChildren, Named, WithDescription, ...?

So, the last point is my problem. My english is not perfect, but even I feel strange about such names. They sound to me less successfully chosen then other, less meaningful. But I often end up choosing right this kind of names.

It is even a greater discomfort in C# where interfaces are expected to start with an 'I':
IHasListener, IKnowsSibling, ...
Sound to me like LOLSPEAK "I can haz a kitteh, tawtally being full of cuteness, OMG!@#!"

So, how should I name an interface which indicates a belonging or property of a class?

+8  A: 

The problem is the way you choose to describe the "belonging of a property".

Most of your examples you gave can be mapped to the other categories you mentioned.

Just a few, for example:

HasListener => implements Listenable

ContainsChildren => implements Parent

WithDescription => implements Descriptable

Try to stick with more conventional naming schemes, preferably ones that describe your object in the best, more readable manner.

Also, make sure you are not over-interfacing your classes with useless interfaces. Make it very concise and to-the-point, otherwise you'll developers reading your code will get lost very fast.

Yuval A
I don't think that "HasListener => implements Listenable" is correct.
ivan_ivanovich_ivanoff
hasListener() should probably be a method for Listenable interface... Youy are mixing up lots of naming schemes...
Yuval A
WithListener => Describable
Software Monkey
+4  A: 

In some of the problem cases that you outline, I think there may be an answer by looking "from the other side":

HasListener -> Speaker
LinksToRoot, HasParent -> Child (or perhaps Node)
ContainsChildren -> Parent

Of course, different cases will be more or less obvious.

Fredrik Mörk
HasListener : a class contains a listener. Does this fact make it a speaker? But the other examples are good.
ivan_ivanovich_ivanoff
If not a speaker, perhaps a publisher.
gooli
Yes, good comments. Obviously Speaker was just a quick example to illustrate my thought. I guess that the problem domain of the application might give hints for better naming :o)
Fredrik Mörk
A: 

I think you can take some of what you're doing and turn them into "profession"-type interfaces:

  • HasListener -> ListenerContainer
  • WithDescription -> DescriptionContainer (Describable might also work, depending on what exactly a "description" is in this context)

A lot of your other interfaces seem to have something to do with a tree structure. I would suggest naming them according to their function in the tree.

  • ContainsChildren -> Parent or Collection
  • BelongsToParent -> Child

As for the other ones, I'd need to know more about what specifically these interfaces are for. Some of them, like Named, are probably named just fine.

Welbog
A: 

Those sounds like terrible interface names to me, I agree. The "HasListener" sounds more like a method call that should return a boolean than an interface name.

Interfaces should not exist to hold/store properties of a class. They should be used to outline a relationship that all classes that implement it should follow. I personally stick with the "is a" relationship. If there is a direct relationship, i.e. a cat "is a(n)" animal, then I will create an interface for it and name it Animal giving it a reasonable name.

I would be really interested in knowing what the "HasListener" interface outlines. What exactly does it do? Why can't it be named MyProjectListeners(replacing MyProject with the project name) that describes what listeners defined for this project must adhere to?

amischiefr
An object might do a lot more than have listeners. Having listeners might be just one of the few aspects that make up an object. E.g., a Window might support closing - but so might TabItems and other objects. So IClosable is a good interface that describes one aspect of a more complicated object.
Paul Stovell
Closable is indeed a good name for an interface. I was more speaking of the other names such as "HasListener" which describes more of a state of an object than the object itself.
amischiefr
A: 

HasListener is ok, as well as Listenable. I don't have anything against those.

But IHasListener is terrible: first because we don't really need the I prefix to tell that it is an interface (look at the class signature!) and second because it sounds like "I doesn't speak English".

The only interface in Java that I created with the I prefix was IRule. :)

Bruno Rothgiesser
But you should know, that in C# it is a naming convention, accepted by most developers, to start an interface with I.
ivan_ivanovich_ivanoff
Also in Java. Like it or not, many development standards use the convention of appending "I" to interface names.
Yuval A
A: 

I actually quite like the "IHasListener" approach.

In the .NET Framework, you'll find this in interfaces such as:

  • IRaiseListChangedEvents
  • IHasXmlNode

Names like "Listenable" suggest that the implementation does the listening, not that it contains a listener. IHasListener clearly says what it does.

Paul Stovell
What's your opinion about the other names?
ivan_ivanovich_ivanoff