Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Boy Scout Rule

395 views

Published on

Imagine that your codebase get a little better everyday. It can if you (and your teammates) apply The Boy Scout Rule.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

The Boy Scout Rule

  1. 1. The Boy Scout Rule Alistair McKinnell @amckinnell Nulogy
  2. 2. Imagine that your codebase gets a little better everyday
  3. 3. It’s up to you
  4. 4. It’s up to you (and your teammates)
  5. 5. The Boy Scout Rule
  6. 6. Always check a module
 in cleaner than when 
 you checked it out
  7. 7. KnowYour Language
  8. 8. 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
  9. 9. 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
  10. 10. def apply_other_filters(params) case params[:has_production] when "yes" ["produced_uom > 0"] when "no" ["produced_uom = 0"] else [] end end
  11. 11. def get_number_of_lines reader = open_csv_reader reader.shift lines = 0 reader.each do |row| lines += 1 end lines end
  12. 12. 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
  13. 13. def get_number_of_lines reader = open_csv_reader reader.shift end reader.count
  14. 14. def repeat_commas(length) str = "" length.times { |i| str << ',' } str end
  15. 15. def repeat_commas(length) str = "" length.times { |i| str << ',' } str end str str str
  16. 16. def repeat_commas(length) (1..length).map { ',' }.join end
  17. 17. def repeat_commas(length) ',' * length end
  18. 18. ',' * length
  19. 19. def create @organization = Organization.new(org_params) validate_org :new end def update load_organization @organization.attributes = org_params validate_org :edit end
  20. 20. 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
  21. 21. 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
  22. 22. def assign_attributes(product, container) product.code = container.code product.desc = container.desc product.identifier = container.identifier product end
  23. 23. def assign_attributes(product, container) product.code = container.code product.desc = container.desc product.identifier = container.identifier product end product product product product
  24. 24. 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
  25. 25. 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
  26. 26. KnowYour Language
  27. 27. 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)
  28. 28. 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
  29. 29. 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
  30. 30. 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 )
  31. 31. KnowYour Library
  32. 32. def serializer DEPENDENCIES[type][:serializer] end
  33. 33. def serializer DEPENDENCIES[type][:serializer] end [:serializer]
  34. 34. def serializer DEPENDENCIES[type] end .fetch(:serializer)
  35. 35. Fail Fast
  36. 36. def number "#{order.number}-#{order_item.number}" end
  37. 37. Remove Dead Code
  38. 38. The Boy Scout Rule
  39. 39. Always check a module
 in cleaner than when 
 you checked it out
  40. 40. KnowYour Language
 KnowYour Library Fail Fast Remove Dead Code
  41. 41. When I look at my code 
 from six months ago
 I expect to see ways to improve it
  42. 42. Put your learning and understanding back 
 into the code
  43. 43. It’s up to you

×