Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Message Oriented Programming

Message Oriented Programming

In this talk we will examine the fundamental pieces to what I am calling message oriented programming. We will look at the request/response message pattern, we will look at the structure of messages themselves, and we will look at how protocols can be created to enforce a sane message passing system.

Avatar for Brian Knapp

Brian Knapp

March 10, 2015
Tweet

Other Decks in Programming

Transcript

  1. I build things and do stuff. Obvious Architecture Ruby Web

    Benchmark Report I obsess about things that most people don’t worry about http://brianknapp.me/ @_brianknapp 3
  2. Bad Input! It’s all around us SQL injection, XSS, buffer

    overflows, nil, etc. Even in the database! 14
  3. Bad input -> Bad output -> Bad input -> Bad

    output -> Bad input -> Bad output -> Bad input -> Bad output -> Bad input -> Bad output -> Bad input -> Bad output -> Bad input -> Bad output -> Bad input -> Bad output -> Bad input -> Bad output -> 16
  4. -Alan Kay “I’m sorry that I long ago coined the

    term “objects” for this topic because it gets many people to focus on the lesser idea. The big idea is “messaging” - that is what the kernal of Smalltalk/Squeak is all about (and it’s something that was never quite completed in our Xerox PARC phase).” 26
  5. 28

  6. - Alan Kay To me, one of the nice things

    about the semantics of real objects is that they are “real computers all the way down (RCATWD)” – this always retains the full ability to represent anything. The old way quickly gets to two things that aren’t computers – data and procedures – and all of a sudden the ability to defer optimizations and particular decisions in favour of behaviours has been lost. In other words, always having real objects always retains the ability to simulate anything you want, and to send it around the planet. 33
  7. - Alan Kay And RCATWD also provides perfect protection in

    both directions. We can see this in the hardware model of the Internet (possibly the only real object-oriented system in working order). You get language extensibility almost for free by simply agreeing on conventions for the message forms. 34
  8. What is a message? Container used for the transportation of

    information between two (or more) things Messages can have many roles, formats, etc. Facilitate communication! Imply protocols 42
  9. What is a protocol? A standard mechanism & structure for

    transmitting messages Protocols are all around us 44
  10. Metcalfe's law Metcalfe's law states that the value of a

    telecommunications network is proportional to the square of the number of connected users of the system (n2). 53 Source: http://en.wikipedia.org/wiki/Metcalfe%27s_law
  11. 56

  12. Is it a well written piece of code? Is it

    clean code? Do you understand it? What is the protocol? 62
  13. The function has a poorly defined protocol There are a

    lot of open questions It is fragile Doesn’t account for edge cases or errors 76
  14. How would you fix it? A better specification up front?

    Add documentation? Type checking? TDD? Error handling? 80
  15. class MyMath ! prepend MyMathProtocol ! # Does basic division

    # # @param numerator [Numeric] the numerator value # @param denominator [Numeric] the denominator value # # @return { result: Numeric, # error: String } # # @see MyMathProtocol#divide def divide(numerator:, denominator:) result = numerator / denominator { result: result, error: nil } end ! end A Better Solution? 81
  16. module MyMathProtocol ! ! # This enforces that the {MyMath#divide}

    satisfies the message format # # @param numerator [Numeric] the numerator value # @param denominator [Numeric] the denominator value # # @return { result: Numeric, # error: String } # # @return { error: "bad numerator type”, result: nil } if numerator is nil or not Numeric # @return { error: "bad denominator type”, result: nil } if denominator is nil or not Numeric # @return { error: "bad denominator value, can't be zero”, result: nil } if denominator is nil or not Numeric # # @return <output error hash> if error returned from MyMath#divide # @return { error: "bad return format”, result: nil } if output format isnt correct # @return { error: "bad result type”, result: nil } if output result isn't a Number # # @see MyMath#divide def divide(numerator:, denominator:) return { error: "bad numerator type”, result: nil } if numerator.nil? || !(numerator.is_a?(Numeric)) return { error: "bad denominator type”, result: nil } if denominator.nil? || !(denominator.is_a?(Numeric)) return { error: "bad denominator value, can't be zero”, result: nil } if denominator == 0 ! ! output = super(numerator: numerator, denominator: denominator) ! ! return { error: "bad return format”, result: nil } if ( !output.is_a? Hash || !output.has_key?(:error) || !output.has_key?(:result) ) ! return output if output[:error] return { error: "bad result type”, result: nil } if !output[:result].is_a? Numeric ! return output end ! end 82
  17. You have already learned this lesson elsewhere! You speak English

    or some common language You use protocols constantly - HTTP, Twitter, LOL, USB, Rails & Ruby Ever built a JSON API? 90
  18. How do we start? Think about the rules that govern

    our existing communication systems Think about the why’s of good and bad input Learn some lessons from Erlang & Actor model Play with Module #prepend Start designing protocols into system components 92
  19. Microservices create physical boundaries and protocols in the absence of

    them existing in a traditional monolithic web app 94
  20. A look at where we are going More networked systems

    Increased complexity Hopefully more discussion, better tooling, better vocabulary, MOP languages Higher expectation of reliability from users Ruby is a great place to start! 96
  21. We spend so much time thinking about logic, we don’t

    take enough time to think about communication. 97
  22. Image Credits Broken Window: http://commons.wikimedia.org/ wiki/File %3AWTF_Fred_Oostryck_Broken_window_of_Wool stores.jpg My Family:

    Copyright ME, please don’t use this photo Organization Photo: http://www.gettyimages.com/ gi-resources/ub/unfinishedbusiness/index.html Furby In Microwave: http:// pinkfurbyadventures.tumblr.com/post/ 28347444631 Dr Evil: http://www.quickmeme.com/meme/35ze3t 99 Minesweeper: http://commons.wikimedia.org/wiki/ File:Full_Flagged_Minesweeper.PNG Alan Kay: http://commons.wikimedia.org/wiki/ File:Alan_Kay_%283097597186%29.jpg Internet Meme: http://knowyourmeme.com/memes/ first-world-problems All other photos are Public Domain and available on Wikimedia Commons