Back to homepage

Object oriented programming talk

Posted on November 12, 2021

I had a talk on a meetup recently about my view on Object oriented programming. Here is a transcript.

There is also a recroding of the event, its only in czech however.

“Object oriented programs are offered as alternatives to correct ones” ~ Edsger W. Dijkstra, TUG LINES Issue 32, August 1989
Alan Kay, the inventor of the term object oriented programming once said this is what he meant by OOP. Sounds interest but what it actually means?

Alan Kay was a biologist. So the inspiration for OOP has to do a lot with biology of cells and organisms. Amazing thing about organisms is how well they scale and survive.

By scaling I mean that the tiniest living thing works the same way as the blue whale. So it scales in size. It also scales in features without an effect on the rest of the system. Its also very error prone. A lot of our cells dies every day and we keep on living.

How is that possible? Mr Kay identified that the key to this is cells and their isolation. Cell has a membrane that keeps its internals hidden and also protects them from outside world. Cell also has some purpose it usually does one thing on nothing else. We call it isolation, or as in quote "local retention and protection and hiding of state-process."

Cells do not interact directly. The only way they interact is by releasing hormones. They do their task and inform the rest of the system (body) that they did by releasing the hromone into bloodstream. This is what messaging means. Also note that the message is send in a "fire and forget" style. The cell is unaware if anyone will respond and how. The target cells are known at the point the hormone reaches them, not when its released. That is the extreme late binding.

Just to recap. Main goals of OOP are scalability, fault tolerance and ability to adapt without downtimes. And by scalability I dont mean only horizontal scaling in AWS. By scaling I mean also adding features, new parts of the system, etc... To achive that there are three principles proposed

But how is actually any language helps to achieve those goals?

We often argue if OOP is better than FP or if Java supports OOP better then C# or Kotlin. That debate is pointless. OOP is not about language but about design of things. We can of course apply those ideas on a language level but it does not matter if they are not applied on architecture level.

Alan Kay created a Smaltalk language which is an interesting language BUT I would argue that language is not what matters but design we are creating is. I think that no perfect language like that exists. I still recommend to explore Smaltalk just to grasp the idea of “everything is an object”. Block of code is an object. A method call itself is an object, etc..

But that is not a topic of this talk.

If language is not object oriented what is?

Programming is a process - so it can be applied on any level of what we as programmers do. Internet is a living example of object oriented programing. And this is no coincidence since A. Kay was one of the designer of ARPAnet the early version of the Internet. Very complex systems hiding their state. Message is sent and we do not know who will respond. Extreme late binding - we don’t know which server will respond until the very last moment and it can change with each message Each object is implemented with different language, technology or even hardware.

Some software architectures are also examples of OOP.

Microservices were even designed with one of the OOP ideas in mind. Fault tolerance - failure of one part of the system does not take down whole system. Alan Kay also had an idea that each object could be written in different language run on different computer etc…

It does not have to be microservices, modular and well written monolith have same properties as microservices. Its about how we write the code not the language and its features.

One big idea of OOP was that data should have the meaning assigned to them. Why? Because its scalable.

If we send only data as message its up to receiver to find interpretation. If I send you this image and you open it with a image preview software you will see a W. Shakespeare picture. If you open it with a unzip tool you will find an html with whole work of Shakespeare in it. Source. This is a nice easter egg, but it has serious consequences.

Lets look at this story where two modules in an satelite which comunicated each in different units crashed. Becuase the data send by one module were left for whatever interpretation.

We can send a schema and description, but its still prone to misinterpretation and it does not scale. If I come up with a new data format I can send you a description but its up to you to write your own interpretter. What Kay had in mind is to bind an interpreter to the data.

There is interesting debate between Kay and r. Hickey on hacker news about this topic.

Another big idea of OOP proposed by Alan Kay is that objects should be able to send messages to objects they know nothing about And still be able to deliver the message as it was meant to be. The most we can do is structural typing (duck typing) but there must be lot more to that.

So many fancy and revolutionary ideas, right?

But this is what we got in the end.

On Java courses at universities you are taught that objects map to real life objects. And its true, the cells, living organisms, not a Car or Shape with Triangle subclass.

...

I showed you Internet and microservices but what about code?

Simplified example but this is a class - also called an object. Pretty mainstream piece of code. What properties of OOP does it have? None. But this is by some still considered as OOP because it uses classes. It is tightly coupled with another class (interface) repository When there is no boss it throws exception (it fails) and takes down whole system possibly. It does not hide its inner state that much - it just does not allow outside access to its field but does not hide actually anything. It does not scale. What if we introduce more types of users for example?

I rewrote it quickly in python using simple functions. I would argue that this example is more object oriented than the previous one. It scales way better - it accepts any list, loaded from DB or hardcoded, it has a structural typing so its extensible and so on.

So If Java (or any other similar language) is not Object oriented language what it is?

Its a language that organizes code in a classes, methods, constructors etc… I have heard a term class oriented, but I like class organized more. Because that is actually the only thing Java forces you. To put your code in classes and methods. Thats all, everythong else is up to you.

It has support for many things - ordinary procedures (static methods), inheritance, interfaces, generics and many others. It is up to us what programming paradigm we will choose. We can even do functional programming in Java and no i dont mean javas Function interface and Streams.

The worst is to choose none and pretend its OOP.

The rest of the presentation I would like to defend the need to go with some code design choice. Because well designed code can get us some benefits of the OOP or at least can get us close to it.

When I said that you can do functional programming in Java I was not joking. Class instances (objects) are just partially applied functions and constructors are equivalent for high order functions.

The last three lines in this example looks just like java but writte with different symbols without any clases and objects.

I found this picture on the Internet. Except naming the second example “functional” it nicely illustrates difference between No design at all and some design. I would call the second example declarative more than functional.

And this is another way ho to write it. It might be called object oriented, but I would also call it declarative or even functional. This is my favorite way how to write code.

This is an example from my recent work. Its not perfect but there are the previous ideas applied in there. The code is declarative, it does not execute anything yet. Its more like SQL. Its also more functional, since its just composition, but classes not functions. When I present such code to someone I get very upset reactions. Its a complex to read, so much new keywords and tabs.

But in fact what I did is that I just wrapped a method calls to classes and put some design sauce around it. Take the classes out and execute it directly instead of making it declarative and this is what you end up with. Standard procedure calls. This is considered readable and normal.

Only thing I did is that I visualised this structure in one place and wrapped it into classes. That allowed me to better test it and compose, thus reuse it better.

I dont mean its the best solution or it is perfect but it is designed somehow at least. And by creating this design we can more focus on the original OOP ideas.

But back to OOP. Data together with meaning left no space for misinterpretation. In the first example there are many problems (see the comment), in the second I gave a meaning to the strings and just by that I fixed those problems. In dynamically typed system test can do the same checks as type system. What else is type system then a unit test after all.

Note that this says nothing about the methods in the Email and Name classes. Because it does not matter. Tha data are bound with meaning, not methods (behaviour) how they teach at universities.

(this slide is not in the recording - I forget to add it)

To ilustrate it look at this example. Not every class with data and methods has meaning. What is meaning of this class? I can't see any meaning, regardles of the methods. What is the meaning of the User? Its so wide ranged that there is no one clear meaning. In the Email example the meaning was to say that this string is in some format. That was all.

How to improve meaning? What about a AnonymousUser with method register. The meaning of anonymous user is to register. Or ProductBuyer with method putInShoppingCart - the meaning is much clearer the smaller it is (sounds like SRP, right?).

Back to the users manager example, with classes design and OOP in mind. Does it scale? Much better, it allows to switch between user storage in DB or micrsoervice with a single line. When there is a new type of user there is no change to those lines.

Fault tolerance depends on all the parts of the code of course. For example in case of microservice we can just wrap it in Hystrix in and we have some fault tolerance in place It wraps the integer (data) in some context. Not perfect but it has some OOP properties.

And I also wanted to ilustrate that if you design your service in a tell dont ask way it allows you to switch between architectures very easily. Microservices became an implementation detail. That is the fire and forget analogy of releasing the hormones from the beginng.

From what I have shown it seems that the FP way is much better. Might be. And functional code often seems better. I like FP maybe more then Java style.

Even Alan Kay said that Lisp is the best language ever made.

But let me answer by completely non-IT analogy

Lot of studies suggests that eating vegetarian diet prolongs your life and has other health benefits.

But when you look at why is that here are the reasons. Vegetarians are mostly active people who care about their body, about quality of their food and wellbeing.

Its not vegetable. Its their lifestyle.

Being a vegetarian does not stop you from eating cakes, drinking coke and watch netflix all day. But once you decide to change your diet drastically, you start getting curious about your diet quality, quantity and overall about your health.

And thats the same phenomenon happening in FP vs Class/Java/OOP world. The reason why FP seems better most of the time and why FP code looks better is, and I am speaking solely from my experience here, is that FP programmers usually care more about the code and its design. They tend to be the craftsmen among the programmers. I have seen many meetups about functional programming - but none about class oriented. When talking to functional programmer they tend to be more excited about things then Java programmers. Most Java meetups are about new version of hibernate and how to use this new parameter of garbage collector. FP languages are usually slightly better then Java/C#/C++ but we can do same things in our languages.

We just need to care more about the code, its maintainability, its design and usability. To become craftsmen with tools we have.