Slide 1

Slide 1 text

The maybe monad as a replacement for nil

Slide 2

Slide 2 text

My Problem My Solution

Slide 3

Slide 3 text

Everyone Stand Up

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

a!/samphippen

Slide 6

Slide 6 text

My Problem

Slide 7

Slide 7 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 8

Slide 8 text

Type

Slide 9

Slide 9 text

Types

Slide 10

Slide 10 text

What is a type?

Slide 11

Slide 11 text

What is a data type?

Slide 12

Slide 12 text

A type is a set of possible values and operations

Slide 13

Slide 13 text

Class

Slide 14

Slide 14 text

Terms are literally interchangeable in Ruby

Slide 15

Slide 15 text

Terms are literally interchangeable in Ruby Konstantin

Slide 16

Slide 16 text

Fixnum

Slide 17

Slide 17 text

Slide 18

Slide 18 text

+

Slide 19

Slide 19 text

/

Slide 20

Slide 20 text

Slide 21

Slide 21 text

1.class # => Fixnum

Slide 22

Slide 22 text

You know what all these things do

Slide 23

Slide 23 text

1+1 # => 2

Slide 24

Slide 24 text

Array

Slide 25

Slide 25 text

count

Slide 26

Slide 26 text

each

Slide 27

Slide 27 text

In Ruby some types are interchangeable

Slide 28

Slide 28 text

Typeclass

Slide 29

Slide 29 text

A set of types and common operations

Slide 30

Slide 30 text

There is some expectation of what the operations will do

Slide 31

Slide 31 text

Duck typing

Slide 32

Slide 32 text

All number types in Ruby form a typeclass

Slide 33

Slide 33 text

Fixnum Float BigDecimal

Slide 34

Slide 34 text

Numeric Op Numeric = Numeric

Slide 35

Slide 35 text

Positive Numeric + Positive Numeric = Positive Numeric =

Slide 36

Slide 36 text

Also collections

Slide 37

Slide 37 text

Hash Set Array

Slide 38

Slide 38 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 39

Slide 39 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 40

Slide 40 text

Type Checking

Slide 41

Slide 41 text

This term has two meanings

Slide 42

Slide 42 text

Compile time type checking

Slide 43

Slide 43 text

public static final List seriouslyiamsoboredwh ocares

Slide 44

Slide 44 text

Like in Java

Slide 45

Slide 45 text

Clearly we don’t do this in Ruby

Slide 46

Slide 46 text

So what do I mean?

Slide 47

Slide 47 text

ActiveRecord::Base #find_by

Slide 48

Slide 48 text

pony = Pony.find_by(:id => smth) pony.neigh

Slide 49

Slide 49 text

pony = Pony.find_by(:id => smth) if pony pony.neigh else puts “No Pony can’t neigh” end

Slide 50

Slide 50 text

The problem here is two return types

Slide 51

Slide 51 text

nil Pony < AR::Base

Slide 52

Slide 52 text

We’re forced to add a type check

Slide 53

Slide 53 text

Also, I think this is the wrong type check

Slide 54

Slide 54 text

pony = Pony.find_by(:id => smth) if !pony.nil? pony.neigh else puts “No Pony can’t neigh” end

Slide 55

Slide 55 text

A more explicit type check

Slide 56

Slide 56 text

But still wrong

Slide 57

Slide 57 text

pony = Pony.find_by(:id => smth) if !pony.nil? pony.neigh else puts “No Pony can’t neigh” end

Slide 58

Slide 58 text

pony = Pony.find_by(:id => smth) if pony.respond_to?(:neigh) pony.neigh else puts “No Pony can’t neigh” end

Slide 59

Slide 59 text

This type checking adds unnecessary complexity to our app

Slide 60

Slide 60 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 61

Slide 61 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 62

Slide 62 text

Antithesis

Slide 63

Slide 63 text

I am using it to mean “DOING IT WRONG”

Slide 64

Slide 64 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 65

Slide 65 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 66

Slide 66 text

Object oriented programming

Slide 67

Slide 67 text

Konstantin Haase says:

Slide 68

Slide 68 text

Data abstraction and control abstraction

Slide 69

Slide 69 text

Alan Kay says:

Slide 70

Slide 70 text

Everything is an object

Slide 71

Slide 71 text

Objects communicate by sending and receiving messages

Slide 72

Slide 72 text

def bees if :bar == a.foo else end end

Slide 73

Slide 73 text

def bees a.foo nil end

Slide 74

Slide 74 text

Tell don’t ask

Slide 75

Slide 75 text

Objects have their own memory (in terms of objects).

Slide 76

Slide 76 text

Data hiding

Slide 77

Slide 77 text

Every object is an instance of a class (which must be an object).

Slide 78

Slide 78 text

The class holds the shared behavior for its instances (in the form of objects in a program list)

Slide 79

Slide 79 text

To eval a program list, control is passed to the first object and the remainder is treated as its message.

Slide 80

Slide 80 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 81

Slide 81 text

Type Checking is the Antithesis of Object Oriented Programming

Slide 82

Slide 82 text

My Problem

Slide 83

Slide 83 text

My Problem

Slide 84

Slide 84 text

My Solution

Slide 85

Slide 85 text

Just always make your methods return things of a consistent type class

Slide 86

Slide 86 text

Thanks!

Slide 87

Slide 87 text

No obviously there’s more

Slide 88

Slide 88 text

Third party APIs do this all the time

Slide 89

Slide 89 text

pony = Pony.find_by(:id => smth) if pony pony.neigh else puts “No Pony can’t neigh” end

Slide 90

Slide 90 text

The problem here is two return types

Slide 91

Slide 91 text

As a client of this API I am forced to add a type check

Slide 92

Slide 92 text

nil is such a common case

Slide 93

Slide 93 text

How do we fix it?

Slide 94

Slide 94 text

Null object pattern

Slide 95

Slide 95 text

I think this one is quite well known

Slide 96

Slide 96 text

class Pony def horse_power 0.5 end end

Slide 97

Slide 97 text

Pony.find_by( :key => value ) || NullPony.new

Slide 98

Slide 98 text

class NullPony def horse_power 0 end end

Slide 99

Slide 99 text

NullPony quacks the same as Pony

Slide 100

Slide 100 text

Solves the typing problem

Slide 101

Slide 101 text

Summing over ponies will only count Pony objects

Slide 102

Slide 102 text

0 might be the wrong default

Slide 103

Slide 103 text

Pony * NullPony = 0

Slide 104

Slide 104 text

Decided the default for horse_power when defining the class

Slide 105

Slide 105 text

Change is inevitable

Slide 106

Slide 106 text

Can’t predict how NullPony will be used in the future

Slide 107

Slide 107 text

Maybe Typeclass

Slide 108

Slide 108 text

Solves same problem

Slide 109

Slide 109 text

Allows for runtime defaults

Slide 110

Slide 110 text

#map(&blk) -> Maybe #value_or(a) -> a

Slide 111

Slide 111 text

class Just def initialize(value) @value = value end def map(&blk) def value_or(x) Just.new(blk.call(@value)) @value end end end

Slide 112

Slide 112 text

class Nothing def map(&blk) self end def value_or(x) x end end

Slide 113

Slide 113 text

A consistent interface for dealing with missing values

Slide 114

Slide 114 text

NoMethodError: undefined method `foo' for nil:NilClass

Slide 115

Slide 115 text

NoMethodError: undefined method `foo' for nil:NilClass

Slide 116

Slide 116 text

[Maybe, Nothing, Maybe]

Slide 117

Slide 117 text

call map on all of them

Slide 118

Slide 118 text

collapse with value_or

Slide 119

Slide 119 text

To Recap:

Slide 120

Slide 120 text

Null object can replace nils if you know the defaults at class definition time

Slide 121

Slide 121 text

Maybe if you want defaults at run time

Slide 122

Slide 122 text

Your job is not to make Alan Kay happy

Slide 123

Slide 123 text

RSpec RSpec ! ! RSpec 3

Slide 124

Slide 124 text

tinyurl.com/ samfr2014

Slide 125

Slide 125 text

Let’s have some questions a!/samphippen [email protected]