tell a good story with

36
Tell a good story with Ruby {slide: 'http://goo.gl/Sfl0RF'}

Upload: tai-su

Post on 02-Nov-2014

266 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Tell a good story with

Tell a good story with Ruby{slide: 'http://goo.gl/Sfl0RF'}

Page 2: Tell a good story with

I.am '���'

@taiansu

co-organizer of RailsGirls Taipei

work for OptimisDev

Page 3: Tell a good story with

I.was "a computer magazine editor", 8.years.ago

I.write_code? #=> false

Page 4: Tell a good story with

So why I'm here?

Page 5: Tell a good story with

I.start_coding from: "Two books"

Special Thanks

Page 6: Tell a good story with

Learn to Program

by Chis Pine

Page 7: Tell a good story with

���� Ruby

by ��������

Ruby Programming - �Ruby�������

Page 8: Tell a good story with

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

Page 9: Tell a good story with

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

Page 10: Tell a good story with

Good story, poorly told.

Page 11: Tell a good story with

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.

Page 12: Tell a good story with

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.

Page 13: Tell a good story with

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

Page 14: Tell a good story with

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

Page 15: Tell a good story with

Two TV Serises

Page 16: Tell a good story with
Page 17: Tell a good story with
Page 18: Tell a good story with

MacGyver

vs.

Mission Impossible

Page 19: Tell a good story with

Sending strong message

The foundation of an object oriented system is the message.

— Sandi Metz, Practical Object-Oriented Design in Ruby

Page 20: Tell a good story with

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.

Page 21: Tell a good story with

Identify the message to be send

*Tell, don't ask

Page 22: Tell a good story with

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.

Page 23: Tell a good story with

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.

Page 24: Tell a good story with

Find the roles which correspond to those messages

*Single responsibility principle

Page 25: Tell a good story with

Message => Receiver Role

• #parselegacypurchaserecords => *legacydata_parser*

• #each=> purchase_list

• #emailaddress, #productid => purchase_record

• #getcustomer=> customerlist

etc...

Page 26: Tell a good story with

Ensure the methods's logic receives objects which can play those roles.

*Law of Demeter

Page 27: Tell a good story with

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.

Page 28: Tell a good story with

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.

Page 29: Tell a good story with

Don't Know Where to Start?

Let's talk about method

Page 30: Tell a good story with

Four parts of every method

1. collecting inputs

2. performing works

3. delivering output

4. handling failures

• diagnostics and cleanup

Page 31: Tell a good story with
Page 32: Tell a good story with

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.

Page 33: Tell a good story with

Books Recommendation

Page 34: Tell a good story with

Practical object-oriented design in

Ruby

by Sandi Metz

Page 35: Tell a good story with

Confident Ruby

by Avdi Grimm

Page 36: Tell a good story with

Questions?