Let us talk about objects naming

24 September 2023

Naming is very important when it comes to architecture and design. Words express the intent, and comprehensive language helps us better understand what we have, do and want.

Here is a culprit. In e-commerce we have many entities used in processes and daily basis workflow. The most essential of them are customers, orders, carts and items. Except the interesting detail we often tend to lose out of sight. Items is not a sole entity.
It is Product that customer sees in the shop and it represents the generic information about the selling item.
It is CartItem that customer puts into their cart and we use to store product details.
It is OrderItem that customer generates once they place an order and we use to track the order details.


interface Product
{
    function getName(): string;
    function getImageUrl(Size $imageSize = Size::THUMBNAIL): string;
    function getPrice(): Price;
}

interface CartItem
{
    function getName(): string;
    function getUnitPrice(): Price;
    function getQuantity(): int;
}

interface OrderItem
{
    function getName(): string;
    function getUnitPrice(): Price;
    function getQuantity(): int;
    function getReservationDetails(): StockReservation;
}


interface Cart
{
    function addProduct(Product $product): CartItem;
    function listItems(): CartItemCollection;
}

Depending on the complexity of the business process, there might be a few additional entries. The fact here is that we need multiple different models with very similar data. That similarity often tricks us into thinking that we deal with the same context. It is more transparent to those who practice rich domain models and thus can see that entity is handling too much or doing it in a wrong manner. It's not the end of the world, but it does occur frequently and can be unpleasant to deal with.
Especially if you're working with a big project in production.

Therefore, whenever you deal with the naming, that means slightly different thing from case to case, it might be a good idea to double check if your entity has different contexts or consumers.

Don't take everything plain: we have to challenge and prove the information we face.

Here is what really helps me to do it.