8. Native Language alike
to configureTheDisplay(theProfilesName, brightnessValue)
tell application "System Events"
*if not* UI elements enabled then
DisplayAssistanceInstructions() *of me*
return
*end if*
openDisplaysPrefPane() *of me*
tell process "System Preferences"
click radio button "Display" of tab group 1 of window 1
*end tell*
quitPrefs() *of me*
*end tell*
return
end configureTheDisplay
9. But even in projects using Ruby...
def page201(from_page, visited_queen = false, threw_button = false)
if from_page == 59
fall_down_to_sinkhole
else
exit_the_passageway
end
enemy = if visit_queen
Badger.new
elsif threw_button
nil
else
Trollnew
end
enemy.angry if enemy
# ... 30 lines of code
end
11. Books of "Choose your own adventure"
• If you fight the troll with bare hands, turn to page 137.
• If you try to reason with the troll, turn to page 29.
• If you don your invisibility clock, turn to page 6.
12. What if you read this kind of book from the first
page to the end?
You exit the passageway into a large cavern. Unless you came from
page 59, in which case you fall down the sinkhole into a large cavern.
A huge troll, or possibly a badger ( if you already visited Queen
Pelican), blocks your path. Unless your threw a button down the
wishing well on page 8, in which case there nothing blocking your
way. The [troll or badger or nothing at all] does not look happy to see
you.
19. Sending strong message
The foundation of an object oriented system is the message.
— Sandi Metz, Practical Object-Oriented Design in Ruby
20. Tell a better story
1. Identify the message to be send
2. Find the roles which correspond to those messages
3. Ensure the methods's logic receives objects which can play those
roles.
22. A story of parse many legacy CSV files of purchase
records
1. Parse the purchase records from the CSV contained in a provided
IO object.
2. For each purchase record, use the record's email address to get
the associated customer record, or, if the email hasn't been seen
before, create a new customer record in our system.
3. Use the legacy record's product ID to find or create a product
record in our system.
4. Add the product to the customer record's list of purchases.
23. 1. #parse_legacypurchase_records.
2. For #each purchase record, use the record's #email_address to
#get_customer.
3. Use the record's #product_id to #get_product.
4. #add_purchasedproduct to the customer record.
5. #notify_offiles_available for the purchased product.
6. #log_successfulimport of the product record.
24. Find the roles which correspond to
those messages
*Single responsibility principle
26. Ensure the methods's logic receives
objects which can play those roles.
*Law of Demeter
27. 1. legacydataparser.parsepurchaserecords.
2. For purchase_list.each as purchase_record, use
purchaserecord.emailaddress to customerlist.getcustomer.
3. Use the purchaserecord.productid to productinventory.getproduct.
4. customer.addpurchasedproduct.
5. customer.notifyoffiles_available for the product.
6. self.logsuccessfulimport of the purchase_record.
28. def import_legacy_purchase_data(data)
purchase_list = legacy_data_parser.parse_purchase_records(data)
purchase_list.each do |purchase_record|
customer = customer_list.get_customer(purchase_record.email_address)
product = product_inventory.get_product(purchase_record.product_id)
customer.add_purchased_product(product)
customer.notify_of_files_available(product)
log_successful_import(purchase_record)
end
end
Now it starting to look a lot like code.
30. Four parts of every method
1. collecting inputs
2. performing works
3. delivering output
4. handling failures
• diagnostics and cleanup
31.
32. Some More Tips
1. Workround: Glue of the existing tools to perform works.
2. Refactor: Focus on Inrtent of a method rather than method's
environment
3. One level of abstraction each layer.
4. Pair Programming helps.
5. Some times TDD is a good indicator.