Naming interfaces

Interfaces in .NET have a specific naming convention.  They begin with a capital “I”, followed by some text that indicates what the interface represents.  IEnumerable is a classic example; it means the implementing object can be enumerated.  This naming convention, however, is pretty unique to .NET.  In the Java world, interfaces do not have a special prefix to indicate that they are interfaces.  Java, unlike .NET, uses two keywords, “extends” and “implements”, to indicate whether a class is participating in an inheritance hierarchy and/or representing some behavior, respectively.  In .NET, we use a simple colon and rely on the ordinal position of the class/interface names that come after to indicate the same.

The prefix “I” used for interfaces is seen by many (myself included) as unnecessary.  It serves no useful purpose other than to provide a visual cues to developers, allowing them to identify interfaces quickly while coding.  Java IDEs typically provide these visual cues so that developers don’t have to rely on a naming convention for this purpose.

Eclipse example 1

Eclipse example 2

Visual Studio has visual aids as well, but they are not as obvious, in my opinion.

I do think a naming convention for interfaces is still a good idea, but I don’t think the prefix “I” is necessary.  Instead, I would like to propose that interfaces be named for the behavior that the represent, starting with a prefix like “Can”, or “Will”, or “Has”, followed by a description of that behavior.  For example:

  • IEnumerable becomes CanEnumerate
  • IDisposable becomes CanDispose
  • IQueryable becomes CanQuery
Not only is this naming convention more descriptive and less contrived (sometimes it is difficult to come up with an interface name like IXyzable), it forces developers to really think about the behavior they are trying to implement, and may dissuade them from creating interfaces that have nothing whatsoever to do with behavior.  (The sudden explosion of IOC mania has obscured the real purpose of interfaces, but that is another topic for another time.)
If you have thoughts on this topic, I’d be interested to hear them.

6 thoughts on “Naming interfaces

  1. Jonathan Allen says:

    And IList becomes CanBeAccessedByIndex?

    There is also an advantage in knowing whether someone is talking about an interface or a class when there isn’t an IDE stapled to their forehead.

    As for IOC mania, removing the I from the interface will just result in “Impl” being added to the class name.

  2. IList has more behavior than simple index access. I’ve always thought that .NET collections were a big mess, so I think they would need to be re-organized for more behavior-centric interface names to make any sense.

    There have always been developers who advocate special prefixes for things. Whether it is “I”, or “m_” or “_”, or “txt”, “bln”, “int”, most, if not all, become unnecessary with modern IDEs. And if you’re programming in the .NET space, you’re using a modern IDE.

    I am also against the “Impl” prefix because it really means “not an interface”. But if interfaces are named for behavior, it becomes unnecessary.

  3. Ryan says:

    IAgreeWithJonathan. I see that hideous Impl suffix often in JavaLand, but rarely in .NETLand, because we can have List and IList. If a big part of breaking out dependencies is replacing concrete classes with interfaces, it’s nice to have the “I”s in order to see at a glance how interface-y my code is, or how far I have to go.

  4. Ryan says:

    (Those List and IList were supposed to have T after them, but I guess the comment field thought it was HTML.)

  5. I agree that we should get rid of the I prefix, but I also think your Can, Will, Has proposal is to restrictive. I like interface names that names a _thing_. As such to me List would be a fine interface name. Implementation on the other hand should have more specific names like LinkedList or ArrayList.

  6. chuck says:

    It sort of seems like there’s already a naming convention in the names being things that end in “-able”. The “I” seems redundant then. But I suppose “-able” is just a shorter way to say “Can-“… What about a convention that interface names are adjectives?

Leave a Reply

Your email address will not be published. Required fields are marked *