Post on 17-Feb-2017
transcript
The Boy Scout Rule
Alistair McKinnell@amckinnell
Nulogy
Imagine that your codebase gets a little
better everyday
It’s up to you
It’s up to you(and your teammates)
The Boy Scout Rule
Always check a module in cleaner than when you checked it out
Know Your Language
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
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
def apply_other_filters(params) case params[:has_production] when "yes" ["produced_uom > 0"] when "no" ["produced_uom = 0"] else [] end end
def get_number_of_lines reader = open_csv_reader reader.shift
lines = 0 reader.each do |row| lines += 1 end
lines end
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
def get_number_of_lines reader = open_csv_reader reader.shift
end reader.count
def repeat_commas(length) str = "" length.times { |i| str << ',' } str end
def repeat_commas(length) str = "" length.times { |i| str << ',' } str end
str str str
def repeat_commas(length) (1..length).map { ',' }.join end
def repeat_commas(length) ',' * length end
',' * length
def create @organization = Organization.new(org_params)
validate_org :new end
def update load_organization @organization.attributes = org_params
validate_org :edit end
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
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
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.code = container.code product.desc = container.desc product.identifier = container.identifier product end
product product product product
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
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
Know Your Language
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.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
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
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 )
Know Your Library
def serializer DEPENDENCIES[type][:serializer] end
def serializer DEPENDENCIES[type][:serializer] end [:serializer]
def serializer DEPENDENCIES[type] end .fetch(:serializer)
Fail Fast
def number "#{order.number}-#{order_item.number}" end
Remove Dead Code
The Boy Scout Rule
Always check a module in cleaner than when you checked it out
Know Your LanguageKnow Your Library
Fail FastRemove Dead Code
When I look at my code from six months ago
I expect to see ways to improve it
Put your learning and understanding back
into the code
It’s up to you