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

Viking Code School Presents Christopher Hendrix of Pivotal Labs

Viking Code School Presents Christopher Hendrix of Pivotal Labs

A materials science engineer by training, a developer by profession, and a designer by passion, Chris will be speaking about object orientation in Ruby.

Viking Education

July 10, 2015
Tweet

More Decks by Viking Education

Other Decks in Technology

Transcript

  1. @materialdesignr #goodobjects
    What Makes a Good Object?
    Christopher Hendrix

    View full-size slide

  2. @materialdesignr #goodobjects
    This talk’s
    Presenter

    View full-size slide

  3. @materialdesignr #goodobjects
    This talk’s
    • How to make objects/methods that are:
    • Sane
    • Confident
    • Consistent
    Goal

    View full-size slide

  4. @materialdesignr #goodobjects
    This talk’s
    Guide

    View full-size slide

  5. @materialdesignr #goodobjects
    This talk’s
    • 5 Family Member “Guiding Principles”
    • Quote
    • Theory/Introduction
    • Code Examples
    • Summary
    • Extra Reading
    • Q&A
    Structure

    View full-size slide

  6. @materialdesignr #goodobjects
    Think like a
    Teenager

    View full-size slide

  7. @materialdesignr #goodobjects
    ―Teenager
    “I will never
    grow up.”

    View full-size slide

  8. @materialdesignr #goodobjects
    Think like a
    • Values vs Entities
    • Colors/Numbers vs People

    • Can a Person be a Value Type?
    • Is teleportation murder?
    Teenager

    View full-size slide

  9. @materialdesignr #goodobjects
    Think like a
    Teenager
    • Fundamental
    Attributes

    View full-size slide

  10. @materialdesignr #goodobjects
    Think like a
    Teenager
    • Fundamental
    Attributes
    Start End
    Duration

    View full-size slide

  11. @materialdesignr #goodobjects
    Think like a
    Teenager
    • Fundamental
    Attributes
    • Computed
    Properties
    Start End
    Duration

    View full-size slide

  12. @materialdesignr #goodobjects
    Think like a
    Teenager
    • Designated
    Initializer

    • Custom
    Initializers

    View full-size slide

  13. @materialdesignr #goodobjects
    Think like a
    Teenager
    • What about 

    Freeform Objects?

    View full-size slide

  14. @materialdesignr #goodobjects
    Think like a
    Teenager
    • “Subtyping”

    View full-size slide

  15. @materialdesignr #goodobjects
    Think like a
    Teenager
    • Hide your
    “Subtyping”

    View full-size slide

  16. @materialdesignr #goodobjects
    Think like a
    • Separate Fundamental Attributes from
    Computed Properties
    • Think of a single Designated Initializer
    • Use Custom Initializers which delegate
    • Hide your “Subtyping”
    Teenager

    View full-size slide

  17. @materialdesignr #goodobjects
    Think like a
    Child

    View full-size slide

  18. @materialdesignr #goodobjects
    ―Child
    “But you promised!”

    View full-size slide

  19. @materialdesignr #goodobjects
    • f(x) = x^2
    • A transformation
    • What makes f(x) a Good Function?
    Think like a
    Child

    View full-size slide

  20. @materialdesignr #goodobjects
    “A function is a relation
    between a set of inputs and a
    set of permissible outputs with
    the property that each input is
    related to exactly one output.”
    All Integer
    1
    2
    3

    + Integer
    1
    4
    9

    View full-size slide

  21. @materialdesignr #goodobjects
    • f(x) = x^2
    • A Good Function has
    • Consistent Input Types
    • Consistent Output Types
    • Well defined relation from each input to output
    Think like a
    Child
    Integer
    1
    2
    3

    Integer
    1
    4
    9

    View full-size slide

  22. @materialdesignr #goodobjects
    Think like a
    Child

    View full-size slide

  23. @materialdesignr #goodobjects
    Think like a
    Child

    View full-size slide

  24. @materialdesignr #goodobjects
    Think like a
    Child

    View full-size slide

  25. @materialdesignr #goodobjects
    Think like a
    • Think of your function’s Type Signatures
    • Have consistent outputs
    • Use Arrays to encode potential failure
    Child

    View full-size slide

  26. @materialdesignr #goodobjects
    Think like an
    Adult

    View full-size slide

  27. @materialdesignr #goodobjects
    ―Adult
    “No.”

    View full-size slide

  28. @materialdesignr #goodobjects
    Think like an
    • Not everyone else makes good functions
    • Function dependencies are 

    sources of uncertainty
    Adult

    View full-size slide

  29. @materialdesignr #goodobjects
    Think like an
    Adult

    View full-size slide

  30. @materialdesignr #goodobjects
    Think like an
    Adult

    View full-size slide

  31. @materialdesignr #goodobjects
    Think like an
    Adult
    • Reject
    Unworkable
    Values


    View full-size slide

  32. @materialdesignr #goodobjects
    Think like an
    Adult
    • NaN


    View full-size slide

  33. @materialdesignr #goodobjects
    Think like an
    Adult
    • Does this help?


    View full-size slide

  34. @materialdesignr #goodobjects
    Think like an
    Adult
    • Reject Values at
    entry points

    View full-size slide

  35. @materialdesignr #goodobjects
    Think like an
    Adult
    • Wrap External
    Objects

    View full-size slide

  36. @materialdesignr #goodobjects
    Think like an
    • Say No to bad dependencies
    • Earliest Possible Rejection
    • Wrap External Objects in Confident Values
    Adult

    View full-size slide

  37. @materialdesignr #goodobjects
    Think like a
    Senior

    View full-size slide

  38. @materialdesignr #goodobjects
    ―Senior
    “Who are you?”

    View full-size slide

  39. @materialdesignr #goodobjects
    Think like a
    Senior

    View full-size slide

  40. @materialdesignr #goodobjects
    Think like a
    Senior
    • Accessing Data

    View full-size slide

  41. @materialdesignr #goodobjects
    Think like a
    • Is it really Data?
    • Functions vs. Methods vs. Messages
    Senior
    @materialdesignr
    Sally Bob
    Age:

    24
    24

    View full-size slide

  42. @materialdesignr #goodobjects
    Think like a
    • Is it really Data?
    • Functions vs. Methods vs. Messages
    Senior
    Sally Bob
    Age:

    24
    29
    Age? def age
    if trying_to_impress?
    return @age + 5
    else
    return @age
    end
    end

    View full-size slide

  43. @materialdesignr #goodobjects
    Think like a
    • Is it really Data?
    • Functions vs. Methods vs. Messages
    • Same Message, Different Methods
    Senior
    Sally Harry
    Age:

    32
    32
    Age? def age
    # I’m honest
    return @age
    end

    View full-size slide

  44. @materialdesignr #goodobjects
    Think like a
    Senior
    • Queries
    • Commands

    View full-size slide

  45. @materialdesignr #goodobjects
    Think like a
    Senior
    • Queries
    • Commands

    View full-size slide

  46. @materialdesignr #goodobjects
    Think like a
    • There is No Data, Only Zuul Messages
    • Ask Nicely instead of grabbing internals
    • Tell Confidently instead of DIY
    Senior

    View full-size slide

  47. @materialdesignr #goodobjects
    Think like a
    Baby

    View full-size slide

  48. @materialdesignr #goodobjects
    ―Baby
    “Who am I?”

    View full-size slide

  49. @materialdesignr #goodobjects
    Think like a
    • What’s so special about me?
    • Everything is message sending
    • Messages to Self
    Baby

    View full-size slide

  50. @materialdesignr #goodobjects
    Think like a
    Baby

    View full-size slide

  51. @materialdesignr #goodobjects
    Think like a
    Baby
    • Accessing Data

    View full-size slide

  52. @materialdesignr #goodobjects
    Think like a
    Baby
    • Messages to Self
    • Information
    Hiding

    View full-size slide

  53. @materialdesignr #goodobjects
    Think like a
    • Other Methods should be treated like
    Other Objects
    • Hide your own data
    Baby

    View full-size slide

  54. @materialdesignr #goodobjects
    This talk’s
    • Teenager — Make your constructors matter
    • Child — Think about function signatures
    • Adult — Use errors to prevent unknown state
    • Senior — Send messages to other objects
    • Baby — Send messages to yourself
    Summary

    View full-size slide

  55. @materialdesignr #goodobjects
    Reading is
    Practical Object Oriented Design in Ruby

    ―Sandi Metz
    Confident Ruby

    ―Avdi Grimm
    Refactoring

    ―Martin Fowler
    Fundamental

    View full-size slide

  56. @materialdesignr #goodobjects
    Questions
    &
    Answers

    View full-size slide

  57. @materialdesignr #goodobjects
    This talk’s
    • www.materialdesignr.com
    • @materialdesignr
    • github.com/chendrix
    • linkedin.com/in/materialdesignr
    Self-Promotion

    View full-size slide