Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good (natural-language) naming scheme for belonging or property interfaces

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?

like image 635
ivan_ivanovich_ivanoff Avatar asked Jun 23 '09 12:06

ivan_ivanovich_ivanoff


2 Answers

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.

like image 74
Yuval Adam Avatar answered Oct 23 '22 23:10

Yuval Adam


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.

like image 37
Fredrik Mörk Avatar answered Oct 23 '22 21:10

Fredrik Mörk