Data-Oriented programming

Reducing ๐Ÿ•ธ complexity ๐Ÿ•ธ of information systems




Yehonathan Sharvit viebel

๐Ÿ‘‹Who am I?

  • ๐Ÿ’ป Developer since 2001 (Clojure since 2012)

  • ๐ŸŽ๏ธMaintainer of Klipse

  • ๐Ÿ“– Author of Data-Oriented programming

  • ๐Ÿ“ Blogger at blog.klipse.tech

  • ๐Ÿง™ Clojure wizard at Cycognito

dop book

๐Ÿ•ธ What is complexity?



โš™๏ธComputational complexity

The amount of machine resources (e.g. CPU, memory) required to run a program.

๐Ÿ˜ฐ System Complexity

The amount of brain resources required to understand a system.


Our goal is to reduce system complexity.

๐Ÿ“Š Information systems

Systems that manipulate information in various ways.




Examples of information systems:

  • Web services that fetch data from the database and serves it as JSON

  • Web workers that listen to events and enrich them with data from multiple data sources

  • Front end applications that manage an application state

โŒ› A brief history of Data-Oriented programming

1936 ๐Ÿ‘‰ ฮป-calculus (Alonzo Church)

1958 ๐Ÿ‘‰ LISP (John McCarthy)

1981 ๐Ÿ‘‰ Values and Objects in Programming Languages (Bruce MacLennan)

2000 ๐Ÿ‘‰ Ideal Hash trees (Phil Bagwell)

2006 ๐Ÿ‘‰ Out of the Tar Pit (Ben Moseley and Peter Marks)

2007 ๐Ÿ‘‰ Clojure (Rich Hickey)

2021? ๐Ÿ‘‰ Wikipedia article about Data-Oriented programming (Help wanted)

zen

๐Ÿ’ก Principles of Data-Oriented programming

Data is a first-class citizen โœˆ๏ธ



  1. Separate code from data

  2. Represent data with generic data structures

  3. Do not mutate data


  • Language agnostic

    • OO and FP

    • Statically or dynamically typed

  • Applicable in isolation

  • Powerful when combined

๐Ÿค” What makes a software system complex?

watchmen library


complex class relation

๐Ÿ•ธ๏ธSources of complexity

  • Nodes with many edges

  • Many kinds of arrows

    • Association

    • Composition

    • Inheritance

    • Usage

๐Ÿ’ก Principle #1: Separate Code from Data

principle1 nutshell v2
Adhering to the principle in OOP ๐Ÿ‘
class AuthorData {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}
class NameCalculation {
  static fullName(data) {
    return data.firstName + " "
           + data.lastName;
  }
}
Breaking the principle in FP ๐Ÿ‘Ž
function createAuthor(firstName,
                      lastName) {
  return {
    fullName: function() {
      return firstName + " " + lastName;
    }
  };
}

๐Ÿ…Reduction of System Complexity

from oop to dop

โ˜•What makes this system less complex?

  • Separation of concerns

  • Code diagram constraints

    • Stateless (static)

    • Only usage relation

  • Data diagram constraints

    • Only association and composition

๐Ÿ’กPrinciple #2: Represent Data with generic data structures

In DOP, we represent a record as a heterogeneous map



Adhering to the principle ๐Ÿ‘
var watchmenBook = new Map([
    ["publicationYear", 1986],
    ["title", "Watchmen"]
    ]);
Breaking the principle ๐Ÿ‘Ž
class Book {
    isbn; title;
    constructor(publicationYear, title) {
        this.publicationYear = publicationYear;
        this.title = title;
    }
}
var watchmenBook = new Book("978-1779501127",
                            "Watchmen");

๐Ÿ…Reduction of System Complexity even more

data code relation

๐Ÿ‘Ž ๐Ÿ‘Ž ๐Ÿ‘Ž

code only

๐Ÿ‘ ๐Ÿ‘ ๐Ÿ‘

โ˜•What makes this system less complex?

  • Weak dependency between code and data

  • Loose coupling between components

  • Flexible data model

  • Generic data manipulation functions

๐Ÿ… Data is represented as data

zen4

๐Ÿ… Data is represented as data

library data
  • Visualize the whole system data

  • Display data on console

  • Serialization for free

  • Reflection for free

  • Information path (e.g. ["catalog", "authorsById", "alan-moore", "name"])

๐Ÿ’ก Principle #3: Do not mutate Data

structural sharing

Update the value associated with
["catalog", "booksByIsbn", "watchmen"]
to 1986.

Most of the nodes are shared!

Immutability in practice

  • Immutability in Java

    • @value annotation from Project Lombok

    • Records (since Java 14)

  • Immutability in JavaScript

    • Naive structural sharing with: Lodash, Ramda, Immer

    • Advanced structural sharing with: Immutable.js

    • EcmaScript Proposal for record and tuple

โณSummary





do principles journey
  1. Separate Code from Data

  2. Represent Data with generic data structures

  3. Do not mutate data

๐ŸŽนImagine

๐Ÿ™‹ Questions

dop book