We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies.

We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies. Less

We use cookies and other tracking technologies... More

Login or register
to apply for this job!

Login or register
to save this job!

Login or register to start contributing with an article!

Login or register
to see more jobs from this company!

Login or register
to boost this post!

Show some love to the author of this blog by giving their post some rocket fuel 🚀.

Login or register to search for your ideal job!

Login or register to start working on this issue!

Engineers who find a new job through Python Works average a 15% increase in salary 🚀

Blog hero image

The essence of Data Oriented Programming

Yehonathan Sharvit 16 August, 2020 | 3 min read

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     |
+---------------+------------------+----------------+
Author's avatar
Yehonathan Sharvit
Believe in Elegance and Simplicity. Loves Clojure.
    Clojure
    JavaScript
    ruby
    Shell
    ClojureScript
    python
    java
    big data

Related Articles

Related Issues

viebel / klipse-clj
viebel / klipse-clj
  • Open
  • 0
  • 0
  • Intermediate
  • Clojure
viebel / klipse
  • Open
  • 0
  • 0
  • Intermediate
  • Clojure
viebel / klipse
  • 1
  • 0
  • Intermediate
  • Clojure
viebel / klipse
  • Started
  • 0
  • 1
  • Intermediate
  • Clojure
  • $80
viebel / klipse
  • Open
  • 0
  • 0
  • Advanced
  • Clojure
  • $80
viebel / klipse
  • Started
  • 0
  • 2
  • Advanced
  • Clojure
  • $180
viebel / klipse
  • Started
  • 0
  • 1
  • Intermediate
  • Clojure
viebel / klipse
  • 1
  • 1
  • Advanced
  • Clojure
  • $300

Get hired!

Sign up now and apply for roles at companies that interest you.

Engineers who find a new job through Python Works average a 15% increase in salary.

Start with GitHubStart with Stack OverflowStart with Email