Naming Interfaces in PHP, Java, et al.
Pet Peeves #1
I've written about this already in a previous post, but I think it deserves a post of its own. I think it is time we stop appending the word
Interface to our interfaces name.
It is just completely unnecessary.
The same goes for anything similar: prepending
Abstract to abstract classes or suffixing traits with the word
Trait, or appending
Exception to exception classes.
It makes sense for three main reasons.
First, it makes sense from an object-oriented naming point of view. It makes sense that the most abstract thing has the most "pure" name in a cluster of implementors. For instance, Symfony Serializer has
SerializerInterface and the default serializer implementation is called
Serializer. Doesn't make more sense to have
Serializer as the interface (because that is the thing being abstracted) and then call the implementation
DefaultSerializer? Notice how instead we prepend the implementation name to say something specific about the implementation instead. It makes sense, because the implementation is specific, while the interface is generic.
Second, it makes sense from a screen real-estate point of view. Again back to Symfony serializer, which implements five interfaces that have the word
Interface suffixed on their name. So we are using 45 extra characters we don't need, over half of the recommended maximum characters per line (80). And again, we don't need those characters. They are just unnecessary pollution.
Third, it makes sense from a modern tooling point of view. The only possibility that makes sense for keeping this practice, is that we do it to be able to easily identify what is an interface when reading our code. The problem with the argument though is that code assistance tools are pretty good at telling us what the stuff in our code is. They even put different icons on our IDE for interfaces, abstract classes and normal classes. They also autocomplete only interfaces when we type
implements and they show what a type is when we hover over the name. I understand this could have been a good reason back in the days when we wrote software with simple text editors. But not today.
I hope I have convinced you. Please help me stop this madness that has completely taken the PHP ecosystem (and others!) by storm. We don't need to do this.
Did you find this article valuable?
Support Matías Navarro-Carter by becoming a sponsor. Any amount is appreciated!