Data Objects

September 26, 2019

Object oriented programming (“OOP”) gets a bad rap. Some of it is deserved but OOP does shines when you need to manage state. Check out genservers in Erlang to see the hoops you have to go through to manage state.

You can avoid some of the pitfalls of OOP if you draw hard lines between state and data. One of the ways to draw a hard line between state and data is to only create methods in a class that operate on the objects state and returns the result. I refer to these as data objects.

Here’s a quick example.

class Rectangle {
  constructor(dataJson) {
    this.width = dataJson.width;
    this.height = dataJson.width;

  area() {
      return this.width * this.height;

rectangle = new Rectangle({width: 10, height: 5})

// rectangle.area() === 50

Notice that we do not mutate state and the method returns a result. Methods in a data object must return a result based on its state and should avoid side effects. Once you start introducing side effects the object is no longer a data object, it’s something that’s more difficult to debug.

Send comments, corrections to waynechoi@gmail.com