41

After reading Should package names be singular or plural? it occurred to me that I've never seen a proper debate covering one of my pet peeves: naming implementations of interfaces.

Let's assume that you have a interface Order that is intended to be implemented in a variety of ways but there is only the initial implementation when the project is first created. Do you go for DefaultOrder or OrderImpl or some other variant to avoid the false dichotomy? And what do you do when more implementations come along?

And most important... why?

Gary
  • 24,420
  • 9
  • 63
  • 108

9 Answers9

70

Names have the opportunity to convey meaning. Why would you throw away that opportunity with Impl?

First of all, if you will only ever have one implementation, do away with the interface. It creates this naming problem and adds nothing. Even worse, it could cause trouble with inconsistent method signatures in APIs if you and all other developers aren't careful to always use only the interface.

Given that, we can assume that every interface has or may have two or more implementations.

  • If you have only one right now, and you don't know in what way the other may be different, Default is a good start.

  • If you have two right now, name each one according to its purpose.

    Example: Recently, we had a concrete class Context (in reference to a database). It was realized that we needed to be able to represent a context that was offline, so the name Context was used for a new interface (to maintain compatibility for old APIs), and a new implementation was created, OfflineContext. But guess what the original was renamed to? That's right, ContextImpl (yikes).

    In this case, DefaultContext would probably be ok, and people would get it, but it is not as descriptive as it could be. After all, if it's not offline, what is it? So we went with: OnlineContext.


Special case: Using the "I" prefix on interfaces

One of the other answers suggested using the I prefix on interfaces. Preferably, you don't need to do this.

However, if you need both an interface, for custom implementations, but you also have a primary concrete implementation that will be used often, and the basic name for it is just too simple to give up to an interface alone, then you can consider adding "I" to the interface (though, it's completely fine if it still doesn't sit right for you and your team).

Example: Many objects can be an "EventDispatcher". For the sake of APIs, this must conform to an interface. But, you also want to provide a basic event dispatcher for delegation. DefaultEventDispatcher would be fine, but it's a bit long, and if you are going to be seeing the name of it often, you might prefer to use the base name EventDispatcher for the concrete class, and implement IEventDispatcher for custom implementations:

/* Option 1, traditional verbose naming: */
interface EventDispatcher { /* interface for all event dispatchers */ }
class DefaultEventDispatcher implements EventDispatcher {
  /* default event dispatcher */
}

/* Option 2, "I" abbreviation because "EventDispatcher" will be a common default: */
interface IEventDispatcher { /* interface for all event dispatchers */ }
class EventDispatcher implements IEventDispatcher {
  /* default event dispatcher. */
}
Nicole
  • 28,111
  • 12
  • 95
  • 143
  • 3
    +1 for solid answer. Like the reasoning behind not using Impl. – Gary May 12 '11 at 20:35
  • 1
    Thanks. I updated with my thoughts on "I", since it's frequently seen in practice -- hope it doesn't change your mind on the +1 :) – Nicole May 12 '11 at 20:39
  • 1
    Thanks for the update. Interesting case for the I prefix. – Gary May 12 '11 at 20:46
  • 3
    +1 absolutely agree. Naming an interface away from the domain concept it represents is entirely missing the point. – rupjones May 12 '11 at 22:10
  • +1 for suggestion of ditching interfaces when you have just one implementation. – rmaruszewski Aug 10 '12 at 07:48
  • Don't understand the last sentence. It almost sounds like you want the class name to start with I, which is the opposite of C#, where interface names start with I. – RenniePet Jul 29 '13 at 13:35
  • @RenniePet Good point, thanks. I meant "implement", so I've corrected that and added an example. – Nicole Jul 29 '13 at 19:39
  • 12
    "if you will only ever have one implementation", how do you know beforehand that you will only have one implementation ? "every interface has or may have two or more implementations"... before having two implementations, you first have none, the you have one, then you have two, I mean there can be a moment when you have only one implementation, just prior to implementing the second one. – Tulains Córdova Jul 29 '13 at 19:49
  • @user61852 I think you are picking at semantics here. I'm talking about intended design and the eventual state, respectively to your complaints. No need to be pedantic; if this advice doesn't ring true for you, don't take it. – Nicole Jul 29 '13 at 20:10
  • 3
    @NickC I'm not being pedantinc about semantics (I'm a poet and I didn't even know it). English is not my mother tongue so I cannot be pedantic about it. I was talking about flawed logic. You use interfaces for decoupling. That doesn't require a certain number of implementations. – Tulains Córdova Jul 29 '13 at 20:24
  • 2
    @user61852 Ok, let me be more specific. I'm leaving some judgment up to the reader. I realize that the precise number of implementations cannot be guaranteed in design the design phase, but I think most programmers can make a good judgment. If the type of object feels, by nature, to be single-classed, then I wouldn't use an interface+class pair. I was trained early on to always use interfaces; I think that advice is flawed. Second, based on that initial principle, the reader can assume: if they have an interface, they *intend* to have more two or more implementations. That is all I am saying. – Nicole Jul 29 '13 at 21:06
  • @user61852 I don't see the issue as it was stated really. If there is no intent on multiple types of a object skipping the interface is fine. If you later in the development decide you need different behaviors you could always extract an interface and go from there... – Rig Jul 30 '13 at 00:18
  • 5
    `if you will only ever have one implementation, do away with the interface` - unless you want to test the component in which case you might want to keep that interface in order to create MockOrder, OrderStub or similar. – JBRWilkinson Feb 16 '14 at 17:18
  • 1
    Creating an single implementation "impl" class is a classic case of YAGNI - "You ain't gonna need it". It's fatuous to create an interface-class pair just for the sake of it, in the off chance that some time in the future you're going to need lots of implementations. That's what refactoring is for. It's relatively trivial to convert a class into an interface. Calling something "InterfaceNameImpl" suggests that there was never a need for an interface in the first place. A class is perfectly sufficient. – ayahuasca Jul 04 '18 at 09:57
18

I decide the naming by the use case of the interface.

If the interface is used for decoupling, then I choose Impl for implementations.

If the purpose of the interface is behavioral abstraction, then the implementations are named according to what they are concretely doing. I often append the interface name to that. So if the interface is called Validator, I use FooValidator.

I find that Default is a very bad choice. First it pollutes code completion features, because the names always start with it. The other thing is that a default is subject to change over time. So what first might be a default can some time after be a deprecated feature. So either you always start renaming your classes as soon as defaults change or you live with misleading names.

SpaceTrucker
  • 1,462
  • 10
  • 13
12

I agree with Nicole's answer (particularly that the interface probably isn't necessary in most cases), but for the sake of discussion I'll throw out an additional alternative other than OrderImpl and DefaultOrder: hide the implementation behind a static factory method like Orders.create(). For example:

public final class Orders {
  public static Order create() {
    return new Order() {
      // Implementation goes here.
    };
  }
}

With this approach, the implementation could be an anonymous inner class, or it could be a private class with Default or Impl in the name, or it could be named something else entirely. Which ever choice is made, the caller doesn't need to care, so you get more flexibility now and later on when/if you decide to change it.

Some great examples of this pattern in practice are the java.util.Collections and java.util.concurrent.Executors utility classes, whose methods return hidden implementations. As Effective Java mentions (in Item 1), this pattern can help keep the "Conceptual weight" of the API smaller.

Nicole
  • 28,111
  • 12
  • 95
  • 143
Andrew McNamee
  • 221
  • 2
  • 3
4

I always go for OrderImpl simply because it shows up alphabetically right after the Order interface.

Ben Hoffstein
  • 509
  • 3
  • 6
  • 2
    And how to do you handle the ongoing further implementations, for special handling and so on? – Gary May 12 '11 at 20:14
  • 2
    You asked about the initial implementation. Once you start implementing DomesticOrder, ForeignOrder, or whatever, they are named more thoughtfully and without regard to their position in the alphabet. – Ben Hoffstein May 12 '11 at 20:19
  • Quite right - I've edited the original question to reflect this new requirement. – Gary May 12 '11 at 20:20
  • 1
    Not a good idea If there are going to be more that one implementation. – Sadegh Jul 03 '17 at 19:27
1

I think that while Default can make sense in some cases, it would be more helpful to describe the implementation. So if your interface is UserProfileDAO then your implementations can be UserProfileSQLDAO or UserProfileLDAPDAO or something like that.

jiggy
  • 1,590
  • 11
  • 15
0

if possible, name it after what/how it does its thing.

usually worst approach is naming it after how it is supposed to be used.

If it is supposed to be used as base class for implementation, you can use BaseX or AbstractX (if it is abstract (but try to squeze in what it does, because if it did not do anything you would not create it, you already have interface). If it provides simplest possible functionality and it is expected to be used directly (not extending it) when such functionality suffices, you can call it SimpleX or BasicX.

If it is used unless some other implementation is provided, name it DefaultX

user470365
  • 1,229
  • 6
  • 8
0

Try to figure out what is more concrete or specific about the class than the interface and incorporate that into the name.

Say you have an interface for persisting data, FooDao. Persistence could be to a flat file, an XML document, a spreadsheet, a relational database, a non-relational database, etc. If the class is persisting data to any SQL database, try FooSql implements FooDao.

If you have code specific to a relational database vendor, try FooMssql, FooOracle, or FooDb2. Or, maybe FooDaoMssql or FooDaoPostgre would suit your purposes better.

The point is, don't just add letters that only serve the purpose of creating a unique identifier; try to give the name something more meaningful. That has two good effects. It still communicates more meaning to the next developer, even if you have only one implementing class. Even if you start with only one implementing class, if you ever find yourself needing another implementation, you won't find yourself with names like ..Impl and ..Impl2, or ..Impl and ..Mongo.

0

You can name interface with prefix I (IWhatever) and then make implementation Whatever.

Official Java code conventions don't tell about this kind of naming for interfaces, however this kind of naming helps recognition and navigation.

Matthieu
  • 4,559
  • 2
  • 35
  • 37
  • Why would you prefix the interface with an I? Is it to aid recognition in navigation? – Gary May 12 '11 at 20:19
  • @Gary: prefixing interface types with I is a well-established convention in the Delphi language. In Delphi class types are prefixed with T. So we would have an IOrder interface and a TOrder default implementation with TSomethingOrder and TBullMarketOrder as specific implementations. – Marjan Venema May 12 '11 at 20:22
  • 3
    I've seen this convention used heavily in .NET development as well. Perhaps because Microsoft's documentation and examples use it. – Ben Hoffstein May 12 '11 at 20:23
  • 5
    It appears that @Renesis has come up with a useful case for using it within Java. Personally, I'd rely on the IDE to tell me the difference otherwise we'd have E's for Enums, C's for classes and all largely redundant. – Gary May 12 '11 at 20:51
-2

Most of these answers describe what is being done but not why.

What has happened to OO principles? What does Impl tell me about a class and how to use it? You should be concerned about understanding the usages not how it is constructed.

If I create a Person interface, what is a PersonImpl? Meaningless. At least DefaultPerson tells me whoever coded it shouldn't have created an interface.

Interfaces are typing for polymorphism and should be used as such.

  • 1
    The accepted answer from @NickC goes into some detail about what is being done and why. – Gary Nov 13 '13 at 16:32