Slide 1

Slide 1 text

May 2016 @sandimetz Sandi Metz Get a Whiff of This Sandi Metz

Slide 2

Slide 2 text

@sandimetz May 2016 Kent Beck

Slide 3

Slide 3 text

@sandimetz May 2016 Kent Beck Martin Fowler

Slide 4

Slide 4 text

@sandimetz May 2016 Kent Beck Martin Fowler

Slide 5

Slide 5 text

@sandimetz May 2016 Classic Smells

Slide 6

Slide 6 text

@sandimetz May 2016 Classic Smells Alternative Classes w/ Different Interfaces Comments Data Class Data Clumps Divergent Change Duplicated Code Feature Envy Parallel Inheritance Hierarchies Primitive Obsession Refused Bequest Shotgun Surgery Speculative Generality Switch Statements Temporary Field Inappropriate Intimacy Incomplete Library Client Large Class Long Method Long Parameter List Message Chains Middle Man Lazy Class

Slide 7

Slide 7 text

@sandimetz May 2016

Slide 8

Slide 8 text

@sandimetz May 2016 Alternative Classes w/ Different Interfaces Comments Data Class Data Clumps Divergent Change Duplicated Code Feature Envy Parallel Inheritance Hierarchies Primitive Obsession Refused Bequest Shotgun Surgery Speculative Generality Switch Statements Temporary Field Classic Smells Inappropriate Intimacy Incomplete Library Client Large Class Long Method Long Parameter List Message Chains Middle Man Lazy Class

Slide 9

Slide 9 text

@sandimetz May 2016 Alternative Classes w/ Different Interfaces Comments Data Class Data Clumps Divergent Change Duplicated Code Feature Envy Parallel Inheritance Hierarchies Primitive Obsession Refused Bequest Shotgun Surgery Speculative Generality Switch Statements Temporary Field Classic Smells Inappropriate Intimacy Incomplete Library Client Long Method Long Parameter List Message Chains Middle Man Lazy Class Large Class

Slide 10

Slide 10 text

@sandimetz May 2016 Alternative Classes w/ Different Interfaces Data Class Data Clumps Divergent Change Duplicated Code Feature Envy Parallel Inheritance Hierarchies Primitive Obsession Refused Bequest Shotgun Surgery Speculative Generality Switch Statements Temporary Field Classic Smells Inappropriate Intimacy Long Method Long Parameter List Message Chains Middle Man Lazy Class Large Class

Slide 11

Slide 11 text

@sandimetz May 2016 Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field Feature Envy Inappropriate Intimacy Message Chains Middle Man Data Class Duplicated Code Speculative Generality Lazy Class Data Clumps Primitive Obsession Long Method Long Parameter List Large Class

Slide 12

Slide 12 text

@sandimetz May 2016 Data Clumps Primitive Obsession Long Method Long Parameter List Large Class

Slide 13

Slide 13 text

@sandimetz May 2016 Data Clumps Primitive Obsession Long Method Long Parameter List Large Class

Slide 14

Slide 14 text

@sandimetz May 2016 Bloaters Data Clumps Primitive Obsession Long Method Long Parameter List Large Class

Slide 15

Slide 15 text

@sandimetz May 2016 Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery Feature Envy Inappropriate Intimacy Message Chains Middle Man Data Class Duplicated Code Speculative Generality Lazy Class Data Clumps Primitive Obsession Long Method Long Parameter List Large Class Bloaters

Slide 16

Slide 16 text

@sandimetz May 2016 Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field

Slide 17

Slide 17 text

@sandimetz May 2016 Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field

Slide 18

Slide 18 text

@sandimetz May 2016 Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field

Slide 19

Slide 19 text

@sandimetz May 2016 Tool Abusers Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field

Slide 20

Slide 20 text

@sandimetz May 2016 Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field Feature Envy Inappropriate Intimacy Message Chains Middle Man Data Class Duplicated Code Speculative Generality Lazy Class Data Clumps Primitive Obsession Long Method Long Parameter List Large Class Bloaters Abusers Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery

Slide 21

Slide 21 text

@sandimetz May 2016 Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery

Slide 22

Slide 22 text

@sandimetz May 2016 Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery

Slide 23

Slide 23 text

@sandimetz May 2016 Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery

Slide 24

Slide 24 text

@sandimetz May 2016 Change Preventers Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery

Slide 25

Slide 25 text

@sandimetz May 2016 Data Class Duplicated Code Speculative Generality Lazy Class Feature Envy Inappropriate Intimacy Message Chains Middle Man Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field Data Clumps Primitive Obsession Long Method Long Parameter List Large Class Bloaters Abusers Preventers

Slide 26

Slide 26 text

@sandimetz May 2016 Data Class Duplicated Code Speculative Generality Lazy Class

Slide 27

Slide 27 text

@sandimetz May 2016 Data Class Duplicated Code Speculative Generality Lazy Class

Slide 28

Slide 28 text

@sandimetz May 2016 Data Class Duplicated Code Speculative Generality Lazy Class

Slide 29

Slide 29 text

@sandimetz May 2016 Dispensables Data Class Duplicated Code Speculative Generality Lazy Class

Slide 30

Slide 30 text

@sandimetz May 2016 Feature Envy Inappropriate Intimacy Message Chains Middle Man Data Class Duplicated Code Speculative Generality Lazy Class Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field Data Clumps Primitive Obsession Long Method Long Parameter List Large Class Bloaters Abusers Preventers Dispensables

Slide 31

Slide 31 text

@sandimetz May 2016 Feature Envy Inappropriate Intimacy Message Chains Middle Man

Slide 32

Slide 32 text

@sandimetz May 2016 Feature Envy Inappropriate Intimacy Message Chains Middle Man

Slide 33

Slide 33 text

@sandimetz May 2016 Feature Envy Inappropriate Intimacy Message Chains Middle Man

Slide 34

Slide 34 text

@sandimetz May 2016 Couplers Feature Envy Inappropriate Intimacy Message Chains Middle Man

Slide 35

Slide 35 text

@sandimetz May 2016 Feature Envy Inappropriate Intimacy Message Chains Middle Man Data Class Duplicated Code Speculative Generality Lazy Class Divergent Change Parallel Inheritance Hierarchies Shotgun Surgery Alternative Classes w/ Different Interfaces Refused Bequest Switch Statements Temporary Field Data Clumps Primitive Obsession Long Method Long Parameter List Large Class Bloaters Abusers Preventers Dispensables Couplers

Slide 36

Slide 36 text

@sandimetz May 2016 Refactoring

Slide 37

Slide 37 text

@sandimetz May 2016 Refactoring Recipe

Slide 38

Slide 38 text

@sandimetz May 2016 Fowler 149: Extract Class

Slide 39

Slide 39 text

@sandimetz May 2016 Refactoring Recipe

Slide 40

Slide 40 text

@sandimetz May 2016 Code Smell Curative Refactoring Recipe

Slide 41

Slide 41 text

@sandimetz May 2016 http://www.industriallogic.com/wp-content/uploads/2005/09/smellstorefactorings.pdf

Slide 42

Slide 42 text

@sandimetz May 2016 http://www.industriallogic.com/wp-content/uploads/2005/09/smellstorefactorings.pdf

Slide 43

Slide 43 text

@sandimetz May 2016

Slide 44

Slide 44 text

@sandimetz May 2016 1

Slide 45

Slide 45 text

@sandimetz May 2016 class Sale < Persistence end

Slide 46

Slide 46 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 47

Slide 47 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 48

Slide 48 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 49

Slide 49 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 50

Slide 50 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 51

Slide 51 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 52

Slide 52 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 53

Slide 53 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 54

Slide 54 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 55

Slide 55 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 56

Slide 56 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 57

Slide 57 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 58

Slide 58 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 59

Slide 59 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 60

Slide 60 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 61

Slide 61 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 62

Slide 62 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 63

Slide 63 text

@sandimetz May 2016 class Sale < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 64

Slide 64 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 65

Slide 65 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 66

Slide 66 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 67

Slide 67 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 68

Slide 68 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 69

Slide 69 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 70

Slide 70 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 71

Slide 71 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end Kaboom

Slide 72

Slide 72 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 73

Slide 73 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 74

Slide 74 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 75

Slide 75 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 76

Slide 76 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 77

Slide 77 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 78

Slide 78 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 79

Slide 79 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 80

Slide 80 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 81

Slide 81 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 82

Slide 82 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 83

Slide 83 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Larry def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Curly def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Mo def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 84

Slide 84 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Larry def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Curly def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Mo def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Fee def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Fi def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Foe def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 85

Slide 85 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Larry def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Curly def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Mo def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Fee def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Fi def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Foe def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 86

Slide 86 text

@sandimetz May 2016

Slide 87

Slide 87 text

@sandimetz May 2016 2 Data Clump | Extract Class

Slide 88

Slide 88 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 89

Slide 89 text

@sandimetz May 2016 class Sale < Persistence end class Expense < Persistence end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 90

Slide 90 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 91

Slide 91 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 92

Slide 92 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 93

Slide 93 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 94

Slide 94 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 95

Slide 95 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 96

Slide 96 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 97

Slide 97 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 98

Slide 98 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 99

Slide 99 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Foo def sales_total(params) Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 100

Slide 100 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 101

Slide 101 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 102

Slide 102 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end

Slide 103

Slide 103 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 104

Slide 104 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Bar def weekly_sales_total(params) start_date = (Date.parse(params[:starting])) end_date = start_date + 6 Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 105

Slide 105 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Bar def weekly_sales_total(params) Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 106

Slide 106 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: (start_date..end_date)).sum("cost") end end

Slide 107

Slide 107 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end

Slide 108

Slide 108 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end

Slide 109

Slide 109 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 110

Slide 110 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Baz def expense_total(params) start_date = (Date.parse(params[:starting])) rescue Date.today end_date = (Date.parse(params[:ending])) rescue start_date Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 111

Slide 111 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Baz def expense_total(params) Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 112

Slide 112 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: (Date.parse(params[:starting]))..(Date.parse(params[:ending]))).sum("cost") end end

Slide 113

Slide 113 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 114

Slide 114 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 115

Slide 115 text

@sandimetz May 2016 class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 116

Slide 116 text

@sandimetz May 2016 class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 117

Slide 117 text

@sandimetz May 2016 class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end Data Clump | Extract Class

Slide 118

Slide 118 text

@sandimetz May 2016 3 Message Chain | Hide Delegate

Slide 119

Slide 119 text

@sandimetz May 2016 class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 120

Slide 120 text

@sandimetz May 2016 class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 121

Slide 121 text

@sandimetz May 2016 Foo Sale.where(date: range).sum("cost")

Slide 122

Slide 122 text

@sandimetz May 2016 Foo Sale Sale.where(date: range).sum("cost")

Slide 123

Slide 123 text

@sandimetz May 2016 Foo Sale where Sale.where(date: range).sum("cost")

Slide 124

Slide 124 text

@sandimetz May 2016 Foo Sale Persistence where Sale.where(date: range).sum("cost")

Slide 125

Slide 125 text

@sandimetz May 2016 Foo Sale Persistence where where Sale.where(date: range).sum("cost")

Slide 126

Slide 126 text

@sandimetz May 2016 Foo Sale Persistence where where Sale.where(date: range).sum("cost")

Slide 127

Slide 127 text

@sandimetz May 2016 Foo Sale Persistence where where List Sale.where(date: range).sum("cost")

Slide 128

Slide 128 text

@sandimetz May 2016 Foo Sale Persistence where where List Sale.where(date: range).sum("cost") ??? ??? ???

Slide 129

Slide 129 text

@sandimetz May 2016 Foo Sale Persistence where where List Sale.where(date: range).sum("cost") ??? ??? ???

Slide 130

Slide 130 text

@sandimetz May 2016 Foo Sale Persistence where where List Sale.where(date: range).sum("cost") ??? ??? ???

Slide 131

Slide 131 text

@sandimetz May 2016 Foo Sale Persistence where where List a list Sale.where(date: range).sum("cost") ??? ??? ???

Slide 132

Slide 132 text

@sandimetz May 2016 Foo Sale Persistence where where List a list a list Sale.where(date: range).sum("cost") ??? ??? ???

Slide 133

Slide 133 text

@sandimetz May 2016 Foo Sale Persistence where where List sum a list a list Sale.where(date: range).sum("cost") ??? ??? ???

Slide 134

Slide 134 text

@sandimetz May 2016 Foo

Slide 135

Slide 135 text

@sandimetz May 2016 DateRange Foo

Slide 136

Slide 136 text

@sandimetz May 2016 Sale #where DateRange Foo

Slide 137

Slide 137 text

@sandimetz May 2016 Sale #where DateRange Foo Immediate Collaborators

Slide 138

Slide 138 text

@sandimetz May 2016 List #sum Sale #where DateRange Foo

Slide 139

Slide 139 text

@sandimetz May 2016 List #sum Sale #where DateRange Foo

Slide 140

Slide 140 text

@sandimetz May 2016 List #sum Sale #where DateRange Foo Sale.where(date: range).sum("cost")

Slide 141

Slide 141 text

@sandimetz May 2016 List #sum Sale #where DateRange Foo I know my collaborators collaborators

Slide 142

Slide 142 text

@sandimetz May 2016 List #sum Sale #where DateRange Foo This is gonna hurt

Slide 143

Slide 143 text

@sandimetz May 2016 class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 144

Slide 144 text

@sandimetz May 2016 class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 145

Slide 145 text

@sandimetz May 2016 class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 146

Slide 146 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 147

Slide 147 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 148

Slide 148 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end

Slide 149

Slide 149 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.where(date: range).sum("cost") end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end

Slide 150

Slide 150 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end

Slide 151

Slide 151 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.where(date: range).sum("cost") end end

Slide 152

Slide 152 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end

Slide 153

Slide 153 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end

Slide 154

Slide 154 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end

Slide 155

Slide 155 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end

Slide 156

Slide 156 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 157

Slide 157 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.where(date: range).sum("cost") end end

Slide 158

Slide 158 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.total(within: range) end end

Slide 159

Slide 159 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.total(within: range) end end

Slide 160

Slide 160 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.total(within: range) end end

Slide 161

Slide 161 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.total(within: range) end end

Slide 162

Slide 162 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end class Bar def weekly_sales_total(params) range = DateRange.new(starting: params[:starting]).week_range Sale.total(within: range) end end class Baz def expense_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Expense.total(within: range) end end Message Chain | Hide Delegate

Slide 163

Slide 163 text

@sandimetz May 2016 4 Sidebar: How Much Reality?

Slide 164

Slide 164 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end

Slide 165

Slide 165 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end

Slide 166

Slide 166 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end

Slide 167

Slide 167 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end

Slide 168

Slide 168 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 169

Slide 169 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 170

Slide 170 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 171

Slide 171 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end Check the query result

Slide 172

Slide 172 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 173

Slide 173 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 174

Slide 174 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 175

Slide 175 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end Depend on Sale class?

Slide 176

Slide 176 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 177

Slide 177 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 178

Slide 178 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end Depend on Totalizable role!

Slide 179

Slide 179 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 180

Slide 180 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 181

Slide 181 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range Sale.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 182

Slide 182 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 183

Slide 183 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 184

Slide 184 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 185

Slide 185 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 186

Slide 186 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 187

Slide 187 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end Two players of the Totalizable role

Slide 188

Slide 188 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 189

Slide 189 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 190

Slide 190 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 191

Slide 191 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} assert_equal 28, Foo.new.sales_total(starting: "2016-04-01", ending: "2016-04-07") end end

Slide 192

Slide 192 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} assert_equal 28, Foo.new.sales_total(params) end end

Slide 193

Slide 193 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} model = TotalizableDouble assert_equal 28, Foo.new.sales_total(params) end end

Slide 194

Slide 194 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} model = TotalizableDouble assert_equal 28, Foo.new.sales_total(params) end end

Slide 195

Slide 195 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} model = TotalizableDouble assert_equal 28, Foo.new.sales_total(params, model) end end

Slide 196

Slide 196 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} model = TotalizableDouble assert_equal 28, Foo.new.sales_total(params, model) end end

Slide 197

Slide 197 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} model = TotalizableDouble assert_equal 47, Foo.new.sales_total(params, model) end end

Slide 198

Slide 198 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} model = TotalizableDouble assert_equal 47, Foo.new.sales_total(params, model) end end FAST

Slide 199

Slide 199 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # Slow Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} model = TotalizableDouble assert_equal 47, Foo.new.sales_total(params, model) end end

Slide 200

Slide 200 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end # FAST Test class TotalizableDouble def self.total(within:) 47 end end class FooTest < Minitest::Test def test_sales_total params = {:starting => "2016-04-01", :ending => "2016-04-07"} model = TotalizableDouble assert_equal 47, Foo.new.sales_total(params, model) end end

Slide 201

Slide 201 text

@sandimetz May 2016 5 Duplicated Code | Pull Up Method

Slide 202

Slide 202 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end

Slide 203

Slide 203 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end

Slide 204

Slide 204 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end

Slide 205

Slide 205 text

@sandimetz May 2016 class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end

Slide 206

Slide 206 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end

Slide 207

Slide 207 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end

Slide 208

Slide 208 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end

Slide 209

Slide 209 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end Speculative Generality

Slide 210

Slide 210 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence def self.total(within:) where(date: within).sum("cost") end end class Expense < Persistence def self.total(within:) where(date: within).sum("cost") end end Dependency Injection Is Awesome

Slide 211

Slide 211 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence end class Expense < Persistence end

Slide 212

Slide 212 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence extend Totalable end class Expense < Persistence extend Totalable end

Slide 213

Slide 213 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence extend Totalable end class Expense < Persistence extend Totalable end

Slide 214

Slide 214 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence extend Totalable end class Expense < Persistence extend Totalable end Duplicated Code | Pull Up Method

Slide 215

Slide 215 text

@sandimetz May 2016 Code Smell Curative Refactoring Recipe

Slide 216

Slide 216 text

@sandimetz May 2016

Slide 217

Slide 217 text

@sandimetz May 2016 class DateRange attr_reader :starting, :ending def initialize(starting:, ending: nil) @starting = Date.parse(starting) rescue Date.today @ending = Date.parse(ending) rescue @starting end def range starting..ending end def week_range starting..(starting + 6) end end

Slide 218

Slide 218 text

@sandimetz May 2016 module Totalable def total(within:, date_field: :date, on: "cost") where({date_field => within}).sum(on) end end class Sale < Persistence extend Totalable end class Expense < Persistence extend Totalable end

Slide 219

Slide 219 text

@sandimetz May 2016 class Foo def sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end class Bar def weekly_sales_total(params, model=Sale) range = DateRange.new(starting: params[:starting]).week_range model.total(within: range) end end class Baz def expense_total(params, model=Expense) range = DateRange.new(starting: params[:starting], ending: params[:ending]).range model.total(within: range) end end

Slide 220

Slide 220 text

@sandimetz May 2016 Fist Pump

Slide 221

Slide 221 text

@sandimetz May 2016 Fist Pump

Slide 222

Slide 222 text

@sandimetz May 2016

Slide 223

Slide 223 text

@sandimetz May 2016 Reek

Slide 224

Slide 224 text

@sandimetz May 2016

Slide 225

Slide 225 text

@sandimetz May 2016

Slide 226

Slide 226 text

@sandimetz May 2016

Slide 227

Slide 227 text

@sandimetz May 2016

Slide 228

Slide 228 text

@sandimetz Thanks May 2016

Slide 229

Slide 229 text

@sandimetz References May 2016 http://martinfowler.com/books/refactoring.html http://www.refactoringinruby.info/ Kevin Rutherford, William Wake Mäntylä, M. V. and Lassenius, C. "Subjective Evaluation of Software Evolvability Using Code Smells: An Empirical Study". Journal of Empirical Software Engineering, vol. 11, no. 3, 2006, pp. 395-431. https://github.com/troessner/reek https://blog.codeship.com/how-to-find-ruby-code-smells-with-reek

Slide 230

Slide 230 text

@sandimetz Credits May 2016 Coffee on a Winter's morning, Stefan Lins, https://www.flickr.com/photos/mrlins/8533102693/ Royal Australian Air Force in Australia during World War II, Argus Newspaper Collection of Photographs, State Library of Victoria, https://www.flickr.com/photos/statelibraryofvictoria_collections/16554906225/ Chevy Crusher, Ruby Eng , https://www.flickr.com/photos/mac-ash/4105522760/ Misuse Makes Mishaps, Jeremy Brooks , https://www.flickr.com/photos/jeremybrooks/2205135193, resistance is futile, Lora, https://www.flickr.com/photos/laroyo/6871150550/ Well this is pointless, Janet McKnight, https://www.flickr.com/photos/janetmck/2613441853/ Glue goo 3, Sam-Cat, https://www.flickr.com/photos/samcatchesides/3326878644/ poses of victory, Daniel Novta, https://www.flickr.com/photos/vanf/7304661962/ Odorous smells!, auddess, https://www.flickr.com/photos/auddess/4744679975 EXPLORE] Smell the Flowers, Christine und David Schmitt, https://www.flickr.com/photos/cheesy42/8624536219/ PIZZA, Tobi Gaulke. https://www.flickr.com/photos/gato-gato-gato/15958062095/ Ain't Life Grand, Anne Worner, https://www.flickr.com/photos/wefi_official/9305161946/

Slide 231

Slide 231 text

@sandimetz http://poodr.com May 2016

Slide 232

Slide 232 text

@sandimetz http://99bottlesbook.com May 2016

Slide 233

Slide 233 text

@sandimetz Sandi Metz @sandimetz http://sandimetz.com May 2016

Slide 234

Slide 234 text

@sandimetz Questions? May 2016

Slide 235

Slide 235 text

@sandimetz Sandi Metz @sandimetz http://sandimetz.com May 2016