23

Ok, I understand the normal conventions of using verbs with functions and nouns with classes. What about interfaces? Is there any methodology when coming up with interface names that may not be that obvious?

Just to make it clear, I'm not talking about whether to put an "I" in front of the name or if to use camelCase or PascalCase. I'm wondering about the method of figuring out a clear, semantic name for an interface.

EDIT I'm obsessing on how to name an interface in the clearest way. I guess it just needs to be a noun too because when I think of naming classes I think of the closest "real" world object it can relate to. I suppose real world interfaces are things like a keyboard, mouse, remote control, ATM screen. Those are all nouns. Anyhow, any additional insight on a good way to formulate interface names would be appreciated.

closed as primarily opinion-based by user40980, user22815, user53019 Dec 3 '15 at 19:41

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Hm.. Based on the title, I thought this was going to be about equating parts of speech between programming and English. In which case, madlibs seem possible. – Izkata Jul 14 '12 at 17:26
  • Remember, when you create a class, you are also creating an interface for objects instantiated by that class... Or to put it another way, classes are realized interfaces. So, they should use the same naming convention. – Daniel T. Dec 3 '15 at 12:26
  • Also, IMHO the "use verbs with functions" is too general. You should use verbs with commands and nouns with queries. – Daniel T. Dec 3 '15 at 12:28
21

I'd say it depends on what the interface defines. In some cases when the interface is rather specific and detailed, I think a noun is best. Examples are IList, ICollection.

Sometimes though an interface is more about adding certain general features to a class. In that case I think an adjective is best. Examples are IDisposable, IEnumerable, ...

Maybe another way to think about this is how many "abilities" your interface defines.

For example, the IList<T> interface defines these abilities: Add, Clear, Contains, Insert, Remove, ... These are all properties of a list so IList is a good name.

IDisposable on the other hand only defines one ability: Dispose. So it is suited for anything that is disposable. Hence the name IDisposable.

  • 10
    I'd suggest using an "_able" adjective if your interface would be be named after things that can be done to your object (e.g. IEnumerable), a "_er" noun if your interface would be named after things your object does to other objects (e.g. IEqualityComparer), and a general noun if your interface is named after a type of thing whose behavior it mimics (e.g. IList<T>). – supercat Aug 8 '12 at 22:53
  • Comment by @supercat should really be included in this answer. It makes lot of sense. – Nikhil Vartak Sep 28 '18 at 5:48
  • Actually prefixing the interface name with I is redundant. – user1870400 Sep 13 at 11:09
5

An interface describes behavior so the names should say so. I am not sure about a rule for this but you will know the name when you hear it.

Some examples:

Flyable      - Can Fly
Workable     - Can work
Negotiatable - Can negotiate.

I think it is not proper to name interfaces with I and call Set with ISet because, as a developer, you should not be worried whether it is an interface or a class. I see this practice does not exist in Java now.

  • +1 for "as a developer you should not be worried wheter iti s an interface or a class." – Kazark Jan 29 '13 at 19:58
  • "as a developer you should not be worried whether it is an interface or a class" - disagree because if it is an interface then you need to know which members you should implement. – Maximus Minimus Jan 29 '13 at 20:55
  • @MaximusMinimus prefixing it with I is redundant regardless. Then why not prefix abstract classes with A, regular classes with C etc?? – user1870400 Sep 13 at 11:14
  • @user1870400 - true, but that's not the part I'm disagreeing with. – Maximus Minimus Sep 13 at 13:38
4

Since an interface is really just an expression of the "what" but not "how" of a type, I would name them in the same way that you name classes, at least from a parts of speech perspective. The only thing I might add to this is that they're often going to be more general than the specific implementers (e.g. "Customer" interface may have "ProspectiveCustomer" and "PayingCustomer" implementers).

0

An interface is used like a class. Therefore, it should also be named with a noun when you name classes with nouns. Prefer abstract nouns, for example "Vehicle" when the classes are "Car" and "Truck".

0

Quick Answer: Interface is more about addition of a SET of abilities, features, common boundaries or interconnections between entities, classes, models, concepts, etc..

Thing about adding common behavior to classes and entities, and then a good meaningful name will come up to identify the set of realted features.

Interface Naming like : IStateMachineBuilder, IUserContextProviderBuilder, IEntityBuilderBuilder, IActiveAware, etc. probably explain the concept.

  • 1
    Yes, interfaces represent contracts, or capabilities; so much is well known. But how does this answer the question of how to name them? All you're really saying in that regard is, "Think about it, and inspiration will strike you." This does not give any recommendation, for instance, which of the following names should be chosen, any why: IStateMachineBuilder (noun), IBuildStateMachine (named after an action), ICanBuildStateMachine (named after a capability), etc. – stakx Aug 31 '13 at 8:29
0

It depends, if the interface contains only methods it will be good to name it using a verbs like "Actions" where as if it represents a generic class wchich contains static , final fields then we can use a generic Noun just like "Vehicle"

Not the answer you're looking for? Browse other questions tagged or ask your own question.