We are not Object Oriented anymore

by @matteocollina

Back to School

Back to School

Back to School

What is an Object?

Objects provides

Objects provides Encapsulation

function myFunc (obj) {
  return function () {
    return obj.doSomethig()
  }
}

Objects provides Accessors

Objects provides Accessors

class Person {
  constructor (name) { this._name = name }
  get name () { return this._name }
  set name (val) { this._name = val }
}

Are Accessor useful?

Objects provides Inheritance

class Person { }
class Student extends Person {}

Objects provides Inheritance

Objects are GOOD

Are classes good?

Are models good?

class Person {
  save (cb) {
    db.save(cb)
  }
}

Are fat models good?

Solution: Let's encapsulate the model!

Solution: Build internal APIs!

...but I only wanted to access my data!

 Writing code for the sole purpose of deleting it  - Greg Young

 All code is sh*t   - Matteo Collina

What if we design messages, and not models?




 The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be. - Alan Key, father of OOP

What is a message?

The anatomy of a message

{
  person:  {
    name: 'Matteo'
    surname: 'Collina'
  }
}

The anatomy of a recipient

recipient(message, function (err, result) {
  console.log(err, result)
})

Node Callback Style

Node Callback Style

func(msg, function myFunc(err, arg1, arg2, ...) {
  /*
  this is called asynchronously
  err contains an Error object
  */
})

Node Callback Style

How to remote a function call?

Final message

{
  role: 'person',
  cmd: 'save'
  person:  {
    name: 'Matteo'
    surname: 'Collina'
  }
}

Command pattern!

Command Pattern!

Pattern Matching!

var i = bloomrun()
i.add({ cmd: 'save' }, function save (arg, cb) {
  alert('saving ' + JSON.stringify(arg))
  cb(null, true) })
  
var msg = {
  cmd: 'save',
  person: { name: 'matteo' } }
i.lookup(msg)(msg, function (err, result) {
  alert([err, result].join(' ')) })
  

We call such a way of composing code a

Microservice

http://senecajs.org

var seneca = require('seneca')()
seneca.add({
  role:'user',
  cmd:'login'
}, function (args, callback) {
  var loggedIn = args.username === 'matteo' &&
                 args.password === 'collina'
  callback(null, { loggedIn:loggedIn })
})
seneca.listen()
var seneca = require('seneca')()
var client = seneca.client()

client.act({
  role:'user',
  cmd:'login',
  username: 'matteo',
  password: 'collina'
}, function (err, result) {
  console.log(result.loggedIn)
})


Features

DEMO!

This presentation

http://github.com/mcollina

Thanks!


If you need help with Node.js


matteo.collina@nearform.com

@matteocollina on Twitter

www.nearform.com