As FP has become fashionable, and for good reasons, you may be tempted to bash object-oriented programming. So I believe it’s time to firmly affirm that object modeling in 2018 is definitely not your 1998 object modeling.
Objects delusions that we learnt to dismiss
After two decades of trying things and realizing their flaws, now we know better. Here is a list of delusions you have to give up.
Not looking for naive realism anymore
The simplest your object model the better, even if it doesn’t resemble the real-life concepts it is supposed to represent.
A car object doesn’t have to have 4 wheels. A dog doesn’t have to have a dog.bark() method, even if you need it to bark. And from a shipping point of view, a book doesn’t have to have a NumberOfPages attribute.
No distributed objects anymore
Interacting with distributed objects over some network « without even knowing » used to be a very seducing abstraction, leading to little wonders like CORBA, RMI or EJB. Unfortunately it is much too leaky, and you really can’t ignore when it’s distributed. Distributed data, documents, DTO work fine. Remote calling pure services works fine too. But don’t distribute full-fledged objects with data and behaviors.
No canonical reusable objects anymore
By age of 35 you should have try of design at least one company-wide, pompous, onerous and impractical canonical unified domain model.
— DDD Borat (@DDD_Borat) May 22, 2018
There is not such a thing as canonical objects that are reusable for all cases and for everyone. Instead, objects dedicated to specific uses in particular Bounded Contexts are the way to go. Even if this means some reasonable amount of apparent code duplication.
No mutable state in objects anymore
Encapsulating state doesn’t have to mean encapsulating mutable state. Good objects are immutable objects. As a rule of thumb, just try to make all your objects immutable, except when they really have to be mutable. For example the whole point of a Builder is to deal with the very mutable process of building another immutable object or a graph of immutable objects. Setters (and even getters) are no longer desirable.
No more anthropomorphic objects anymore
Objects are not people. They can collaborate and exchange messages, but as objects, not as human beings. They don’t have be named after unconvincing made-up names that indicate agency by ending with -er: SomethingManager, SomethingHandler etc.
Not what you learnt at school
If you randomly go for an image search on “UML diagrams” on the web, you’ll find that most examples fail at one or many of these aspects. If you learnt objects at school, you also probably learnt about them in a naive and now outdated fashion.
Objects didn’t fail, and they will be back with a vengeance. It’ll take a few more years, just long enough for mainstream developers to keep on bashing objects while learning how to do FP wrong.
Thanks to Yvan Phelizot for the review and feedbacks