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

Go Ahead, Make a Mess - Sandi Metz

Sandi Metz
September 15, 2012

Go Ahead, Make a Mess - Sandi Metz

This talk shows how to use Object-Oriented Design to create the best kinds of messes, those that let you get software out the door today without regretting your actions tomorrow.

This is the 45 minute version, given at GoGaRuCo, Sept. 15 2012.

Sandi Metz

September 15, 2012
Tweet

More Decks by Sandi Metz

Other Decks in Programming

Transcript

  1. @sandimetz Sept 2012 Satisfaction Those who cannot remember the past

    are condemned to repeat it. 5 Friday, September 14, 2012
  2. @sandimetz Sept 2012 Example #1 class  Game    def  shock_cost

           Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math        #  ...            #  which  eventually  return        cost    end     31 Friday, September 14, 2012
  3. @sandimetz Sept 2012 Example #1 class  Game    def  shock_cost

           Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math        #  ...            #  which  eventually  return        cost    end     31 Friday, September 14, 2012
  4. @sandimetz Sept 2012 Example #1 class  Game    def  shock_cost

           Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math        #  ...            #  which  eventually  return        cost    end     31 Friday, September 14, 2012
  5. @sandimetz Sept 2012 Example #1 class  Game    def  shock_cost

           Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math        #  ...            #  which  eventually  return        cost    end     31 Friday, September 14, 2012
  6. @sandimetz Sept 2012 Example #1 class  Game    def  shock_cost

           Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math        #  ...            #  which  eventually  return        cost    end     31 Friday, September 14, 2012
  7. @sandimetz Sept 2012 Example #1 class  Game    def  shock_cost

           Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math        #  ...            #  which  eventually  return        cost    end     MESS 31 Friday, September 14, 2012
  8. @sandimetz Sept 2012 Example #1 class  Game    def  shock_cost

           Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math        #  ...            #  which  eventually  return        cost    end     Omega Mess 31 Friday, September 14, 2012
  9. @sandimetz Sept 2012 class  Game    def  shock_cost    

       Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math            #  ...            #  which  eventually  return        cost    end     end Example #1 38 Friday, September 14, 2012
  10. @sandimetz Sept 2012 class  Game    def  shock_cost    

       Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math            #  ...            #  which  eventually  return        cost    end     end It’s not about the code Example #1 38 Friday, September 14, 2012
  11. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose 43 Friday, September 14, 2012
  12. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose 43 Friday, September 14, 2012
  13. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose 43 Friday, September 14, 2012
  14. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Public API 43 Friday, September 14, 2012
  15. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Public API Private Behavior 43 Friday, September 14, 2012
  16. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Public API Private Behavior Dependencies 43 Friday, September 14, 2012
  17. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Public API Private Behavior Dependencies Expose 43 Friday, September 14, 2012
  18. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Public API Private Behavior Dependencies Expose Hide 43 Friday, September 14, 2012
  19. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Public API Private Behavior Dependencies Expose Hide Minimize 43 Friday, September 14, 2012
  20. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Public API Private Behavior Dependencies Expose Hide Minimize Move 43 Friday, September 14, 2012
  21. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation Game depends on 3. cost message 4. Shock class name 5. cost receiver 44 Friday, September 14, 2012
  22. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation Game depends on 3. cost message 4. Shock class name 5. cost receiver 44 Friday, September 14, 2012
  23. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation Game depends on 3. cost message 4. Shock class name 5. cost receiver 44 Friday, September 14, 2012
  24. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation Game depends on 3. cost message 4. Shock class name 5. cost receiver 45 Friday, September 14, 2012
  25. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 46 Friday, September 14, 2012
  26. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 1 46 Friday, September 14, 2012
  27. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method (expose)

    2. cost calculation Game depends on 3. cost message 4. Shock class name 5. cost receiver 47 Friday, September 14, 2012
  28. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method (expose)

    2. cost calculation Game depends on 3. cost message 4. Shock class name 5. cost receiver 47 Friday, September 14, 2012
  29. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 1 48 Friday, September 14, 2012
  30. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 1 2 48 Friday, September 14, 2012
  31. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method (expose)

    2. cost calculation (hide) Game depends on 3. cost message 4. Shock class name 5. cost receiver 49 Friday, September 14, 2012
  32. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method (expose)

    2. cost calculation (hide) Game depends on 3. cost message 4. Shock class name 5. cost receiver 49 Friday, September 14, 2012
  33. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method (expose)

    2. cost calculation (hide) Game depends on 3. cost message 4. Shock class name 5. cost receiver 49 Friday, September 14, 2012
  34. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 50 Friday, September 14, 2012
  35. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 3 50 Friday, September 14, 2012
  36. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation Game depends on 3. cost message (minimize) 4. Shock class name 5. cost receiver 51 Friday, September 14, 2012
  37. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation Game depends on 3. cost message (minimize) 4. Shock class name 5. cost receiver 51 Friday, September 14, 2012
  38. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 3 52 Friday, September 14, 2012
  39. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 4 5 3 52 Friday, September 14, 2012
  40. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 4 5 3 52 Friday, September 14, 2012
  41. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide ? Minimize Move 4 5 3 52 Friday, September 14, 2012
  42. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation Game depends on 3. cost message (minimize) 4. Shock class name 5. cost receiver ??? 53 Friday, September 14, 2012
  43. @sandimetz Sept 2012 class  Game    def  shock_cost    

       Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math            #  ...            #  which  eventually  return        cost    end     end Example #1 54 Friday, September 14, 2012
  44. @sandimetz Sept 2012 What is the future cost of doing

    nothing now? 55 Friday, September 14, 2012
  45. @sandimetz Sept 2012 class  Game    def  shock_cost    

       Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math            #  ...            #  which  eventually  return        cost    end     end Example #1 56 Friday, September 14, 2012
  46. @sandimetz Sept 2012 class  Game    def  shock_cost    

       Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math            #  ...            #  which  eventually  return        cost    end     end I would inject this dependency Example #1 56 Friday, September 14, 2012
  47. @sandimetz Sept 2012 class  Game    def  shock_cost    

       Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math            #  ...            #  which  eventually  return        cost    end     end I would inject this dependency I would neaten this mess Example #1 56 Friday, September 14, 2012
  48. @sandimetz Sept 2012 class  Game    def  shock_cost    

       Shock.new.cost    end end class  Shock    def  cost            #  lines  of  messy  math            #  ...            #  which  eventually  return        cost    end     end I would inject this dependency I would neaten this mess You might not Example #1 56 Friday, September 14, 2012
  49. @sandimetz Sept 2012 Example #2 class  Game    def  shock_cost(type)

           Shock.new(type).cost    end end 60 Friday, September 14, 2012
  50. @sandimetz Sept 2012 Example #2 class  Game    def  shock_cost(type)

           Shock.new(type).cost    end end 60 Friday, September 14, 2012
  51. @sandimetz Sept 2012 Example #2 class  Game    def  shock_cost(type)

           Shock.new(type).cost    end end 60 Friday, September 14, 2012
  52. @sandimetz Sept 2012 Example #2 class  Shock    attr_reader  :type

       def  initialize(type)  #  ...    def  cost        case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end     61 Friday, September 14, 2012
  53. @sandimetz Sept 2012 Example #2 class  Shock    attr_reader  :type

       def  initialize(type)  #  ...    def  cost        case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end     61 Friday, September 14, 2012
  54. @sandimetz Sept 2012 Example #2 class  Shock    attr_reader  :type

       def  initialize(type)  #  ...    def  cost        case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end     61 Friday, September 14, 2012
  55. @sandimetz Sept 2012 Example #2 class  Shock    attr_reader  :type

       def  initialize(type)  #  ...    def  cost        case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end     61 Friday, September 14, 2012
  56. @sandimetz Sept 2012 Example #2 class  Shock    attr_reader  :type

       def  initialize(type)  #  ...    def  cost        case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end     61 Friday, September 14, 2012
  57. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation 3. 4. 63 Friday, September 14, 2012
  58. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    cost calculation 3. 4. 63 Friday, September 14, 2012
  59. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation 64 Friday, September 14, 2012
  60. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation 64 Friday, September 14, 2012
  61. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation 64 Friday, September 14, 2012
  62. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation 65 Friday, September 14, 2012
  63. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation Omega Mess? 66 Friday, September 14, 2012
  64. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation Omega Mess? 66 Friday, September 14, 2012
  65. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation Meta Knowledge 67 Friday, September 14, 2012
  66. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation 72 Friday, September 14, 2012
  67. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation 72 Friday, September 14, 2012
  68. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations 3. four shock types 4. which type gets what calculation 72 Friday, September 14, 2012
  69. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 2 73 Friday, September 14, 2012
  70. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 2 73 Friday, September 14, 2012
  71. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations (move) 3. four shock types 4. which type gets what calculation 74 Friday, September 14, 2012
  72. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations (move) 3. four shock types 4. which type gets what calculation 74 Friday, September 14, 2012
  73. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 75 Friday, September 14, 2012
  74. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 3 75 Friday, September 14, 2012
  75. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations (move) 3. four shock types (move) 4. which type gets what calculation 76 Friday, September 14, 2012
  76. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations (move) 3. four shock types (move) 4. which type gets what calculation 76 Friday, September 14, 2012
  77. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 77 Friday, September 14, 2012
  78. @sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose

    Outside of My Purpose Expose Hide Minimize Move 4 77 Friday, September 14, 2012
  79. @sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.

    four shock cost calculations (move) 3. four shock types (move) 4. which type gets what calculation (min) 78 Friday, September 14, 2012
  80. @sandimetz Sept 2012 Move it Find the message (Create the

    object) 80 Friday, September 14, 2012
  81. @sandimetz Sept 2012 Move it Find the message (Create the

    object) Send the message 80 Friday, September 14, 2012
  82. @sandimetz Sept 2012 #3 Composition    def  cost    

       case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end 83 Friday, September 14, 2012
  83. @sandimetz Sept 2012 #3 Composition    def  cost    

       case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end 83 Friday, September 14, 2012
  84. @sandimetz Sept 2012 #3 Composition    def  cost    

       case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end 83 Friday, September 14, 2012
  85. @sandimetz Sept 2012 #3 Composition    def  cost    

       case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end What’s the message? 83 Friday, September 14, 2012
  86. @sandimetz Sept 2012 #3 Composition    def  cost    

       case  type        when  :front            someObligingObj.compute        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end 84 Friday, September 14, 2012
  87. @sandimetz Sept 2012 #3 Composition    def  cost    

       case  type        when  :front            someObligingObj.compute        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end ??? 84 Friday, September 14, 2012
  88. @sandimetz Sept 2012    def  cost        case

     type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition class  FrontShockCost    def  compute        #  the  original  mess    end end 85 Friday, September 14, 2012
  89. @sandimetz Sept 2012    def  cost        case

     type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition class  FrontShockCost    def  compute        #  the  original  mess    end end 85 Friday, September 14, 2012
  90. @sandimetz Sept 2012    def  cost        case

     type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition class  FrontShockCost    def  compute        #  the  original  mess    end end Hide the Mess 85 Friday, September 14, 2012
  91. @sandimetz Sept 2012    def  cost        case

     type        when  :front            FrontShockCost.new.compute        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition class  FrontShockCost    def  compute        #  the  original  mess    end end 86 Friday, September 14, 2012
  92. @sandimetz Sept 2012    def  cost        case

     type        when  :front            FrontShockCost.new.compute        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition class  FrontShockCost    def  compute        #  the  original  mess    end end Send the Message 86 Friday, September 14, 2012
  93. @sandimetz Sept 2012    def  cost        case

     type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition 87 Friday, September 14, 2012
  94. @sandimetz Sept 2012    def  cost        case

     type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition 87 Friday, September 14, 2012
  95. @sandimetz Sept 2012    def  cost        case

     type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition 87 Friday, September 14, 2012
  96. @sandimetz Sept 2012    def  cost        case

     type        when  :front            #  the  original  mess        when  :rear            #  a  slightly  different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end #3 Composition Hide all of the Messes 87 Friday, September 14, 2012
  97. @sandimetz Sept 2012 #3 Composition class  FrontShockCost    def  compute

           #  the  original  mess    end end class  RearShockCost    def  compute        #  slightly  changed  mess    end end #  etc 88 Friday, September 14, 2012
  98. @sandimetz Sept 2012 #3 Composition class  FrontShockCost    def  compute

           #  the  original  mess    end end class  RearShockCost    def  compute        #  slightly  changed  mess    end end #  etc 88 Friday, September 14, 2012
  99. @sandimetz Sept 2012 #3 Composition class  FrontShockCost    def  compute

           #  the  original  mess    end end class  RearShockCost    def  compute        #  slightly  changed  mess    end end #  etc 88 Friday, September 14, 2012
  100. @sandimetz Sept 2012 #3 Composition class  Shock    def  cost(type)

           case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end 89 Friday, September 14, 2012
  101. @sandimetz Sept 2012 #3 Composition class  Shock    def  cost(type)

           case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end Send the Message 89 Friday, September 14, 2012
  102. @sandimetz Sept 2012 #3 Composition class  Shock    def  cost(type)

           case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end 90 Friday, September 14, 2012
  103. @sandimetz Sept 2012 #3 Composition class  Shock    def  cost(type)

           case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end 90 Friday, September 14, 2012
  104. @sandimetz Sept 2012 #3 Composition class  Shock    def  cost(type)

           case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end Really? 90 Friday, September 14, 2012
  105. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four shock cost calculations 3. four shock types 4. which type gets what calculation 91 Friday, September 14, 2012
  106. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four shock cost calculations 3. four shock types 4. which type gets what calculation 91 Friday, September 14, 2012
  107. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four ShockCostxx classes 3. four shock types 4. which type gets what calculation 92 Friday, September 14, 2012
  108. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four ShockCostxx classes 3. four shock types 4. which type gets what calculation 93 Friday, September 14, 2012
  109. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four ShockCostxx classes 3. four shock types 4. which type gets what class 94 Friday, September 14, 2012
  110. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end #3 Composition 97 Friday, September 14, 2012
  111. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end #3 Composition 97 Friday, September 14, 2012
  112. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end when  :front    FrontShockCost.new.compute #3 Composition 98 Friday, September 14, 2012
  113. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end when  :foo    FooShockCost.new.compute #3 Composition 98 Friday, September 14, 2012
  114. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end when  :foo    FooShockCost.new.compute #3 Composition 98 Friday, September 14, 2012
  115. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end when  :foo    FooShockCost.new.compute #3 Composition Convention over con guration 98 Friday, September 14, 2012
  116. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end when  :foo    FooShockCost.new.compute class_name  =  type.to_s.titleize  +  "ShockCost" #3 Composition Convention over con guration 98 Friday, September 14, 2012
  117. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end class_name  =  type.to_s.titleize  +  "ShockCost" eval(class_name).new.compute #3 Composition Convention over con guration when  :foo    FooShockCost.new.compute 99 Friday, September 14, 2012
  118. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end class_name  =  type.to_s.titleize  +  "ShockCost" eval(class_name).new.compute Object.const_get(class_name).new.compute #3 Composition Convention over con guration when  :foo    FooShockCost.new.compute 100 Friday, September 14, 2012
  119. @sandimetz Sept 2012 class  Shock    def  cost(type)    

       case  type        when  :front            FrontShockCost.new.compute        when  :rear            RearShockCost.new.compute        when  :lefty            LeftyShockCost.new.compute        else            ShockCost.new.compute        end    end    #  ... end class_name  =  type.to_s.titleize  +  "ShockCost" eval(class_name).new.compute Object.const_get(class_name).new.compute class_name.constantize.new.compute #3 Composition Convention over con guration when  :foo    FooShockCost.new.compute 101 Friday, September 14, 2012
  120. @sandimetz Sept 2012 Example #3 class  Shock    def  cost(type)

         class_name  =  (type.to_s.titleize  +  "ShockCost")      class_name.constantize.new.compute    end end 102 Friday, September 14, 2012
  121. @sandimetz Sept 2012 Example #3 class  Shock    def  cost(type)

         class_name  =  (type.to_s.titleize  +  "ShockCost")      class_name.constantize.new.compute    end end 102 Friday, September 14, 2012
  122. @sandimetz Sept 2012 Example #3 class  Shock    def  cost(type)

         class_name  =  (type.to_s.titleize  +  "ShockCost")      class_name.constantize.new.compute    end end Factory 102 Friday, September 14, 2012
  123. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four ShockCostxx classes 3. four shock types 4. which type gets what class 104 Friday, September 14, 2012
  124. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four ShockCostxx classes 3. four shock types 4. which type gets what class 104 Friday, September 14, 2012
  125. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four ShockCostxx classes 3. four shock types 4. which type gets what class 104 Friday, September 14, 2012
  126. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. four ShockCostxx classes 3. four shock types 4. which type gets what class 104 Friday, September 14, 2012
  127. @sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method

    2. the convention 105 Friday, September 14, 2012
  128. @sandimetz Sept 2012 Before #3 Composition    def  cost  

         case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly              #    different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end 106 Friday, September 14, 2012
  129. @sandimetz Sept 2012 Before #3 Composition    def  cost  

         case  type        when  :front            #  the  original  mess        when  :rear            #  a  slightly              #    different  mess        when  :lefty            #  another  variant        else            #  and  so  on.        end    end Many changes affect this code 106 Friday, September 14, 2012
  130. @sandimetz Sept 2012 After Example #3 class  Shock    def

     cost(type)      class_name  =  (type.to_s.titleize  +  "ShockCost")      class_name.constantize.new.compute    end end 107 Friday, September 14, 2012
  131. @sandimetz Sept 2012 After Example #3 class  Shock    def

     cost(type)        to_class(type).new.compute    end    def  to_class(type)        (type.to_s.titleize  +  "ShockCost").constantize    end end 108 Friday, September 14, 2012
  132. @sandimetz Sept 2012 After Example #3 class  Shock    def

     cost(type)        to_class(type).new.compute    end    def  to_class(type)        (type.to_s.titleize  +  "ShockCost").constantize    end end Few changes affect this code 109 Friday, September 14, 2012
  133. @sandimetz Sept 2012 After Example #3 class  FrontShockCost    def

     compute        #  the  original  mess    end end class  Shock    def  cost(type)        to_class(type).new.compute    end    def  to_class(type)        (type.to_s.titleize  +  "ShockCost").constantize    end end Few changes affect this code 109 Friday, September 14, 2012
  134. @sandimetz Sept 2012 After Example #3 class  FrontShockCost    def

     compute        #  the  original  mess    end end class  Shock    def  cost(type)        to_class(type).new.compute    end    def  to_class(type)        (type.to_s.titleize  +  "ShockCost").constantize    end end class  RearShockCost    def  compute        #  slightly  changed  mess    end end Few changes affect this code 109 Friday, September 14, 2012
  135. @sandimetz Sept 2012 After Example #3 class  FrontShockCost    def

     compute        #  the  original  mess    end end class  Shock    def  cost(type)        to_class(type).new.compute    end    def  to_class(type)        (type.to_s.titleize  +  "ShockCost").constantize    end end class  RearShockCost    def  compute        #  slightly  changed  mess    end end Etc... Few changes affect this code 109 Friday, September 14, 2012
  136. @sandimetz Sept 2012 After Example #3 class  FrontShockCost    def

     compute        #  the  original  mess    end end class  Shock    def  cost(type)        to_class(type).new.compute    end    def  to_class(type)        (type.to_s.titleize  +  "ShockCost").constantize    end end class  RearShockCost    def  compute        #  slightly  changed  mess    end end Etc... Few changes affect this code Need a new one? Follow the convention! 109 Friday, September 14, 2012
  137. @sandimetz Sept 2012 Reprise Find the message (Create the object)

    Send the message 119 Friday, September 14, 2012
  138. http://www.flickr.com/photos/slumadridcampus/5736110401/ http://www.flickr.com/photos/jennifermarquez/4441957235/ http://www.flickr.com/photos/58789412@N00/4106497024/ http://www.flickr.com/photos/27927484@N00/264072199/ http://www.flickr.com/photos/alexindigo/2572565946/in/photostream/ http://www.flickr.com/photos/moje_moje/4500967490/ http://www.flickr.com/photos/dr_nixon/4366089630/ http://www.flickr.com/photos/question_everything/4778500858/ http://www.flickr.com/photos/thegforcers/6202236364/in/photostream/ http://www.flickr.com/photos/jeffhester/2785524078/

    http://www.flickr.com/photos/thegforcers/6040964794/in/photostream/ http://www.flickr.com/photos/magnus_akselvoll/4741056713/ http://www.flickr.com/photos/chiotsrun/6553699405/ http://www.flickr.com/photos/45682148@N05/7943889710/ http://www.flickr.com/photos/piper/393928412/ http://www.flickr.com/photos/thegforcers/6202237588/in/photostream/ http://www.flickr.com/photos/juju2nantes/5895167252/ Credits 137 Friday, September 14, 2012