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

Better State Managment

Better State Managment

A story on Functional Programming: how is it different and what benefits does it give to Object Oriented programmers.

nbartlomiej

October 04, 2012
Tweet

More Decks by nbartlomiej

Other Decks in Programming

Transcript

  1. 1 greet verb name = 2 verb ++ ", "

    ++ name ++ "!" 3 4 greet "Hello" "world" -- "hello, world!"
  2. 1 greet = lambda do |verb, name| 2 "#{verb}, #{name}!"

    3 end 4 5 pGreet = greet.curry.('R') 6 7 pGreet.('world') # "R, world!"
  3. board = [[Nothing, Nothing, Nothing], [Nothing, Nothing, Nothing], [Nothing, Nothing,

    Nothing]] replace item index list = take index list ++ [item] ++ drop (index+1) list
  4. board = [[Nothing, Nothing, Nothing], [Nothing, Nothing, Nothing], [Nothing, Nothing,

    Nothing]] replace item index list = take index list ++ [item] ++ drop (index+1) list replace2d item x y matrix = ...
  5. replace2d "x" 0 1 ( replace2d "o" 2 1 (

    replace2d "x" 1 0 board ) )
  6. ... replace2d "x" 0 1 ( replace2d "o" 2 1

    ( replace2d "x" 1 0 board ) )
  7. ... replace2d "x" 0 1 ( replace2d "o" 2 1

    ( replace2d "x" 1 0 board ) )
  8. replace item index list = take index list ++ [item]

    ++ drop (index+1) list Code mostly: • • • •
  9. replace item index list = take index list ++ [item]

    ++ drop (index+1) list Code mostly: •Immutable • • •
  10. replace item index list = take index list ++ [item]

    ++ drop (index+1) list Code mostly: •Immutable •Stateless • •
  11. replace item index list = take index list ++ [item]

    ++ drop (index+1) list Code mostly: •Immutable •Stateless •Without side effects •
  12. replace item index list = take index list ++ [item]

    ++ drop (index+1) list Code mostly: •Immutable •Stateless •Without side effects •Referentially transparent*
  13. OF

  14. 1 class Square 2 3 # what goes in ->

    4 def diagonal 5 # <- what is returned
  15. 1 class Square 2 3 def render 4 buffer =

    RenderBuffer.current 5 buffer.move_to @position 6 # ...
  16. 1 class Square 2 3 def render buffer 4 buffer.move_to

    @position 5 # ... 6 Dependency Injection
  17. 1 class Square 2 attr_accessible :width, :position, 3 :style 4

    5 class Style 6 Single Responsibility Principle
  18. Limit state • Split classes (domain / AR::Concerns) • Single-purpose

    wrappers (e.g. for session) • Limit singleton objects & static methods • ...
  19. Why learn Functional Programming? 1. Better State Management 2. FP

    world needs heroes 3. To know Ruby better
  20. “A large fraction of the flaws in software development are

    due to programmers not fully understanding all the possible states their code may execute in.” John Carmack, “Functional programming in C++”, 2012
  21. Why learn Functional Programming? 1. Better State Management 2. FP

    world needs heroes 3. To know Ruby better