Please stop using these names

Wolfgang Klinger
2 min readAug 15, 2022

--

Stop using IMyInterface and MyInterfaceImpl !

Photo by Kenny Ginapp: https://www.pexels.com/photo/smart-plug-electric-socket-4148576/

I see this in books, in articles, even in production code (you wouldn’t believe it). There are even some conventions in languages like C# or Java to use the I prefix for interfaces. Ouch!

Interface

Why would I want the name of an argument to include the information that it implements an interface? I don’t care. I don’t care if it’s an interface or an abstract or an actual class. I just need to know what the thing does, what it’s meant to do.

public function doSomething(ProductRepository $repository): void

That’s all I want to know. That’s the contract.

We prefer not to declare implementation decisions in such a public manner, since doing so makes them difficult to change. For example, what may occur if you determined that IBar should actually be an abstract class rather than an interface. Should the name be changed to ABar or ACBar?

Implementation

But there is more to come! The class that’s actually implementing the interface is then called BarImpl! Oh my gosh!

Why on earth would I use such a name? That the class is implementing the interface is already part of the language.

final class ProductRepositoryImpl implements IProductRepository

Adding Impl is duplication. Avoid duplication (see book recommendation below on names).

Advice

Without this useless postfix Impl I’m forced to think about a proper name that reveals intent or some fact that makes it clear what I’m working with here. Now compare the above declaration with this one:

final class InMemoryProductRepository implements ProductRepository

This makes sense!

TLDR

The name of the interface or abstract class should be — you guessed it — abstract, and the name of the implementing class should be as specific as possible. But no meaningless (and IMHO even absolutely distracting) abbreviations before or after!

Happy coding.

--

--

Wolfgang Klinger
Wolfgang Klinger

Written by Wolfgang Klinger

Programmer, Photographer, Gardener

No responses yet