Reduce system complexity in Java
with
Data-Oriented programming


Taming objects


sou java

April 2021

Yehonathan Sharvit viebel

๐Ÿ‘‹Who am I?

  • ๐Ÿ’ป Developer since 2001 (C++, Java, JavaScript, Ruby, Clojure)

  • ๐Ÿ“– Author of Data-Oriented programming

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

dop book

๐Ÿค” What is Data-Oriented Programming?





Set of best practices that reduce complexity of information systems by treating data as a first-class citizen

๐Ÿ““ 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.

๐Ÿ“Š 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 event data from multiple data sources

  • Front end applications that store application state

๐Ÿค” What makes an information system complex?

watchmen library


complex class relation

๐Ÿ•ธ๏ธSources of complexity

  • Nodes with many edges

  • Many kind of arrows

    • Association

    • Composition

    • Inheritance

    • Usage

๐Ÿ’ก Separate Code from Data

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

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Separate Code from Data in Java

class AuthorData {
    private String firstName;
    private String lastName;
    // setters
    // getters
}
class AuthorCode {
    static String fullName(AuthorData author) {
        return author.firstName() +
            " " + author.lastName();
    }
}
var asimov = new AuthorData("Isaac", "Asimov");
AuthorCode.fullName(asimov); // "Isaac Asimov"

๐Ÿค” What makes code hard to understand?

By reference or by value?

In Java, object references are passed by value




var asimov = new AuthorData("Isaac", "Asimov");
var asimov2 = AuthorCode.toUpperLastName(asimov);
asimov2.lastName() // "ASIMOV"
asimov.lastName() // ??
  1. โš ๏ธWe could trust the implementation

  2. ๐Ÿ”’ We protect ourselves by copying the object

  3. ๐Ÿ˜ฑ It makes the code hard to understand

๐Ÿค” What makes code hard to understand?

Thread-safety

class MemberData {
    private boolean blocked;
    private String firstName;
    private String lastName;
   // getters and setters
}
class MemberCode {
    static borrow(MemberData member, String bookId) {
        if(!member.isBlocked()) {
            System.out.println("The book is yours!");
        }
    }
}




  1. โš ๏ธThis code is not thread safe!

  2. ๐Ÿ”’ We add lock mechanisms.

  3. ๐Ÿ˜ฑ It makes the code hard to understand.

๐Ÿ’ก Do not mutate data

Treat data as a value. Values never change.




Main benefits of immutable data

  1. Inherently Thread-safe

  2. No side-effects

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Immutable data in Java

We can solve any problem in Java with Java annotations (anonymous)

@value annotation by Project Lombok

@value class AuthorData {
    String firstName;
    String lastName;
}

Auto generation of:

  • constructor, immutable private fields

  • getters, setters

  • toString(), hashCode(), equals()

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Immutable data in Java

Java records (Only since Java 14!)

public record AuthorData (String firstName,
                          String lastName) {}

Native implementation of:

  • constructor, immutable private fields

  • getters, setters

  • toString(), hashCode(), equals()

๐Ÿ… Benefits of Immutable data in Java

var asimov = new AuthorData("Isaac", "Asimov");
var asimov2 = AuthorCode.toUpperLastName(asimov);
asimov.lastName() // "asimov"
  1. No mutations!

  2. No unpleasant surprises

  3. No need to do a defensive copy!

class MemberCode {
    static borrow(MemberData member, String bookId) {
        if(!member.isBlocked()) {
            System.out.println("The book is yours!");
        }
    }
}
  1. Thread-safe!

  2. No race conditions!

  3. No need to protect with a lock!

๐ŸŽ‰ Java libraries that embrace Data-Oriented programming

  • gson: Convert Objects into JSON and back

  • cqengine: Ultra-fast SQL-like queries on collections

  • Paguro: Immutable Collections and Functional Transformations

  • bifurcan: Functional, durable data structures

๐Ÿค” What else in the book?

  • Efficient immutable collections

  • Represent record with immutable maps

  • Polymorphism without inheritance

  • Manage the application state

  • Highly-scalable concurrent systems

  • Flexible access to the database

dop book

โณSummary

dop book