Should interfaces be named with Interface suffix? And abstract classes with Abstract prefix?
--
Let’s think for a while — where do we use class/interface name?
- During creating object from the class (
new Product(...)
) - In usage
Creating objects
We aren’t able to create objects from abstract classes and interfaces, so it is always without prefix/suffix. I hope no one suggest to call a concrete class a new ClassProduct(...)
, but rather new Product(...)
.
OOP
I’d like to introduce a philosophical note here. What is object-oriented programming about? I believe that OOP is about
Encapsulating data by behavior.
Behavior means we use methods of objects, not their properties.
Usage
Let’s consider following code
class CartItem {
private string $productId;
private int $amount; public function price(Prices $prices): Price {
$unitPrice = $prices->unitPrice($this->productId);
return $unitPrice->multiply($this->amount);
}
}
What is the Prices
? Is it a class? Is it an abstract class? Is it an interface?
We don’t know.
And we don’t care!
The only think we care is
- What it represents? It represents prices.
- What method it provides? It provides unit price by a product ID.
Would the readability improve if we added prefix/suffix?
public function price(PricesInterface $prices): Price {public function price(AbstractPrices $prices): Price {
I don’t see a value in such naming. The original variant is simpler and easier to read.
public function price(Prices $prices): Price {
Conclusion
When we use a certain concept, we care only about WHAT it represents and WHAT methods it provide. We don’t care whether it is a class, abstract class or interface.
Therefore I suggest to not use prefixes/suffixes for interfaces and abstract classes.
I suggest to spend more time on good naming instead.