OOP at Scale
Quick, what’s the defining item of object oriented programming? Classes? Hierarchy? Inheritance? Enterprise Java Factories?
According to Alan Kay, who came up with the term over 50 years ago, the 3 defining parts of OOP are:
- Message passing
- Encapsulation
- Dynamic binding
Nothing about objects, classes, or inheritance. More about isolation and clear boundaries. You can (and should) do that with your code as well. Use function calls (message passing) instead of doing things yourself. Don’t set up some shared state and pass control off, explicitly send the required info and explicitly collect the answer.
Keep like things (data) and functionality (methods) together. The most common way to do that is with a class, but duck typing can suffice. It doesn’t need to be a class. It could just be a library. Everything about an employee could start with Employee_ and just be a loose method in there. Use data structures that collect all the relevant information together, not just a big heap of data.
Wait until as late as possible to decide exactly which method to call. Sure, you know what you want to do, and how to call it (which message to send), but don’t decide exactly which one until later. Dependency injection is a great example. If you need to durably store a blob of data just call the Store() method. Which store (file system, S3, database, testing, etc.) you determine at runtime because it’s encapsulated.
That’s Object Oriented Programming.
Now consider microservices. While there are lots of transports, by definition, all communication between services is via a message. It has to be because the thing you're talking to is potentially running on a computer miles away. You’ve got to send a message.
And since that service is somewhere else you can’t just reach inside it for some info or to change something. You have to send a message. It doesn’t get much more encapsulated than that.
As for dynamic binding, there’s DNS, which can change between one message and another. And most microservice systems have another level of load balancer behind that DNS entry, so the actual service you’re talking to is decided until after you make the request, and milliseconds before the message is handled. It doesn’t get much more dynamic than that.
And that’s OOP at Scale