loading

The perfect choice of one-stop service for diversification of architecture.

(Functional) OOP with Message Passing

There is a great misunderstanding of what object-oriented programming really is and its origins. The term "object" was first coined in the 1950s and became really popular on the early 60s on Lisp circles as a powerful design pattern. Finally on the 70s the first object-oriented language, Smalltalk, was developed by Alan Kay.

The truth is that what object-oriented programming really represents has been lost over the years as modern languages kept copying the syntax and ideas imposed by the C programming language. Alan Kay has declared publicly many times that the essence of OOP is the concept of message passing, and that modern programming languages focus more on the object notation itself than in the concept of message passing. This of course is the motivation for this article and our focus here will be to show the essence of object-oriented programming using a popular modern programming language, JavaScript. I hope you enjoy this journey.

What is a message?Before going deeply into object-oriented implementation it is fundamental to go deeply into the prerequisites. Let's start with messages!So what the hell is a message? When you want to communicate two endpoints you need a standardized information to be passed along the way. A message is this standardized information and is essential to guarantee that the two endpoints will be as decoupled as possible. It is important to notice that if the producer sends a message that the consumer does not understand, nothing happens because it is not a predefined standard.Our problemSuppose I want to grab a student's best grade from a list of grades in all the exams he participated. We could write a silly function to do that without complex functionality, just vanilla JavaScript for the masses:The above code works perfectly, but since we are working with an array as a parameter, it does not sound very semantic. It is not clear that the list of grades belongs to a single student. How do we fix this? Well There are two solutions to this problem:We change the name of the function to getStudentBestGradeWe pass as a parameter the student as an object.Of course, we will focus on the second one! yay!Using the "class" keywordWell, since we are object-oriented programming lovers, we can do it with the class keyword. This is such an easy job anyway:Now the code looks much better as I can simply pass on the student as a parameter and: Bingo! Much better than the previous versionBut this is not the idea behind this article. It looks quite nice but we are using JavaScript object-oriented notation to do that. Why not do it with functions only? So keep on reading :)Our First Functional ObjectTo understand fully what we will be talking about in the next paragraphs, we need to start simple. We will first use the concept of function scope to create a functional counter with a private embedded value.Holy cow, that is a function that returns another function. Don't be scared, this is quite a normal thing to do. I know it may sound absurd for many people, but that is actually very useful. I like to tell people that this first function is a function builder because it lets you create more dynamic functions by simply injecting state into them.Ok, but what the heck is state? Think of state as anything that can cause your functions to output different values for the same input, in this case, the parameter initial does exactly that.

As we can see in the example above, for the same counters, every time we call the function we have as output a different value. So just as a review, note that the Counter function builds in the example two functions with private embedded value inside. Isn't that what private attributes on objects do? ;)Now that we understand state injection, we can complicate things a bit more and write a JavaScript-only class using functions only. Why JavaScript only? Because we will use JSON (JavaScript Object Notation).As we can see, we used the same approach as on the counters example. Our function student (that represents our class) receives the grades as a parameter and maintains it as a private attribute _grades. The public methods (getGrades, getBestGrade) of this class are returned as a JSON. Since this class is written using a fancy looking function, there is no need to use the new keyword.This implementation is very common on old JavaScript source codes but is very powerful. But as I promised before, we want to do it the right OOP way, and that means that we need to use the concept of message passing. The message passing implementation is language agnostic and should work on any decent programming language out there.I hope you love this, now is when the real fun begins! Message Passing Object NotationLet's write the same object as before but using the message passing strategy.

As we can see, this OOP implementation at first sight does not look very elegant in JavaScript, but I can guarantee it will work on most popular programming languages. The idea is not what this implementation can do differently, but how we can extend this and implement real cool hierarchy and polymorphism (We will do this after the next title).To use the message passing strategy, you have to understand that for each message, the object will have different handlers (functions) that will be specialized in dealing with the message. If we pass no parameters, then we can simply get the function and use it in a more flexible way. Below I show you how you can use the message passing strategy:An advantage of using this object and not the previously declared object, the one using JSON, is the fact that in case we send an invalid message there will be a predefined response for this request that we can model for our domain.Inheritance and PolymorphismFor this example, we need a class to extend from, and there is no other class that looks nicer than Person (I was not very creative really). Since every student is a person, this means that every student should have all attributes and methods from the base class. Below I have implemented the class Person.Since Student needs to extend the base class, we should do some nice things to make it all work out together and produce a nice looking hierarchy:Students must have a name and an age. So for now on, the constructor should also have name and ageOne of the attributes of our class will be the base class that will be constructed with the parameters sent on the Student constructorIf the message we are looking for is not found, we will redirect it to the base class on the default case of the switch.Here we go, ladies and gentlemen! :DFinally, we have a nice little class that extends Person using functions and message passing only. You can use it the same way as before, by sending messages.

ConclusionI hope you liked what you read. This article focus on the main idea behind object-oriented notation and how to use message passing. Of course this is a very humble implementation and most languages optimize a lot how classes are represented when they become bits and bytes. There are lots of other things we could implement such as method overload and multiple inheritances, but they are quite an easy task to do once we have these things implemented.

References1 OOP v.

s. FP codes/til/referential-transparency.

·RELATED QUESTION

Do decimal equivalents to binary number values hold significance in software programming?

No, there is no meaningful relationship between the decimal and binary notations of the numbers. One is base 10 and the other base 2 and ten is not a power of two. The reason we use base-10 Arabic numerals is probably because have ten fingers and because it's a much better system then Roman numerals. Nobody was thinking about binary digits at the time.However, there is a relationship to hexadecimal numbers, which are base 16. 16 is 2^4 so each digit represents four bits (binary digits). This makes going from hexadecimal to binary and vise versa a snap. For instance, if you know that the hex number A (10 decimal) is 1010, then you know right away how to decode the hex number AAAA: 1010101010101010When you're working with binary numbers and boolean operators hex is a lot more intuitive.150 OR 64 in decimal is very awkward. I can't do that in my head. But 96h OR 40h is no problem. You only have to think about a single nibble. You know that 9d is 1001 and 4 is 0100 so you know you can add 4 and 9 to get the result: 13d, or Dh. So the answer is D6. The translation from D6 into decimal (13 * 16 6214) however is awkward, so it's nice if you can avoid using decimal

(Functional) OOP with Message Passing 1

GET IN TOUCH WITH Us
recommended articles
Related Blogs blog
The future in which everybody can code will probably never happen. And being practical, even if we had a basic knowledge of some of these almost ubiquitous languages...
Although GPR has been widely used in hydrology, engineering, environment and other fields, many basic theoretical and technical problems have not been fundamentally ...
So I am thinking of a possible answer to my own question:Build my own journal note entry app linked to a wiki.Zim Wiki uses a file based system for wiki. Maybe I cou...
About 8 I think1. where can i get ncaa football 10 rosters with names?For the last two years I got mine from "Pastapadre". From what i can tell they are really prett...
The Haunting I vaguely remember some kind of eye injury in the movie.• Other Related Knowledge ofa cocktail glass— â€&...
rsync can be somewhat painful if you have a very large number of files - especially if your rsync version is lower than 3. On the other hand: if you use tar, you wou...
Blockchain Technology Explained: Powering BitcoinMicrosoft recently became the latest big name to officially associate with Bitcoin, the decentralized virtual curren...
PLEASE HELP ME CHOOSE A VIDEO CAMERA!?the Flip ultra HD is a really good HD portable camcorder, and it's fairly cheap. A lot of famous youtubers use it, such as timo...
In order to implement the tasks proposed in the outline of the national medium and long term science and technology development plan (2006-2020), the national key R ...
Alibaba group and Royal Philips of the Netherlands announced that they have officially signed an IT infrastructure service framework agreement to jointly promote the...
no data
Customer service
detect