javascriptparadigmsdata-oriented-design

Does "Data Oriented Programming" just mean "using a global object" in javascript terms?


Ola,

I am trying to understand how DOP would work through a JavaScript dominated lens. DOP seems to only offer a handful of examples, most of which are written in OOP based languages. My lack of experience with OOP makes it hard for me to translate these examples to something I understand.

After a bit of searching, i found this javascript example: https://blog.klipse.tech/dop/2021/04/01/dop-challenges.html

In particular, the first example stands out to me:

var libraryData = {
  "name": "The smallest library on earth",
  "address": "Here and now",
  "catalog": {
    "booksByIsbn": {
      "978-1779501127": {
        "isbn": "978-1779501127",
        "title": "Watchmen",
        "publicationYear": 1987,
        "authorIds": ["alan-moore",
                      "dave-gibbons"],
        "bookItems": [
          {
            "id": "book-item-1",
            "rackId": "rack-17",
          },
          {
            "id": "book-item-2",
            "rackId": "rack-17",
          }
        ]
      }
    },
    "authorsById": {
      "alan-moore": {
        "name": "Alan Moore",
        "bookIsbns": ["978-1779501127"]
      },
      "dave-gibbons": {
        "name": "Dave Gibbons",
        "bookIsbns": ["978-1779501127"]
      }
    }
  },
  "userManagement": {
    "librarians": {
      "franck@gmail.com" : {
        "email": "franck@gmail.com",
        "encryptedPassword": "bXlwYXNzd29yZA=="
      }
    },
    "members": {
      "samantha@gmail.com": {
        "email": "samantha@gmail.com",
        "encryptedPassword": "c2VjcmV0",
        "isBlocked": false,
      }
    }
  }
};

From the examples on the site, it seems like you create one global object with the initial state of your program. Then you use functions to read and write to this global object, similar to a database.

Is this the right way to think of DOP? Isn't using global objects generally considered a bad practice?

All this is very confusing to me, thanks for any anwsers!


Solution

  • The short answer is "no".

    Does "Data Oriented Programming" just mean "using a global object" in JavaScript terms?

    No, it means that each function takes the complete object and accesses nested data within it.

    I am trying to understand how DOP would work through a JavaScript dominated lens. DOP seems to only offer a handful of examples, most of which are written in OOP based languages. My lack of experience with OOP makes it hard for me to translate these examples to something I understand.

    The languages used in the Examples are not OOP-specific and they are written in a static nature. There are no objects, classes, methods or instance variables in any of the examples. The languages for the challenges include C#, Clojure, Cue, Elixir, Elm, Go, JS, Python, Ruby and TS. Most of these languages are multi-paradigm.

    See: https://github.com/viebel/data-oriented-programming/tree/main/challenges

    tl;dr; There is no OOP present in the examples. The three principles of OOP are inheritance, polymorphism, and encapsulation. None of these are utilized in a static; functional approach.

    From the examples on the site, it seems like you create one global object with the initial state of your program. Then you use functions to read and write to this global object, similar to a database.

    Yes, you use one centralized data object, but it is only global for the demonstration. This data could be stored as a JSON file on a server that gets loaded when the application spins up. Or the data could be in a MongoDB database where the queries can access the data instead of the application itself.

    Is this the right way to think of DOP? Isn't using global objects generally considered a bad practice?

    Global objects are only bad if you are mutating the data. If the data is immutable (constant), here should be no problem.

    Take a look at Brian Goetz's (Java Language Architect) article on "Data Oriented Programming in Java". It includes more insight on OOP vs DOP. The key takeaway here is that Goetz notes that DOP "keep[s] the code that embodies the business logic of how we act on that data separate."

    Real-world example

    Representational state transfer (REST) is an example of data-driven design. For example, the {JSON} Placeholder website provides a basic REST interface for GET, POST, PUT, PATCH, and DELETE actions. This website utilizes a very large JSON "database" of immutable; queryable data.

    Just because we are using an "object" to store/model the data, does not mean it's OOP. This just happens to be the format the data is stored as.

    See also

    The following article shows the difference between control vs data -oriented design: