tell a good story with
DESCRIPTION
TRANSCRIPT
Tell a good story with Ruby{slide: 'http://goo.gl/Sfl0RF'}
I.am '���'
@taiansu
co-organizer of RailsGirls Taipei
work for OptimisDev
I.was "a computer magazine editor", 8.years.ago
I.write_code? #=> false
So why I'm here?
I.start_coding from: "Two books"
Special Thanks
Learn to Program
by Chis Pine
���� Ruby
by ��������
Ruby Programming - �Ruby�������
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* returnend configureTheDisplay
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
Good story, poorly told.
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.
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.
Chinese Version
�/�,�I'&��@EH70��% 59 3�I��� ��%��;�4��@E�H��8:I.���BC(����#�5�GF=�� )IA+���)H70��!63$D<9�12?�I��� ��*(A+�H�8:I.�BCI.�� ���I�"������0-�>�H
def page201(from_page, visit_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
# ...
end
Two TV Serises
MacGyver
vs.
Mission Impossible
Sending strong message
The foundation of an object oriented system is the message.
— Sandi Metz, Practical Object-Oriented Design in Ruby
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.
Identify the message to be send
*Tell, don't ask
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.
5. Notify the customer of the new location where they can download their files and update their account info.
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.
Find the roles which correspond to those messages
*Single responsibility principle
Message => Receiver Role
• #parselegacypurchaserecords => *legacydata_parser*
• #each=> purchase_list
• #emailaddress, #productid => purchase_record
• #getcustomer=> customerlist
etc...
Ensure the methods's logic receives objects which can play those roles.
*Law of Demeter
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.
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.
Don't Know Where to Start?
Let's talk about method
Four parts of every method
1. collecting inputs
2. performing works
3. delivering output
4. handling failures
• diagnostics and cleanup
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.
Books Recommendation
Practical object-oriented design in
Ruby
by Sandi Metz
Confident Ruby
by Avdi Grimm
Questions?