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

Thinking in Objects RubyConf 2012

Josh Susser
November 03, 2012

Thinking in Objects RubyConf 2012

This talk presents OOP fundamentals, and a basic vocabulary that helps to think about programming in OOP languages.

Josh Susser

November 03, 2012
Tweet

More Decks by Josh Susser

Other Decks in Programming

Transcript

  1. Thinking
    in
    Objects

    View Slide

  2. @joshsusser
    CTO - Cognitive Health Innovations
    RubyRogues
    GoGaRuCo

    View Slide

  3. View Slide

  4. Perspective is worth
    80 I.Q. points.
    –Alan Kay

    View Slide

  5. View Slide

  6. http://www.flickr.com/photos/xlibber/2962488972

    View Slide

  7. Object = Identity
    + Behavior
    + State
    OOP = Objects
    + Encapsulation
    + Polymorphism
    + Inheritance

    View Slide

  8. It’s all about
    MESSAGES

    View Slide

  9. View Slide

  10. Smalltalk
    (OOP)
    LISP
    (FP) Perl
    (scripting)

    View Slide

  11. Object = Identity
    + Behavior
    + State

    View Slide

  12. Identity
    $ nslookup github.com
    207.97.227.239

    View Slide

  13. Identity
    s = "foo"
    s.object_id #=> 123450
    t = "foo" #=> "foo"
    t.object_id #=> 123780

    View Slide

  14. Identity
    s = "foo"; t = "foo"
    s == t #=> true
    s.equal?(t) #=> false

    View Slide

  15. Identity
    s = "foo"; t = "foo"
    s << "d" #=> "food"
    t #=> "foo"

    View Slide

  16. Behavior
    "foo".methods
    #=> [:<=>, :==, :===, :eql?, :hash,
    :casecmp, :+, :*, :%, :[], :[]=, :insert,
    :length, :size, :bytesize, :empty?,
    :=~, :match, :succ, :succ!, :next,
    :next!, :upto, :index, :reverse, ...]

    View Slide

  17. methods
    def even?
    self % 2 == 0
    end
    def odd?
    !self.even?
    end

    View Slide

  18. Object = Identity
    + Behavior
    + State

    View Slide

  19. uniques
    nil
    true, false
    :ruby, :rails

    View Slide

  20. uniques
    GUEST = Object.new
    def GUEST.name
    "Guest User"
    end

    View Slide

  21. State
    s = "foo"
    s << "d" #=> "food"
    s.length #=> 4

    View Slide

  22. State
    = Behavior

    View Slide

  23. Uniform Access
    attr_accessor :first_name

    View Slide

  24. Uniform Access
    def first_name
    @first_name
    end

    View Slide

  25. Uniform Access
    def first_name
    @full_name.split(" ").first
    end

    View Slide

  26. JavaScript
    user.firstName
    user.firstName()

    View Slide

  27. GET /index
    •WEB_ROOT/index.html
    •app/views/home/index.erb
    •memcached / varnish

    View Slide

  28. Encapsulation

    View Slide

  29. Polymorphism
    Duck-typing
    Protocol
    API

    View Slide

  30. Object-based
    Objects
    + Encapsulation
    + Polymorphism
    dynamic binding

    View Slide

  31. Inheritance
    • Implementation
    • Type

    View Slide

  32. Delegation
    • Composition
    - Forwardable
    • Prototypes
    - JavaScript, SELF

    View Slide

  33. Delegation IS
    Inheritance
    Lynn Andrea Stein
    OOPSLA 1987
    "Treaty of Orlando"

    View Slide

  34. OOP= Objects
    + Encapsulation
    + Polymorphism
    + Inheritance

    View Slide

  35. It’s all about
    MESSAGES

    View Slide

  36. call a method

    View Slide

  37. • delegation
    • method_missing
    call a method

    View Slide

  38. send a message
    call a method

    View Slide

  39. class Person
    def name; @name; end
    end
    class Employee < Person
    def name; "#{@name}, #{@title}"; end
    end
    class Letter
    # address letter to recipient's name without title
    def recipient_name
    super_name = Person.instance_method(:name)
    bound_name = super_name.bind(@recipient)
    bound_name.call
    end
    end

    View Slide

  40. class Person
    def name; @name; end
    end
    class Employee < Person
    def name; "#{@name}, #{@title}"; end
    end
    class Letter
    # address letter to recipient's name without title
    def recipient_name
    super_name = Person.instance_method(:name)
    bound_name = super_name.bind(@recipient)
    bound_name.call
    end
    end

    View Slide

  41. use objects
    •blocks & lambdas
    •hashes & arrays
    •symbols (names)
    instead of

    View Slide

  42. use objects
    reification unites
    behavior with
    data

    View Slide

  43. validation
    validates_presence_of :phone
    validate { |r|
    r.home_phone? || r.cell_phone? }
    validate :validate_has_a_phone
    validates_with HasAPhone

    View Slide

  44. polymorphism
    Method lookup is fast.
    Testing class type is slow
    and messy.

    View Slide

  45. def scope
    if @product
    if current_api_user.has_spree_role?("admin")
    unless params[:show_deleted]
    variants = @product.variants_including_master
    else
    variants = @product.variants_including_master_and_deleted
    end
    else
    variants = @product.variants_including_master
    end
    else
    variants = Variant.scoped
    if current_api_user.has_spree_role?("admin")
    unless params[:show_deleted]
    variants = Variant.active
    end
    end
    end
    variants
    end

    View Slide

  46. def scope
    if @product
    if current_api_user.has_spree_role?("admin")
    unless params[:show_deleted]
    variants = @product.variants_including_master
    else
    variants = @product.variants_including_master_and_deleted
    end
    else
    variants = @product.variants_including_master
    end
    else
    variants = Variant.scoped
    if current_api_user.has_spree_role?("admin")
    unless params[:show_deleted]
    variants = Variant.active
    end
    end
    end
    variants
    end

    View Slide

  47. class Variant
    class << self
    alias_method :variants_including_master_and_deleted, :scoped
    alias_method :variants_including_master, :active
    end
    end
    def scope
    scoper = @product || Variant
    if current_api_user.has_spree_role?("admin") && params[:show_deleted]
    scoper.variants_including_master_and_deleted
    else
    scoper.variants_including_master
    end
    end

    View Slide

  48. Object = Identity
    + Behavior
    + State
    OOP = Objects
    + Encapsulation
    + Polymorphism
    + Inheritance
    Messages = secret sauce

    View Slide