views:

192

answers:

8

I have for some time tried to anthropomorphise (meaning human readable) the names I give to interfaces, to me this is the same as give an interface a role based name – trying to capture the purpose of the interface in the name.

I was having a discussion with other developers who think this is a little strange and childish.

What do the folks of SO think?

Examples (C# syntax):

public interface IShowMessages
{
    void Show(string message);
    void Show(string title, string message);
}

public class TraceMessenger : IShowMessages
{
}

public interface IHaveMessageParameters
{
    IList<string> Parameters { get; }
}

public class SomeClass : IHaveMessageParameters
{
}
+2  A: 

Yes, that sounds like a good idea.

What's the alternative?

Code should be human-readable. Any fool can write code a computer can understand. The difficult part is writing code a human can understand.

Humans have to maintain the code, so it's pretty darn important that it is as easy to maintain as possible - that includes that the code should be as readable as possible.

Mark Seemann
+1  A: 

There's nothing strange about using simple human-readable names. But using the I for interface to also stand for the first-person I as though it's talking about itself... is a little unusual, yes.

But the bottom line is, whatever works for you and is understood by you and your team is fine. You gotta go with what works.

Adam Bellaire
that's why I mentioned C# syntax, Historically I come from a windows C++\COM background and this notation has jsut stuck.I know a lot of people don't use the 'I' syntax...
AWC
+10  A: 

IThinkItsATerribleIdea

LukeH
Too bad there isn't a separate vote for funniest response. ;)
Judge Maygarden
Opinions are good. Justifications are better.
Philip Kelley
+1 public class Grenade : IThinkItsATerribleIdea { public Grenade(){ throw new GoesAgainstTheGrainException(); }}
grenade
@Philip: Here you go: "Name interfaces with nouns or noun phrases, or adjectives that describe behavior." http://msdn.microsoft.com/en-us/library/8bc1fexb(VS.71).aspx
LukeH
Goes to show there are even Ludite programmers as well ;)
AWC
-1: Pretty useless answer (and unfortunately the joke is too old for me to omit downvoting despite being useless)
Frerich Raabe
@Frerich: The question asks "What do the folks of SO think?" and I genuinely think it's a bad idea. `IShowMessages`, `IHaveMessageParameters`, `ICanReadAndWriteStuffToAndFromFiles` or `IThinkItsATerribleIdea` are all equally useless in the sense that they convey their purpose with less readability/clarity than their more conventional equivalents.
LukeH
@Luke: Yes. Too bad none of that elobaration shows up in your answer, so my comment stays true: the answer is useless. It doesn't have any value to the OP who would apparently be more interested in *WHY* you think its a terrible idea.
Frerich Raabe
@Frerich: The answer links out to the MSDN interface naming guidelines: "Name interfaces with nouns or noun phrases, or adjectives that describe behavior." http://msdn.microsoft.com/en-us/library/8bc1fexb(VS.71).aspx
LukeH
+4  A: 

Of course you should always choose identifiers which are human readable. As in: transport the meaning which they convey even to somebody who is not as familiar with the problem to be solved by the code as you are.

However, using long identifiers does not make your identifiers more 'readable'. To any reasonably experienced programmer, 'tmp' conveys as much information as 'temporaryVariable' does. Same goes for 'i' vs. 'dummyCounter' etc..

In your particular example, the interface names are actually quite annoying since somebody who's used to developing object oriented systems will read the inheritance as 'is a'. And 'SomeClass is a IHaveMessageParameters' sounds silly.

Try using IMessagePrinter and IMessageParameterProvider instead.

Frerich Raabe
+1  A: 

In my opinion this approach just adds a greater burden on the developers to come up with such names since it intergrates the I as part of a sentence. I don't find IDisposable for example to be more difficult to read than ICanBeDisposed.

AnthonyWJones
A: 

Interfaces describe behavior, and so I name them so as to to communicate the behavior they are mandating. This 'generally' means that the name is a verb, (or adverb) or some form of action-describing phrase. Combined with the "I" for interface, this looks like what you are doing...

ICanMove, IControllable, ICanPrint, ISendMesssages, etc...

using adverbs as in IControllable, IDisposable, IEnumerable, etc. communicates the same thought as a verb form and is terser, so I use this form as well...

Finally, more important (or at least equally important) than what you name the interface, is to keep the interfaces you design as small and logically contained as possible. You should strive to have each interface represent as small and logically connected a set of methods/properties as possible. When an interface has so much in it that there is no obvious name that would describe all the behavior it mandates, it's a sign that there is too much in it, and that it needs to be refactored into two or more smaller interfaces. So, maming interfaces in the way you are proposing helps to enforce this type of organizational design, which is a good thing.

Charles Bretana
A: 

So long as the semantics of what is trying to be achieved aren't lost and terseness isn't irreparably compromised (IDoLotsOfThingsWhichIncludesTheFollowingColonSpace...). I wouldn't generally mind somebody other than myself doing it. Still, there are plenty of contexts in which terseness is paramount, in which this would be unacceptable.

Rushyo
+1  A: 

In the OP's examples, the anthropomorphic way compares well against alternatives - eg: IShowMessages vs. something like IMessageShower. But - this is not always the case. Interfaces I have used when programming game objects include: IOpenClosable and ILockable. Alternatives like ICanBeOpenedAndClosed and ICanBeLocked would be more verbose. Or you could simply do IAmOpenClosable and IAmLockable - but then you'd be adding the "Am" just for the anthropomorphic effect with no real information benefit. I am all for minimizing verbosity if the same amount of information is conveyed.

Anon