Slide 1

Slide 1 text

Alexandre de Oliveira (@kurko) Forget about classes, welcome objects

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Perspective

Slide 5

Slide 5 text

Alan Kay

Slide 6

Slide 6 text

“Point of view is worth 80 IQ points” - Alan Kay

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

VI

Slide 9

Slide 9 text

6

Slide 10

Slide 10 text

Reality as we conceive it

Slide 11

Slide 11 text

Source: http://2.bp.blogspot.com/_EussjcZFO-c/SW1ITAQSlRI/AAAAAAAAAZY/Zmn7X88qBHk/s400/Princess+Anne+says+hello.jpg

Slide 12

Slide 12 text

OOP?

Slide 13

Slide 13 text

“OOP to me means only messaging, local retention and protection and hiding of state” Alan Kay

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Complexity http://dougbelshaw.com/presentations/2012/ILTA/assets/complexity.jpg

Slide 18

Slide 18 text

Problem #1: disorganisation

Slide 19

Slide 19 text

ONE BIG OBJECT™

Slide 20

Slide 20 text

many small objects collaborating with each other

Slide 21

Slide 21 text

http://20px.com/wp-content/uploads/2013/02/unicorn_pooping_a_rainbow_20px.jpg

Slide 22

Slide 22 text

such complexity, much wow

Slide 23

Slide 23 text

http://adeviantman.deviantart.com/art/Pink-Fluffy-Unicorns-Dancing-On-Rainbows-315632047

Slide 24

Slide 24 text

Problem #2: procedural code

Slide 25

Slide 25 text

Domain Organization Project Stories Members Importing projects from Pivotal Tracker

Slide 26

Slide 26 text

class ImportationService ! ! ! ! ! ! ! ! ! ! end

Slide 27

Slide 27 text

class ImportationService def process orgs = Organizations.new(client).import end end

Slide 28

Slide 28 text

class ImportationService def process orgs = Organizations.new(client).import orgs.each do |org| projects = Projects.new(org).import end ! ! ! end end

Slide 29

Slide 29 text

class ImportationService def process orgs = Organizations.new(client).import orgs.each do |org| projects = Projects.new(org).import end projects.each do |project| Members.new(client).import end end end

Slide 30

Slide 30 text

class ImportationService def process orgs = Organizations.new(client).import orgs.each do |org| projects = Projects.new(org).import end projects.each do |project| Members.new(client).import Stories.new(client).import end end end

Slide 31

Slide 31 text

class ImportationService def process orgs = Organizations.new(client).import orgs.each do |org| projects = Projects.new(org).import end projects.each do |project| Members.new(client).import Stories.new(client).import unless client.github? end end end

Slide 32

Slide 32 text

class ImportationService def process orgs = Organizations.new(client).import orgs.each do |org| projects = Projects.new(org).import end projects.each do |project| Members.new(client).import Stories.new(client).import unless client.github? end end end Beginning of the end

Slide 33

Slide 33 text

def process_extension!(extension_price, options={}) options.reverse_merge!({ :credit_card => user.default_credit_card, :billing_address => options[:credit_card].try(:billing_address) || user.default_address, :store => order.store, :tax => tax, :days => 14, :buyout => false }) ! creator = options[:creator] return process_extension_without_charge!(options) if options[:no_charge] ! response = Payment::Gateway.charge!( extension_price.cents.to_f/100, options[:credit_card].ext_cc_id, self.user.id, options[:store].try(:address).try(:country) ) ! # payment failed unless response[:ext_transaction_id].present? self.order.order_responses.create(:message => response[:error]) self.errors.add(:base, response[:error]) return false end ! data = { :ext_transaction_id => response[:ext_transaction_id], :parent_order_id => self.order.id, :parent_order_item_id => self.id, :ccauth_reply_authorization_code => response[:ccauth_reply_authorization_code] } ! if options[:buyout] # it's a buyout OrderTransaction.buyout!( self.order.currency, extension_price.cents - options[:tax].cents, self, data, creator ) ! OrderTransaction.tax_charged!( self.order.currency, options[:tax].cents, self, data, creator ) ! process_convert_to_purchase! ! else # it's a regular extension begin OrderTransaction.extension!( "#{options[:days]} days", self.order.currency, extension_price.cents - options[:tax].cents, self, data, creator ) OrderTransaction.tax_charged!( self.order.currency, options[:tax].cents, self, data, creator ) # Increment extension days for current Item self.increment(:extension_days, options[:days]) self.update_attribute(:state, 'shipped_with_customer_extended') OrderItemMailer.rental_extended(self).deliver rescue Net::SMTPSyntaxError => exception Airbrake.notify( :error_class => exception.class.to_s, :error_message => "Bad Email Address Format. Order Item: #{self.id}. Email: #{self.order.user.email}", :parameters => self.attributes, :backtrace => caller) end end ! return true end 100+ LoC Real Code™

Slide 34

Slide 34 text

Flattened Domain Organization Project Stories Members

Slide 35

Slide 35 text

http://cdn.cantechletter.com/wp-content/uploads/2012/09/NASA-Mars-Rover.jpg Curiosity

Slide 36

Slide 36 text

http://cdn.cantechletter.com/wp-content/uploads/2012/09/NASA-Mars-Rover.jpg 2.5 M LoC

Slide 37

Slide 37 text

http://cdn.cantechletter.com/wp-content/uploads/2012/09/NASA-Mars-Rover.jpg Written in C

Slide 38

Slide 38 text

http://cdn.cantechletter.com/wp-content/uploads/2012/09/NASA-Mars-Rover.jpg Completely OOP

Slide 39

Slide 39 text

WAT http://www.gazetadopovo.com.br/blogs/cinema-em-casa/wp-content/uploads/sites/30/2013/05/short2circuit2654.jpg

Slide 40

Slide 40 text

http://www.wired.com/wp-content/uploads/blogs/design/wp-content/uploads/2012/08/MSL-Rover-NX-CAD-model.jpg

Slide 41

Slide 41 text

Path Finder Radio receiver Sample Analysis Descent Imager ChemCam UV Sensor Engine Wheels

Slide 42

Slide 42 text

Component 1 Component 2 Component 3 Component n Message Queue Component 4 Component 5 Component 6 Component z

Slide 43

Slide 43 text

http://cdn.cantechletter.com/wp-content/uploads/2012/09/NASA-Mars-Rover.jpg • Lots of messages • No mutexes, no transactional memory • Complete isolation between modules

Slide 44

Slide 44 text

OOP is not about Objects http://3.bp.blogspot.com/-MwiEp-63Qro/TrQvxNtQbBI/AAAAAAAABNc/6c8bAI0kdAM/s1600/facteur.jpg

Slide 45

Slide 45 text

It’s about the Message.

Slide 46

Slide 46 text

http://cdn.cantechletter.com/wp-content/uploads/2012/09/NASA-Mars-Rover.jpg Objects: what the system is. Messages: what the system does.

Slide 47

Slide 47 text

http://x4.fjcdn.com/comments/Oh+_a70c957887dd1e4f713fb14db1dc582a.jpg Likely built with OOP

Slide 48

Slide 48 text

http://cdn.cantechletter.com/wp-content/uploads/2012/09/NASA-Mars-Rover.jpg NASA basically rewrote Erlang

Slide 49

Slide 49 text

http://cdn.cantechletter.com/wp-content/uploads/2012/09/NASA-Mars-Rover.jpg "Erlang might be the only object oriented language” - Joe Armstrong

Slide 50

Slide 50 text

Person calls Pencil#write 1/2 Developer’s point of view Person Pencil Pen

Slide 51

Slide 51 text

2/2 Object’s point of view someone someone else pay() pay() someone else

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Objects = 2 Complexity = 1

Slide 55

Slide 55 text

Objects = 3 Complexity = 3

Slide 56

Slide 56 text

Objects = 6 Complexity = ?

Slide 57

Slide 57 text

Objects = 6 Complexity = 15

Slide 58

Slide 58 text

How to stop complexity

Slide 59

Slide 59 text

How to stop wasting time complexity

Slide 60

Slide 60 text

How to stop wasting money wasting time complexity

Slide 61

Slide 61 text

“The key to scalability lies in how messaging is actually done. The key to abstraction and compactness lies in a felicitous combination of design and mathematics." ! - Alan Kay

Slide 62

Slide 62 text

Constructal Law

Slide 63

Slide 63 text

http://upload.wikimedia.org/wikipedia/commons/f/f6/Bright_green_tree_-_Waikato.jpg

Slide 64

Slide 64 text

http://hqwallpapersplus.com/wp-content/uploads/2013/09/Snowflake-Pictures-11.jpg

Slide 65

Slide 65 text

http://readywisconsin.wi.gov/lightning/images/Lightning_1.jpg

Slide 66

Slide 66 text

http://marjorymejia.com/wp-content/uploads/2012/10/800px-Mouths_of_amazon_geocover_1990.png

Slide 67

Slide 67 text

http://lh3.ggpht.com/-MgYN_UYAeCQ/S3xhh3CvNlI/AAAAAAAAAIU/j9rFK8ew7cE/900898-Resin%2520cast%2520human%2520lungs%2520and%2520bronchial%2520tree.jpg

Slide 68

Slide 68 text

http://www.spatialcomplexity.info/files/2012/12/morphology-map.png

Slide 69

Slide 69 text

Objects = 6 Complexity = 15

Slide 70

Slide 70 text

Objects = 7 Complexity = 8

Slide 71

Slide 71 text

C O Before After Architecture comparison

Slide 72

Slide 72 text

Example Module Module Module Computer Computer Computer

Slide 73

Slide 73 text

Danger, Will Robinson! Módulo Módulo Módulo A B

Slide 74

Slide 74 text

Effects ! 1. decreased complexity 2. simpler code 3. lower cognitive load 4. less wasted $$$

Slide 75

Slide 75 text

class ImportationService def process orgs = Organizations.new(client).import orgs.each do |org| projects = Projects.new(org).import end projects.each do |project| Members.new(client).import Stories.new(client).import end end end Before

Slide 76

Slide 76 text

module RemoteService class Organizations def initialize(remote_client) @client = remote_client end def import import_organizations Projects.new(@client).import end ! private ! def import_organizations organizations = @client.organizations OrganizationsTable.new(organizations).save end end end After

Slide 77

Slide 77 text

module RemoteService class Projects def initialize(remote_client) @client = remote_client end def import import_projects Members.new(@client).import Stories.new(@client).import end ! private ! def import_projects projects = @client.projects ProjectsTable.new(projects).save end end end

Slide 78

Slide 78 text

module RemoteService class Projects def initialize(remote_client) @client = remote_client end def import import_projects Members.new(@client).import Stories.new(@client).import unless @client.github? end ! private ! def import_projects projects = @client.projects ProjectsTable.new(projects).save end end end What now?

Slide 79

Slide 79 text

module PivotalTracker class Client def organizations HTTParty.get(‘url’).map { |i| Response::Organization.new(i) } end ! def projects HTTParty.get(‘url2’).map { |i| Response::Project.new(i) } end ! def members HTTParty.get(‘url3’).map { |i| Response::Member.new(i) } end ! def stories HTTParty.get(‘url4’).map { |i| Response::Story.new(i) } end end end

Slide 80

Slide 80 text

module Github class Client def organizations HTTParty.get(‘url’).map { |i| Response::Organization.new(i) } end ! ! ! ! ! ! ! ! ! ! ! ! end end

Slide 81

Slide 81 text

module Github class Client def organizations HTTParty.get(‘url’).map { |i| Response::Organization.new(i) } end ! def projects HTTParty.get(‘url2’).map { |i| Response::Project.new(i) } end ! ! ! ! ! ! ! ! end end

Slide 82

Slide 82 text

module Github class Client def organizations HTTParty.get(‘url’).map { |i| Response::Organization.new(i) } end ! def projects HTTParty.get(‘url2’).map { |i| Response::Project.new(i) } end ! def members HTTParty.get(‘url3’).map { |i| Response::Member.new(i) } end ! ! ! ! end end

Slide 83

Slide 83 text

module Github class Client def organizations HTTParty.get(‘url’).map { |i| Response::Organization.new(i) } end ! def projects HTTParty.get(‘url2’).map { |i| Response::Project.new(i) } end ! def members HTTParty.get(‘url3’).map { |i| Response::Member.new(i) } end ! def stories [] end end end

Slide 84

Slide 84 text

Effects ! 1. flexible code 2. less changes 3. Open/Closed principle 4. less wasted $$$

Slide 85

Slide 85 text

What about debugging?

Slide 86

Slide 86 text

What about debugging? Module Module Module Computer Computer Computer

Slide 87

Slide 87 text

Binary Search Module Module Module Computer Computer Computer

Slide 88

Slide 88 text

Binary Search Module Module Computer Computer Computer has the bug

Slide 89

Slide 89 text

Instrumentation Module Computer Tests

Slide 90

Slide 90 text

Write great code

Slide 91

Slide 91 text

Embrace OO

Slide 92

Slide 92 text

“It (OOP) can be done in Smalltalk and in LISP” Alan Kay

Slide 93

Slide 93 text

Thank you!

Slide 94

Slide 94 text

Resources • http://computinged.wordpress.com/2010/09/15/alan-kay-on-motis-objects-ever-cacm-article/ • http://jlouisramblings.blogspot.com/2012/08/getting-25-megalines-of-code-to-behave.html