The Boy Scout Rule
-
Upload
alistair-mckinnell -
Category
Technology
-
view
275 -
download
0
Transcript of The Boy Scout Rule
![Page 1: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/1.jpg)
The Boy Scout Rule
Alistair McKinnell@amckinnell
Nulogy
![Page 2: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/2.jpg)
![Page 3: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/3.jpg)
Imagine that your codebase gets a little
better everyday
![Page 4: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/4.jpg)
It’s up to you
![Page 5: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/5.jpg)
It’s up to you(and your teammates)
![Page 6: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/6.jpg)
The Boy Scout Rule
![Page 7: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/7.jpg)
Always check a module in cleaner than when you checked it out
![Page 8: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/8.jpg)
Know Your Language
![Page 9: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/9.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/10.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/12.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/14.jpg)
def get_number_of_lines reader = open_csv_reader reader.shift
end reader.count
![Page 15: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/15.jpg)
def repeat_commas(length) str = "" length.times { |i| str << ',' } str end
![Page 16: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/16.jpg)
def repeat_commas(length) str = "" length.times { |i| str << ',' } str end
str str str
![Page 17: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/17.jpg)
def repeat_commas(length) (1..length).map { ',' }.join end
![Page 18: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/18.jpg)
def repeat_commas(length) ',' * length end
![Page 19: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/19.jpg)
',' * length
![Page 20: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/20.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/21.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/22.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/23.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/24.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/25.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/26.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/27.jpg)
Know Your Language
![Page 28: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/28.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/29.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/30.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/31.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/32.jpg)
Know Your Library
![Page 33: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/33.jpg)
def serializer DEPENDENCIES[type][:serializer] end
![Page 34: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/34.jpg)
def serializer DEPENDENCIES[type][:serializer] end [:serializer]
![Page 35: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/35.jpg)
def serializer DEPENDENCIES[type] end .fetch(:serializer)
![Page 36: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/36.jpg)
Fail Fast
![Page 37: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/37.jpg)
def number "#{order.number}-#{order_item.number}" end
![Page 38: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/38.jpg)
![Page 39: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/39.jpg)
Remove Dead Code
![Page 40: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/40.jpg)
The Boy Scout Rule
![Page 41: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/41.jpg)
Always check a module in cleaner than when you checked it out
![Page 42: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/42.jpg)
Know Your LanguageKnow Your Library
Fail FastRemove Dead Code
![Page 43: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/43.jpg)
When I look at my code from six months ago
I expect to see ways to improve it
![Page 44: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/44.jpg)
Put your learning and understanding back
into the code
![Page 45: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/45.jpg)
![Page 46: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/46.jpg)
![Page 47: The Boy Scout Rule](https://reader030.fdocuments.in/reader030/viewer/2022020314/58a7274f1a28ab0d0d8b538f/html5/thumbnails/47.jpg)
It’s up to you