Data-Oriented programming

The secret ๐Ÿ sauce ๐Ÿ that makes Clojure systems less complex




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)

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

๐Ÿ’Ž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

  • Loose coupling between components

  • 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

โณ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