We're planting a tree for every job application! Click here to learn more

The essence of Data Oriented Programming

Yehonathan Sharvit

18 Aug 2020

•

3 min read

The essence of Data Oriented Programming
  • Scala

After many years of suffering in C++ and Java, many years of enlightenment in Clojure, I have realised that what brings joy to me when I code in Clojure is the fact that Clojure is a Data Oriented programming language.

Over the last couple of months, I have discussed with some Clojure friends about the essence of Data Oriented (DO) Programming.

Of course, one cannot really grasp the essence of DO. DO is as profound as the TAO from Tao Te Ching, whose first sentence is:

Tao that can be spoken of is not the Tao

We cannot grasp its essence but we can get closer to an understanding of DO by comparing it with a quite different programming paradigm, namely: Object Oriented (OO) Programming.

To me, there are three main differences between OO and DO:

  • In OO, code and data are mixed together while in DO they are separate
  • In OO, the data is mutable while in DO it is immutable
  • In OO, the data access is specific while in DO, it is universal

The rest of this article is devoted to a brief explanation of each difference and a simple illustration of the benefits that each difference brings to OO.

Code and data: together or separate?

In OO, the main entities of a program are objects which contain:

  • Code in the form of methods
  • Data in the form of members

In DO, we keep code and data separate:

  • Modules (or namespaces) contain code in the form of functions
  • Data in the form of generic maps and lists

The fact that code and data live in separate entities tends to make DO programs less complex than OO programs.

Let me illustrate this profound insight by showing a typical class diagram of a OO system, for instance a library management system.

complex-class-relation.png

And now, let me split each class into a code entity and a data entity. Here is the resulting class diagram:

simple-data.png

The resulting system is made of 2 separate (disjoint) simpler sub-systems. As a consequence this system is less complex than the original one.

Separating code and data reduces complexity

Data: mutable or immutable?

In OO, it's quite common to mutate an object i.e. change the value of a member.

In DO, rather that changing data in place, we create a new version of the data.

The benefits of data immutability are well known but here I'd like to illustrate a simple aspect of the importance of data immutability by asking you to think about the output of this pseudo OO code for a few seconds :

class Member {
      Bool member;

      void displayBlockedStatusTwice() {
         this.isBlocked = true;
         System.Print(this.isBlocked );
    }
}

The correct answer is: in a single threaded environment, it displays true while on a multi threaded environment it's unpredictable.

Indeed in a multi threaded environment, between the two print calls, there could be a context switch and the state of the member object could be changed.

Isn't it insane?

Data immutability brings serenity to developer's mind

Data access: specific or universal?

In OO, the data part of an object has a rigid shape (blueprint) that is defined at compile time in a class. In order to access the data inside an object, one must know the object class.

In DO, data is mostly made of maps and lists (or vectors) and some primitive types (numbers, booleans and strings).

The fact that in order to access the data of an object, one has to import the class definition of the object is in many cases very annoying. Imagine, you need to convert the data part objects into JSON in order to send the information over the wire. In OO, it is a nightmare.

For each object that you want to serialize (convert to JSON), you basically need to write specific piece of code or you need to use reflection.

While in DO, serializing a piece of data is a matter of iterating over the entries and convert them to a string. One can easily write a JSONEncode function that works with any data.

Universal data access is a privilege

Conclusion

In Data Oriented Programming, Data is considered as a value. It means that:

  • Data is immutable
  • Data shape is flexible
  • Data access is universal

In a nutshell, here are the core differences between OO and DO:

+---------------+------------------+----------------+
|               |  OBJECT ORIENTED |  DATA ORIENTED |
+---------------+------------------+----------------+
|               |                  |                |
| Code and data | Together         |  Separate      |
|               |                  |                |
| Data state    | Mutable          |  Immutable     |
|               |                  |                |
| Data access   | Specific         |  Universal     |
+---------------+------------------+----------------+
Did you like this article?

Yehonathan Sharvit

Full-Stack Web Consultant. Expert in Clojure, ClojureScript and Javascript.

See other articles by Yehonathan

Related jobs

See all

Title

The company

  • Remote

Title

The company

  • Remote

Title

The company

  • Remote

Title

The company

  • Remote

Related articles

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

•

12 Sep 2021

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

•

12 Sep 2021

WorksHub

CareersCompaniesSitemapFunctional WorksBlockchain WorksJavaScript WorksAI WorksGolang WorksJava WorksPython WorksRemote Works
hello@works-hub.com

Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ

108 E 16th Street, New York, NY 10003

Subscribe to our newsletter

Join over 111,000 others and get access to exclusive content, job opportunities and more!

© 2024 WorksHub

Privacy PolicyDeveloped by WorksHub