47
The Boy Scout Rule Alistair McKinnell @amckinnell Nulogy

The Boy Scout Rule

Embed Size (px)

Citation preview

Page 1: The Boy Scout Rule

The Boy Scout Rule

Alistair McKinnell@amckinnell

Nulogy

Page 2: The Boy Scout Rule
Page 3: The Boy Scout Rule

Imagine that your codebase gets a little

better everyday

Page 4: The Boy Scout Rule

It’s up to you

Page 5: The Boy Scout Rule

It’s up to you(and your teammates)

Page 6: The Boy Scout Rule

The Boy Scout Rule

Page 7: The Boy Scout Rule

Always check a module in cleaner than when you checked it out

Page 8: The Boy Scout Rule

Know Your Language

Page 9: The Boy Scout Rule

def apply_other_filters(params) case params[:has_production] when "yes" conditions = ["produced_uom > 0"] when "no" conditions = ["produced_uom = 0"] else conditions = [] end conditions end

Page 10: The Boy Scout Rule

def apply_other_filters(params) case params[:has_production] when "yes" conditions = ["produced_uom > 0"] when "no" conditions = ["produced_uom = 0"] else conditions = [] end conditions end

conditions

conditions

conditions

conditions

Page 11: The Boy Scout Rule

def apply_other_filters(params) case params[:has_production] when "yes" ["produced_uom > 0"] when "no" ["produced_uom = 0"] else [] end end

Page 12: The Boy Scout Rule

def get_number_of_lines reader = open_csv_reader reader.shift

lines = 0 reader.each do |row| lines += 1 end

lines end

Page 13: The Boy Scout Rule

def get_number_of_lines reader = open_csv_reader reader.shift

lines = 0 reader.each do |row| lines += 1 end

lines end

lines = 0 reader.each do |row| lines += 1 end

lines

Page 14: The Boy Scout Rule

def get_number_of_lines reader = open_csv_reader reader.shift

end reader.count

Page 15: The Boy Scout Rule

def repeat_commas(length) str = "" length.times { |i| str << ',' } str end

Page 16: The Boy Scout Rule

def repeat_commas(length) str = "" length.times { |i| str << ',' } str end

str str str

Page 17: The Boy Scout Rule

def repeat_commas(length) (1..length).map { ',' }.join end

Page 18: The Boy Scout Rule

def repeat_commas(length) ',' * length end

Page 19: The Boy Scout Rule

',' * length

Page 20: The Boy Scout Rule

def create @organization = Organization.new(org_params)

validate_org :new end

def update load_organization @organization.attributes = org_params

validate_org :edit end

Page 21: The Boy Scout Rule

def create @organization = Organization.new(org_params)

validate_org :new end

def update load_organization @organization.attributes = org_params

validate_org :edit end

validate_org :new

validate_org :edit

Page 22: The Boy Scout Rule

validate_org(failure_action: :new)

validate_org(failure_action: :edit)

def create @organization = Organization.new(org_params)

end

def update load_organization @organization.attributes = org_params

end

Page 23: The Boy Scout Rule

def assign_attributes(product, container) product.code = container.code product.desc = container.desc product.identifier = container.identifier product end

Page 24: The Boy Scout Rule

def assign_attributes(product, container) product.code = container.code product.desc = container.desc product.identifier = container.identifier product end

product product product product

Page 25: The Boy Scout Rule

def assign_attributes(product, container) product.code = container.code product.desc = container.desc product.identifier = container.identifier product end

def assign_attributes(product, container) product.tap do |p| p.code = container.code p.desc = container.desc p.identifier = container.identifier end end

product product product product product.tap

Page 26: The Boy Scout Rule

def assign_attributes(product, container) product.code = container.code product.desc = container.desc product.identifier = container.identifier product end

def assign_attributes(product, container) product.tap do |p| p.code = container.code p.desc = container.desc p.identifier = container.identifier end end

Page 27: The Boy Scout Rule

Know Your Language

Page 28: The Boy Scout Rule

expect(po_item.due).to eq(time('15-01-01')) expect(po_item.price_per_unit).to eq(19) expect(po_item.quantity).to eq(14)

Page 29: The Boy Scout Rule

expect(po_item.due).to eq(time('15-01-01')) expect(po_item.price_per_unit).to eq(19) expect(po_item.quantity).to eq(14)

po_item po_item po_item

Page 30: The Boy Scout Rule

expect(po_item.due).to eq(time('15-01-01')) expect(po_item.price_per_unit).to eq(19) expect(po_item.quantity).to eq(14)

expect(po_item).to have_attributes( due: time('15-01-01'), price_per_unit: 19, quantity: 14 )

po_item po_item po_item

po_item

Page 31: The Boy Scout Rule

expect(po_item.due).to eq(time('15-01-01')) expect(po_item.price_per_unit).to eq(19) expect(po_item.quantity).to eq(14)

expect(po_item).to have_attributes( due: time('15-01-01'), price_per_unit: 19, quantity: 14 )

Page 32: The Boy Scout Rule

Know Your Library

Page 33: The Boy Scout Rule

def serializer DEPENDENCIES[type][:serializer] end

Page 34: The Boy Scout Rule

def serializer DEPENDENCIES[type][:serializer] end [:serializer]

Page 35: The Boy Scout Rule

def serializer DEPENDENCIES[type] end .fetch(:serializer)

Page 36: The Boy Scout Rule

Fail Fast

Page 37: The Boy Scout Rule

def number "#{order.number}-#{order_item.number}" end

Page 38: The Boy Scout Rule
Page 39: The Boy Scout Rule

Remove Dead Code

Page 40: The Boy Scout Rule

The Boy Scout Rule

Page 41: The Boy Scout Rule

Always check a module in cleaner than when you checked it out

Page 42: The Boy Scout Rule

Know Your LanguageKnow Your Library

Fail FastRemove Dead Code

Page 43: The Boy Scout Rule

When I look at my code from six months ago

I expect to see ways to improve it

Page 44: The Boy Scout Rule

Put your learning and understanding back

into the code

Page 45: The Boy Scout Rule
Page 46: The Boy Scout Rule
Page 47: The Boy Scout Rule

It’s up to you