Data-Oriented programming

The secret 🍝 sauce 🍝 that makes Clojure systems less complex

ldnclj

February 15, 2021

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

dop book

📓 Definitions

🍝 Secret sauce

An element, quality, ability, or practice that makes something or someone successful or distinctive.

Merriam-Webster dictionary

😰Complexity

What makes large systems hard to understand.

Out of the Tar Pit (2006)

🤔 What makes Clojure distinctive?





clojure essence

⌛ A brief history of Data-Oriented programming


1936 👉 λ-calculus (Alonzo Church)

1958 👉 LISP (John McCarthy)

2000 👉 Ideal Hash trees (Phil Bagwell)

2007 👉 Clojure (Rich Hickey)

2006 👉 Out of the Tar Pit (Ben Moseley and Peter Marks)

2021? 👉 Wikipedia article about Data-Oriented programming (Help wanted)

zen

💎The main ideas of LISP

Data-Oriented

Computing with symbolic expressions
Representation of information by lists

Functional Programming

Composition of functions
λ-expressions for naming functions

Homoiconicity

Representation of LISP programs as LISP data

The REPL?

The function eval

History of Lisp, John McCarthy 1979

💡 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

📚Design a library management system

watchmen library


complex class relation

🕸️What makes this system complex?

  • Nodes with many edges

  • Many kind of arrows

    • Association

    • Composition

    • Inheritance

    • Usage

💡 Principle #1: Separate Code from Data

principle1 nutshell
Breaking the principle in FP 👎
function createAuthor(firstName,
                      lastName) {
  return {
    fullName: function() {
      return firstName + " " + lastName;
    }
  };
}
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;
  }
}

🏅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




data building blocks

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

🏅Reduction of System Complexity even more

data code relation

👎 👎 👎

code only

👍 👍 👍

☕What makes this system less complex?

  • Weak dependency between code and data

  • Flexible data model

  • Generic data manipulation functions

🏅 Visualize the system data

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

  • Display data on console

  • Serialization for free

  • Reflection for free

💡 Principle #3: Do not mutate Data






411lb0

Mutation is bad!

👨‍💻Application of DO principles






application do

‍🤪Application of DO principles in Java

What we need:

  1. Efficient persistent data structures

  2. Atoms

  3. Parse JSON into a map

  4. Manipulate "untyped" primitives

  5. Database driver that works with maps

⏳Summary





do principles journey
  1. Separate Code from Data

  2. Represent Data with generic data structures

  3. Do not mutate data

🎹Imagine

🤝Collaborate

dop book

🤑 50% discount: mlsharvit2

yweme6kn&chs=400x400

Wikipedia article: Data-Oriented programming

#data-oriented-programming on Clojurians