Use and overuse of interfaces

Cedric’s blog entries on Extensibility the interface way, More on interfaces, and Numbered Interfaces got me thinking more about how often to use interfaces, prefixing them with I, adding an Impl to the end, etc… so for my own benefit I wanted to break it down a bit:

1. Interfaces starting with I: I could take it or leave it. In practice I don’t care if I’m programming against a class or an interface, I generally use an IoC container these days so I’m rarely newing a class anyhow. It is convenient knowing something is an interface from the name though.

2. Using interfaces for almost everything: I think we’ve swung too much in the other direction now and I see a lot of arbitrary interfaces with an Impl class that nobody will ever want to switch the implementation of. Interfaces are great and they should be used liberally but it adds unnecessary bloat to have an interface for everything. As long as you’re using a factory or dependency injection you can always refactor it into an interface/implementation later with most IDE’s in a snap.

3. Naming implementation classes InterfaceImpl: If you can’t conceive of a name that would differentiate this implementation from another, maybe it’s not time for an interface yet! I much prefer implementation names that tell you what differentiates it such as MemoryLruSessionCache, DiskFifoSessionCache, etc…

4. One thing I haven’t fully resolved is whether the implementation of say the SessionCache interface should be named SessionCacheSomeimpl or SomeimplSessionCache. I definitely favor the former because in practice in an alphabetical listing then the interface and the implementations are next to each other. On the other hand it doesn’t work with I-prefixed interfaces. It would also mean the List interface should have an implementation such as ListArray and that feels strange to me, probably just because I’m not used to it.

5. Versioning interfaces with SomeInterface1, SomeInterface2, etc…: Haven’t formed an opinion on this yet.

This entry was posted in Software Engineering. Bookmark the permalink.