Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Naming Convention in Python

Naming Convention in Python

About naming convention in Python, including topics discussed in “A Python Æsthetic” by Brandon Rhodes, and some personal note.

NOTICE: Credits for example code snippets and various contents in this presentation (exclude the last part, starting from slide 27) fully go to Brandon Rhodes.

Tzu-ping Chung

December 19, 2013
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. Name the Ducks • Well-factored Nouns • Relentless Verbs •

    Sin of Synecdoche • Problem of Pluralization
  2. Well-Factored Nouns def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)! ! def

    download_page(url):! page = client.get(url)! process_page(page)! ! def process_page(page):! ... = page.content
  3. Well-Factored Nouns def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)! ! def

    download_page(url):! page = client.get(url)! process_page(page)! ! def process_page(response):! page = response.content
  4. Well-Factored Nouns def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)! ! def

    download_page(url):! page = requests.get(url)! process_page(page)! ! def process_page(response):! page = response.content
  5. Well-Factored Nouns def main():! url = 'http://twitter.com/uranusjr/'! download_page(url)! ! def

    download_page(url):! response = requests.get(url)! process_page(response)! ! def process_page(response):! page = response.content
  6. Relentless Verbs def database():! # What does this even do?!

    ! def get_database():! # Gets an instance for me!! ! def create_database():! # Creates a database and returns! # it for me!
  7. Relentless Verbs def database():! # What does this even do?!

    ! def get_database():! # Gets an instance for me!! ! def create_database():! # Creates a database and returns! # it for me!
  8. Relentless Verbs • Functions act • Verbose verbs can be

    dropped • Practicality beats purity
  9. Sin of Synecdoche # The caller has their choice:! !

    from songlib import fetch! fetch(...)! ! # or:! ! import songlib! songlib.fetch(...)
  10. Problem of Pluralization >>> connections! [<SQLConnection at 0xb72ff4d8>,! <SQLConnection at

    0xb72ff4d0>,! <SQLConnection at 0xb72ff4f0>]! ! >>> connections! {'master': <SQLConnection at 0xb72ff4d8>,! 'backup': <SQLConnection at 0xb72ff4f0>}
  11. datum data datum datum datum dataset data datum datum datum

    data datum datum datum data datum datasets data datu datu datu data datu datu datu data data datu datu datu data datu datu datu data dataset data datu datu datu data datu datu datu data
  12. Problem of Pluralization • Plurals can be ambiguous • There

    are only so many expressions • Total anti-plural may be too much
  13. My Problem def user_profile(request, username):! user = request.user! if user.is_authenticated:!

    if user.username == username:! # Show personal page! else:! # Show public page! else:! # Show page for anonymous user
  14. My Problem def user_profile(request, username):! user = request.user! if user.is_authenticated:!

    if user.username == username:! # Show personal page! else:! # Show public page! else:! # Show page for anonymous user
  15. My Problem def user_profile(request, username):! user = request.user! if user.is_authenticated():!

    if user.username == username:! # Show personal page! else:! # Show public page! else:! # Show page for anonymous user
  16. Descriptive Boolean # is + adjective = method! user.is_authenticated()! !

    ! # No!! user.is_active! ! # Clearly a noun.! user.activeness
  17. Watch Out # is + noun = variable?! >>> user.is_staff!

    True! ! # But...! >>> '1'.isdigit! <built-in method isdigit of str object at 0x106fa63a0>
  18. My Current Solution # is + anything = method! user.is_authenticated()!

    ! ! # WRONG! method.is_action = True! ! # Considered correct (but ugly)! method.action_flag = True! ! # Maybe use something else?! method.action_info = None
  19. Similarly Problematic # Makes more sense being a variable! request.url!

    ! # Obviously needs processing. Okay.! document.md5()! ! # ???! rectangle.left! rectangle.center
  20. No Perfect Solution # Parentheses do not matter in Ruby!

    user.authenticated?! ! # But things can get funky quickly! proc_obj = proc { user.authenticated? }! proc_obj.call! ! # Python can be inconvenient! user.is_authenticated()! ! # But obvious things stay obvious! func = user.is_authenticated! func()