$30 off During Our Annual Pro Sale. View Details »

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. A Python Æsthetic

    View Slide

  2. Video: http://pyvideo.org/video/1599/


    Slides: http://rhodesmill.org/brandon/slides/2012-11-pyconca/

    View Slide

  3. Video: http://pyvideo.org/video/1676/


    Slides: http://rhodesmill.org/brandon/slides/2013-03-pycon/

    View Slide

  4. Name the Ducks
    • Well-factored Nouns

    • Relentless Verbs

    • Sin of Synecdoche

    • Problem of Pluralization

    View Slide

  5. Well-Factored Nouns
    def main():!
    page = 'http://twitter.com/uranusjr/'!
    download_page(page)

    View Slide

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

    View Slide

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

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

  10. 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

    View Slide

  11. 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

    View Slide

  12. Well-Factored Nouns
    • Plan ahead (when you can)

    • Refactor often

    • Challenge yourself

    View Slide

  13. 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!

    View Slide

  14. 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!

    View Slide

  15. Relentless Verbs
    document.md5()! ! ! # Probably okay!
    !
    document.get_md5()!
    !
    document.calculate_md5()

    View Slide

  16. [P]racticality beats purity.

    View Slide

  17. [P]racticality beats purity.
    reversed(iterable)!
    !
    'a'.upper()!
    !
    len(iterable)!
    !
    min(iterable)!

    View Slide

  18. Relentless Verbs
    • Functions act

    • Verbose verbs can be dropped

    • Practicality beats purity

    View Slide

  19. Sin of Synecdoche
    # In medialib.py:!
    def fetch_songs(urls):!
    ...!
    !
    # In songlib.py:!
    def fetch(urls):!
    ...

    View Slide

  20. Sin of Synecdoche
    # The caller has their choice:!
    !
    from songlib import fetch!
    fetch(...)!
    !
    # or:!
    !
    import songlib!
    songlib.fetch(...)

    View Slide

  21. Sin of Synecdoche
    • Be obvious

    • Don’t repeat yourself

    • Let users choose

    View Slide

  22. Problem of Pluralization
    >>> connections!
    [,!
    ,!
    ]!
    !
    >>> connections!
    {'master': ,!
    'backup': }

    View Slide

  23. 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

    View Slide

  24. Anti-Plural
    connection_set!
    connection_list!
    connection_dict!
    !
    def close_all(connection_seq):!
    ...!
    !
    def reset_all(connection_map):!
    ...

    View Slide

  25. I still favor plurals
    And documentation is always necessary.

    View Slide

  26. Problem of Pluralization
    • Plurals can be ambiguous

    • There are only so many expressions

    • Total anti-plural may be too much

    View Slide

  27. My Problem

    View Slide

  28. 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

    View Slide

  29. 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

    View Slide

  30. My Problem
    >>> user.is_authenticated!
    >

    View Slide

  31. 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

    View Slide

  32. View Slide

  33. Descriptive Boolean
    # is + adjective = method!
    user.is_authenticated()!
    !
    !
    # No!!
    user.is_active!
    !
    # Clearly a noun.!
    user.activeness

    View Slide

  34. Watch Out
    # is + noun = variable?!
    >>> user.is_staff!
    True!
    !
    # But...!
    >>> '1'.isdigit!
    object at 0x106fa63a0>

    View Slide

  35. View Slide

  36. 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

    View Slide

  37. Python lacked
    booleans until 2.2.1
    Maybe we can get away with it.

    View Slide

  38. Similarly Problematic
    # Makes more sense being a variable!
    request.url!
    !
    # Obviously needs processing. Okay.!
    document.md5()!
    !
    # ???!
    rectangle.left!
    rectangle.center

    View Slide

  39. 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()

    View Slide

  40. FIRE QUESTIONS

    View Slide