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.

Tell a good story with

791 views

Published on

Published in: Technology, Education
  • Be the first to comment

Tell a good story with

  1. 1. Tell a good story with Ruby {slide: 'http://goo.gl/Sfl0RF'}
  2. 2. I.am ' ' @taiansu co-organizer of RailsGirls Taipei work for OptimisDev
  3. 3. I.was "a computer magazine editor", 8.years.ago I.write_code? #=> false
  4. 4. So why I'm here?
  5. 5. I.start_coding from: "Two books" Special Thanks
  6. 6. Learn to Program by Chis Pine
  7. 7. Ruby by Ruby Programming - Ruby
  8. 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. 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
  10. 10. Good story, poorly told.
  11. 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. 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.
  13. 13. Chinese Version 59 ( )
  14. 14. 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
  15. 15. Two TV Serises
  16. 16. MacGyver vs. Mission Impossible
  17. 17. Sending strong message The foundation of an object oriented system is the message. — Sandi Metz, Practical Object-Oriented Design in Ruby
  18. 18. 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.
  19. 19. Identify the message to be send *Tell, don't ask
  20. 20. 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.
  21. 21. 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.
  22. 22. Find the roles which correspond to those messages *Single responsibility principle
  23. 23. Message => Receiver Role • #parselegacypurchaserecords => *legacydata_parser* • #each => purchase_list • #emailaddress, #productid => purchase_record • #getcustomer => customerlist etc...
  24. 24. Ensure the methods's logic receives objects which can play those roles. *Law of Demeter
  25. 25. 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.
  26. 26. 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.
  27. 27. Don't Know Where to Start? Let's talk about method
  28. 28. Four parts of every method 1. collecting inputs 2. performing works 3. delivering output 4. handling failures • diagnostics and cleanup
  29. 29. 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.
  30. 30. Books Recommendation
  31. 31. Practical object- oriented design in Ruby by Sandi Metz
  32. 32. Confident Ruby by Avdi Grimm
  33. 33. Questions?

×