Share
Embed
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
Transcript
266 slides
-
@sandimetz Sept 2012 Go Ahead Make a Mess Sandi Metz
1 Friday, September 14, 2012 -
@sandimetz Sept 2012 Life Cycle of a Rails Project 2
Friday, September 14, 2012 -
@sandimetz Sept 2012 Start Beta 1.0 2.0 Today Satisfaction Timeline
3 Friday, September 14, 2012 -
@sandimetz Sept 2012 Start Beta 1.0 2.0 Today Satisfaction Timeline
4 Friday, September 14, 2012 -
@sandimetz Sept 2012 Satisfaction Those who cannot remember the past
are condemned to repeat it. 5 Friday, September 14, 2012 -
@sandimetz Sept 2012 Start Beta 1.0 2.0 Today Satisfaction Timeline
23 Friday, September 14, 2012 -
@sandimetz Sept 2012 Object-Oriented Design Stop worrying and learn to
love the mess 24 Friday, September 14, 2012 -
Label the Knowledge Complicate the Code Isolate the Instability 28
Friday, September 14, 2012 -
Label the Knowledge Complicate the Code Isolate the Instability Manage
the Mess 29 Friday, September 14, 2012 -
@sandimetz Sept 2012 Example #1 Label the Knowledge 30 Friday,
September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
Omega Mess End of the Line No Dependencies 32 Friday,
September 14, 2012 -
Omega Mess End of the Line No Dependencies No Dependents
32 Friday, September 14, 2012 -
@sandimetz Sept 2012 Internal External Knowledge Stability Low High 34
Friday, September 14, 2012 -
@sandimetz Sept 2012 Internal External Knowledge Stability Low High 34
Friday, September 14, 2012 -
@sandimetz Sept 2012 Internal External Knowledge Stability Low High 34
Friday, September 14, 2012 -
@sandimetz Sept 2012 Internal External Knowledge Stability Low High 34
Friday, September 14, 2012 -
@sandimetz Sept 2012 Internal External Knowledge Stability Low High 34
Friday, September 14, 2012 -
@sandimetz Sept 2012 Internal External Knowledge Stability Low High 34
Friday, September 14, 2012 -
@sandimetz Sept 2012 Internal External Knowledge Stability Low High 34
Friday, September 14, 2012 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose 43 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose 43 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose 43 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Public API 43 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Public API Private Behavior 43 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Public API Private Behavior Dependencies 43 Friday, September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 46 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 1 46 Friday, September 14, 2012 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 1 48 Friday, September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 50 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 3 50 Friday, September 14, 2012 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 3 52 Friday, September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 What is the future cost of doing
nothing now? 55 Friday, September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 Example #2 Complicate the code 59 Friday,
September 14, 2012 -
@sandimetz Sept 2012 Example #2 Complicate the code Shocks vary
in cost 59 Friday, September 14, 2012 -
@sandimetz Sept 2012 Example #2 class Game def shock_cost(type)
Shock.new(type).cost end end 60 Friday, September 14, 2012 -
@sandimetz Sept 2012 Example #2 class Game def shock_cost(type)
Shock.new(type).cost end end 60 Friday, September 14, 2012 -
@sandimetz Sept 2012 Example #2 class Game def shock_cost(type)
Shock.new(type).cost end end 60 Friday, September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.
cost calculation 3. 4. 63 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Shock knows 1. cost method 2.
cost calculation 3. 4. 63 Friday, September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 2 73 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 2 73 Friday, September 14, 2012 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 75 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 3 75 Friday, September 14, 2012 -
@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 -
@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 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 77 Friday, September 14, 2012 -
@sandimetz Sept 2012 Knowledge Plot Stable Unstable Within My Purpose
Outside of My Purpose Expose Hide Minimize Move 4 77 Friday, September 14, 2012 -
@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 -
@sandimetz Sept 2012 Preference Send a message rather than implement
behavior 79 Friday, September 14, 2012 -
@sandimetz Sept 2012 Move it Find the message 80 Friday,
September 14, 2012 -
@sandimetz Sept 2012 Move it Find the message (Create the
object) 80 Friday, September 14, 2012 -
@sandimetz Sept 2012 Move it Find the message (Create the
object) Send the message 80 Friday, September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 Good News Calculations are isolated 95 Friday,
September 14, 2012 -
@sandimetz Sept 2012 Bad News Still have dependencies 96 Friday,
September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 Meta Knowledge Shock knows 1. cost method
2. the convention 105 Friday, September 14, 2012 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@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 -
@sandimetz Sept 2012 Reprise Find the message (Create the object)
Send the message 119 Friday, September 14, 2012 -
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