Slide 1

Slide 1 text

Don’t Hang Me Out to DRY Kevin Murphy @kevin_j_m

Slide 2

Slide 2 text

Don’t Hang Me Out to DRY Kevin Murphy @kevin_j_m

Slide 3

Slide 3 text

@kevin_j_m vory Tower
 nnovation TechnologY

Slide 4

Slide 4 text

@kevin_j_m T T

Slide 5

Slide 5 text

@kevin_j_m T

Slide 6

Slide 6 text

@kevin_j_m T our code has

Slide 7

Slide 7 text

@kevin_j_m T Make work

Slide 8

Slide 8 text

@kevin_j_m T Make work ☂

Slide 9

Slide 9 text

@kevin_j_m T Make T work Make right

Slide 10

Slide 10 text

@kevin_j_m T Make right ❌

Slide 11

Slide 11 text

@kevin_j_m T Make T T work Make Make right fast

Slide 12

Slide 12 text

@kevin_j_m T Make fast

Slide 13

Slide 13 text

@kevin_j_m T Make T T work Make Make right fast

Slide 14

Slide 14 text

@kevin_j_m

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

@kevin_j_m Kevin Murphy

Slide 17

Slide 17 text

@kevin_j_m T

Slide 18

Slide 18 text

@kevin_j_m Should see 4 testimonials today, but only 3 are seen.

Slide 19

Slide 19 text

@kevin_j_m def number_testimonials if mercury_retrograde? 4 elsif full_moon? 3 elsif tuesday? 2 else coin_flip end end

Slide 20

Slide 20 text

@kevin_j_m def number_testimonials if mercury_retrograde? 4 elsif full_moon? 3 elsif tuesday? 2 else coin_flip end end

Slide 21

Slide 21 text

@kevin_j_m Farmer’s Almanac Mercury in Retrograde OCT 31 NOV 20

Slide 22

Slide 22 text

@kevin_j_m Code Coverage

Slide 23

Slide 23 text

@kevin_j_m

Slide 24

Slide 24 text

@kevin_j_m it "shows 4 testimonials if mercury is in retrograde" do end

Slide 25

Slide 25 text

@kevin_j_m it "shows 4 testimonials if mercury is in retrograde" do allow(Mercury).to receive(:in_retrograde?).and_return(true) end

Slide 26

Slide 26 text

@kevin_j_m it "shows 4 testimonials if mercury is in retrograde" do allow(Mercury).to receive(:in_retrograde?).and_return(true) display = TestimonialDisplay.new end

Slide 27

Slide 27 text

@kevin_j_m it "shows 4 testimonials if mercury is in retrograde" do allow(Mercury).to receive(:in_retrograde?).and_return(true) display = TestimonialDisplay.new expect(display.number_testimonials).to eq 4 end

Slide 28

Slide 28 text

@kevin_j_m

Slide 29

Slide 29 text

@kevin_j_m class Mercury def self.in_retrograde?(date) end end

Slide 30

Slide 30 text

@kevin_j_m class Mercury def self.in_retrograde?(date) false end end

Slide 31

Slide 31 text

@kevin_j_m

Slide 32

Slide 32 text

@kevin_j_m Coverage is insufficient

Slide 33

Slide 33 text

@kevin_j_m TDD

Slide 34

Slide 34 text

@kevin_j_m Pair Programming

Slide 35

Slide 35 text

@kevin_j_m Mutation Testing

Slide 36

Slide 36 text

@kevin_j_m Code Review

Slide 37

Slide 37 text

@kevin_j_m QA

Slide 38

Slide 38 text

@kevin_j_m '

Slide 39

Slide 39 text

@kevin_j_m Coverage is insufficient

Slide 40

Slide 40 text

@kevin_j_m Coverage as a goal

Slide 41

Slide 41 text

@kevin_j_m Randomized with seed 46917 .F.. Failures: 1) TestimonialDisplay#number_testimonials will show 0 or 1 testimonial if no other conditions are met Failure/Error: expect(results.uniq).to match_array [0, 1] expected collection contained: [0, 1] actual collection contained: [1] the missing elements were: [0]

Slide 42

Slide 42 text

@kevin_j_m def number_testimonials if mercury_retrograde? 4 elsif full_moon? 3 elsif tuesday? 2 else coin_flip end end

Slide 43

Slide 43 text

@kevin_j_m def coin_flip rand(2) end

Slide 44

Slide 44 text

@kevin_j_m it "will show 0 or 1 testimonial" do end

Slide 45

Slide 45 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) end

Slide 46

Slide 46 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] end

Slide 47

Slide 47 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] display = TestimonialDisplay.new end

Slide 48

Slide 48 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] display = TestimonialDisplay.new travel_to waning_monday do end end

Slide 49

Slide 49 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] display = TestimonialDisplay.new travel_to waning_monday do 2.times do end end end

Slide 50

Slide 50 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] display = TestimonialDisplay.new travel_to waning_monday do 2.times do results << display.number_testimonials end end end

Slide 51

Slide 51 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] display = TestimonialDisplay.new travel_to waning_monday do 2.times do results << display.number_testimonials end end expect(results.uniq).to match_array [0, 1] end

Slide 52

Slide 52 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] display = TestimonialDisplay.new travel_to waning_monday do 2.times do results << display.number_testimonials end end expect(results.uniq).to match_array [0, 1] end

Slide 53

Slide 53 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] display = TestimonialDisplay.new travel_to waning_monday do 2.times do results << display.number_testimonials end end expect(results.uniq).to match_array [0, 1] end

Slide 54

Slide 54 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) results = [] display = TestimonialDisplay.new travel_to waning_monday do 200.times do results << display.number_testimonials end end expect(results.uniq).to match_array [0, 1] end

Slide 55

Slide 55 text

@kevin_j_m it "will show 0 or 1 testimonial" do waning_monday = Date.new(2019, 9, 16) display = TestimonialDisplay.new travel_to waning_monday do expect(results.uniq).to be_in [0, 1] end end

Slide 56

Slide 56 text

@kevin_j_m Vanity metric?

Slide 57

Slide 57 text

@kevin_j_m

Slide 58

Slide 58 text

@kevin_j_m Coverage is a signal

Slide 59

Slide 59 text

@kevin_j_m

Slide 60

Slide 60 text

@kevin_j_m

Slide 61

Slide 61 text

@kevin_j_m

Slide 62

Slide 62 text

@kevin_j_m

Slide 63

Slide 63 text

@kevin_j_m

Slide 64

Slide 64 text

@kevin_j_m Coverage Consideration Total Cost of Ownership

Slide 65

Slide 65 text

@kevin_j_m T

Slide 66

Slide 66 text

@kevin_j_m +

Slide 67

Slide 67 text

@kevin_j_m ☑ Dark Dungeon API Access Keys should follow same convention

Slide 68

Slide 68 text

@kevin_j_m it "does not provide a company key" do end

Slide 69

Slide 69 text

@kevin_j_m it "does not provide a company key" do generator = AccessKeyGenerator.new end

Slide 70

Slide 70 text

@kevin_j_m it "does not provide a company key" do generator = AccessKeyGenerator.new key = generator.access_key(accessor: :user, user_id: 0) end

Slide 71

Slide 71 text

@kevin_j_m it "does not provide a company key" do generator = AccessKeyGenerator.new key = generator.access_key(accessor: :user, user_id: 0) expect(key.match?(COMPANY_REGEX)).not_to eq true end

Slide 72

Slide 72 text

@kevin_j_m it "appends the user id if the user's id is odd" do generator = AccessKeyGenerator.new key = generator.access_key(accessor_type: :user, user_id: 1) expect(key).to end_with("-1") end it "does not provide a company key" do generator = AccessKeyGenerator.new key = generator.access_key(accessor: :user, user_id: 0) expect(key.match?(COMPANY_REGEX)).not_to eq true end it "raises an exception if it doesn't understand the accessor type" do generator = AccessKeyGenerator.new expect { generator.access_key(accessor_type: :foo) } .to raise_error UnknownAccessorType end end end

Slide 73

Slide 73 text

@kevin_j_m it "appends the user id if the user's id is odd" do generator = AccessKeyGenerator.new key = generator.access_key(accessor_type: :user, user_id: 1) expect(key).to end_with("-1") end it "does not provide a company key" do generator = AccessKeyGenerator.new key = generator.access_key(accessor: :user, user_id: 0) expect(key.match?(COMPANY_REGEX)).not_to eq true end it "raises an exception if it doesn't understand the accessor type" do generator = AccessKeyGenerator.new expect { generator.access_key(accessor_type: :foo) } .to raise_error UnknownAccessorType end end end

Slide 74

Slide 74 text

@kevin_j_m RSpec.describe AccessKeyGenerator do UUID_REGEX = /[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/ COMPANY_REGEX = /CO-[0-9a-fA-F]{8}/ describe "#access_key" do it "provides a base-64 encoded key if the key is for an acquired company" do generator = AccessKeyGenerator.new key = generator.access_key(accessor_type: :company, acquired_company: true) expect(key).to eq Base64.strict_encode64(Base64.decode64(key)) end it "creates an ivory tower company access key if it's a company not acquired" do generator = AccessKeyGenerator.new key = generator.access_key(accessor_type: :company, acquired_company: false) expect(key.match?(COMPANY_REGEX)).to eq true end it "does not provide a full UUID if the key is for a company" do generator = AccessKeyGenerator.new key = generator.access_key(accessor_type: :company)

Slide 75

Slide 75 text

@kevin_j_m RSpec.describe AccessKeyGenerator do UUID_REGEX = /[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/ COMPANY_REGEX = /CO-[0-9a-fA-F]{8}/ describe "#access_key" do it "provides a base-64 encoded key if the key is for an acquired company" do generator = AccessKeyGenerator.new key = generator.access_key(accessor_type: :company, acquired_company: true) expect(key).to eq Base64.strict_encode64(Base64.decode64(key)) end it "creates an ivory tower company access key if it's a company not acquired" do generator = AccessKeyGenerator.new key = generator.access_key(accessor_type: :company, acquired_company: false) expect(key.match?(COMPANY_REGEX)).to eq true end it "does not provide a full UUID if the key is for a company" do generator = AccessKeyGenerator.new key = generator.access_key(accessor_type: :company)

Slide 76

Slide 76 text

@kevin_j_m DRY Code Don’t Repeat Yourself

Slide 77

Slide 77 text

@kevin_j_m DAMP Code Descriptive And Meaningful Phrases

Slide 78

Slide 78 text

@kevin_j_m it "does not provide a company key" do generator = AccessKeyGenerator.new key = generator.access_key(accessor: :user, user_id: 0) expect(key.match?(COMPANY_REGEX)).not_to eq true end

Slide 79

Slide 79 text

@kevin_j_m it "does not provide a company key" do company_regex = /CO-[0-9a-fA-F]{8}/ generator = AccessKeyGenerator.new key = generator.access_key(accessor: :user, user_id: 0) expect(key.match?(COMPANY_REGEX)).not_to eq true end

Slide 80

Slide 80 text

@kevin_j_m it "does not provide a company key" do company_regex = /CO-[0-9a-fA-F]{8}/ generator = AccessKeyGenerator.new key = generator.access_key(accessor: :user, user_id: 0) expect(key.match?(company_regex)).not_to eq true end

Slide 81

Slide 81 text

@kevin_j_m T

Slide 82

Slide 82 text

@kevin_j_m class AccessKeyGenerator def access_key(accessor:, user_id: nil) if accessor == :company "CO-#{SecureRandom.hex(8)}" elsif accessor == :user uuid = SecureRandom.uuid user_id.odd? ? uuid << “-#{user_id}" : uuid end end end

Slide 83

Slide 83 text

@kevin_j_m class AccessKeyGenerator def access_key(accessor:, acquired_co: nil, user_id: nil) if accessor == :company "CO-#{SecureRandom.hex(8)}" elsif accessor == :user uuid = SecureRandom.uuid user_id.odd? ? uuid << “-#{user_id}" : uuid end end end

Slide 84

Slide 84 text

@kevin_j_m class AccessKeyGenerator def access_key(accessor:, acquired_co: nil, user_id: nil) if accessor == :company if acquired_co SecureRandom.base64 else “CO-#{SecureRandom.hex(8)}” end elsif accessor == :user uuid = SecureRandom.uuid user_id.odd? ? uuid << “-#{user_id}" : uuid end end end

Slide 85

Slide 85 text

@kevin_j_m DRY Code?

Slide 86

Slide 86 text

@kevin_j_m class User < ApplicationRecord def generate_access_key SecureRandom.uuid end end

Slide 87

Slide 87 text

@kevin_j_m class Company < ApplicationRecord def generate_access_key SecureRandom.uuid end end

Slide 88

Slide 88 text

@kevin_j_m

Slide 89

Slide 89 text

@kevin_j_m class AccessKeyGenerator def access_key SecureRandom.uuid end end

Slide 90

Slide 90 text

@kevin_j_m class AccessKeyGenerator def access_key(accessor:, user_id: nil) if accessor == :company "CO-#{SecureRandom.hex(8)}" elsif accessor == :user uuid = SecureRandom.uuid user_id.odd? ? uuid << “-#{user_id}" : uuid end end end

Slide 91

Slide 91 text

@kevin_j_m WET Code Write Everything Twice

Slide 92

Slide 92 text

@kevin_j_m

Slide 93

Slide 93 text

@kevin_j_m class User < ApplicationRecord def generate_access_key SecureRandom.uuid end end

Slide 94

Slide 94 text

@kevin_j_m class User < ApplicationRecord def generate_access_key SecureRandom.uuid end end class Company < ApplicationRecord def generate_access_key SecureRandom.uuid end end

Slide 95

Slide 95 text

@kevin_j_m class User < ApplicationRecord def generate_access_key if id.odd? "#{SecureRandom.uuid}-#{id}" else SecureRandom.uuid end end end

Slide 96

Slide 96 text

@kevin_j_m class User < ApplicationRecord def generate_access_key if id.odd? "#{SecureRandom.uuid}-#{id}" else SecureRandom.uuid end end end class Company < ApplicationRecord def generate_access_key if acquisition? SecureRandom.base64 else "CO-#{SecureRandom.hex(8)}" end end end

Slide 97

Slide 97 text

@kevin_j_m DRY Consideration Flexibility

Slide 98

Slide 98 text

@kevin_j_m T

Slide 99

Slide 99 text

@kevin_j_m Passing “15” to API provides wrong result

Slide 100

Slide 100 text

@kevin_j_m Performant Code

Slide 101

Slide 101 text

@kevin_j_m Rust

Slide 102

Slide 102 text

@kevin_j_m gem 'helix-rails', '~> 0.5.0'

Slide 103

Slide 103 text

@kevin_j_m def generate(bound: i32)

Slide 104

Slide 104 text

@kevin_j_m def generate(bound: i32) -> Vec { }

Slide 105

Slide 105 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); }

Slide 106

Slide 106 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { } }

Slide 107

Slide 107 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { }) } }

Slide 108

Slide 108 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), }) } }

Slide 109

Slide 109 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), }) } }

Slide 110

Slide 110 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), (_, 0) => "Buzz".to_string(), }) } }

Slide 111

Slide 111 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), (_, 0) => "Buzz".to_string(), (2, 4) => "Buzz".to_string(), }) } }

Slide 112

Slide 112 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), (_, 0) => "Buzz".to_string(), (2, 4) => "Buzz".to_string(), (_, _) => x.to_string(), }) } }

Slide 113

Slide 113 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), (_, 0) => "Buzz".to_string(), (2, 4) => "Buzz".to_string(), (_, _) => x.to_string(), }) } results }

Slide 114

Slide 114 text

@kevin_j_m FBAAS

Slide 115

Slide 115 text

@kevin_j_m FBAAS FizzBuzz As A Service

Slide 116

Slide 116 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), (_, 0) => "Buzz".to_string(), (2, 4) => "Buzz".to_string(), (_, _) => x.to_string(), }) } results }

Slide 117

Slide 117 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), (_, 0) => "Buzz".to_string(), (2, 4) => "Buzz".to_string(), (_, _) => x.to_string(), }) } results }

Slide 118

Slide 118 text

@kevin_j_m def generate(bound: i32) -> Vec { let mut results = Vec::new(); for x in 1..(bound+1) { results.push(match(x % 3, x % 5) { (0, 0) => "FizzBuzz".to_string(), (0, _) => "Fizz".to_string(), (_, 0) => "Buzz".to_string(), (_, _) => x.to_string(), }) } results }

Slide 119

Slide 119 text

@kevin_j_m Performant Consideration Weigh observed behavior & maintenance

Slide 120

Slide 120 text

@kevin_j_m T Make T T work Make Make right fast

Slide 121

Slide 121 text

@kevin_j_m T Make work for confidence, not metrics

Slide 122

Slide 122 text

@kevin_j_m T Make right knowing “right” is subjective

Slide 123

Slide 123 text

@kevin_j_m T Make fast when you know it’s warranted

Slide 124

Slide 124 text

@kevin_j_m https://www.thegnar.co/rubyconf https://github.com/kevin-j-m/ivory-tower T

Slide 125

Slide 125 text

@kevin_j_m https://www.thegnar.co/rubyconf