Be aware of side effects

23
be aware of side-effects @ryanlemmer / gmt+2 (thinking even more functionally in Ruby)

description

RubyFuza 2013 talk slides

Transcript of Be aware of side effects

Page 1: Be aware of side effects

be aware ofside-effects

@ryanlemmer / gmt+2

(thinking even more functionally in Ruby)

Page 2: Be aware of side effects

you can do this in Haskell

a = "greetings!"

Page 3: Be aware of side effects

and this

a = "greetings!"b = a + " rubyfuza"

Page 4: Be aware of side effects

and this

a = "greetings!"b = +("greetings!"," rubyfuza")

Page 5: Be aware of side effects

but you can’t do this in Haskell

a = "greetings!"a = a + " rubyfuza"

Page 6: Be aware of side effects

but you can’t do this in Haskell

a = "greetings!"a = +("greetings!"," rubyfuza")

Page 7: Be aware of side effects

value

a = "greetings!"a = a + " rubyfuza"

a = "greetings!"b = a + " rubyfuza"

variable

(immutable) (mutable)

Page 8: Be aware of side effects

value

a = "greetings!"a = "greetings!"

variable

(immutable) (mutable)

b = a + " rubyfuza" a = a + " rubyfuza"

... other stuff ... other stuff

Page 9: Be aware of side effects

value

a = "greetings!"a = a + " rubyfuza"

a = "greetings!"b = a + " rubyfuza"

variable=

Page 10: Be aware of side effects

value

a = "greetings!"a = a + " rubyfuza"

a = "greetings!"b = a + " rubyfuza"

there is no variable

Page 11: Be aware of side effects

value variable

val a = "greetings!";val b = a + " rubyfuza";

var a = "greetings!";a = a + " rubyfuza";

!=

Page 12: Be aware of side effects

def add(a, b) a + bend

Page 13: Be aware of side effects

def add_freaky(a, b) z = a*b a + b * zend

Page 14: Be aware of side effects

@a = 11

def incr_by(n) @a += nend

Page 15: Be aware of side effects

@a = 11EXTRA = 10

def incr_by(n) @a += n + EXTRAend

Page 16: Be aware of side effects

class Calculator attr_reader :balance

def initialize(balance) @balance = balance || 0 end

def incr_by(n) @balance += n end ...end

Page 17: Be aware of side effects

def test num = Calculator.new(0).incr_by(10)  assert_equal 10, numend

Page 18: Be aware of side effects

class Calculator attr_reader :x, :y, :z, :w, ... attr_accessor :v1, :v2, :v3, ...

def initialize(obj, b, c, ...) obj.set_something(w,y,z, ...) end

def wicked_calc(p1, p2, ...) x/y/z/w/v1/v2/v3 end ...end

Page 19: Be aware of side effects

class MoreFunctionalCalculator attr_reader :balance

def initialize(balance) @balance = balance || 0 end

def incr_by(n) @balance = self.add(@balance, n) end

private def add(a, b); a+b endend

Page 20: Be aware of side effects

upcased = [] arr.each do |s| upcased.push(s.upcase) end # use upcased

arr.map do |s| s.upcase end

Page 21: Be aware of side effects

u = arr.map do |s| s.upcase end u.collect do |s| s ++ “zzzz” end

upcased = [] arr.each do |s| upcased.push(s.upcase ++ “zzzz”) end

Page 22: Be aware of side effects

taming side-effectsLIMIT them

extract maximum purity (through composition)use functional forms

CONSTRAIN them, make them EXPLICIT, use CONVENTIONS

Haskell => Monads/STM

Erlang => Messages

Active Record

Page 23: Be aware of side effects

why bother?

cost TIME => $$$cost ENERGY

cause ULCERS

because side effects...