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

Writing Beautiful Code - EuroPython 2017

Writing Beautiful Code - EuroPython 2017

Slides of my talk "Writing Beautiful Code" at Euro Python 2017.

7e03bd91a64c190bd4e54bd87d1ab0b2?s=128

Anand Chitipothu

July 11, 2017
Tweet

Transcript

  1. Writing Beautiful Code Anand Chitipothu

  2. Who is speaking? Anand Chitipothu @anandology Advanced programing courses @pipalacademy

    Building data science platform @rorodata
  3. quality without a name

  4. Programs must be written for people to read, and only

    incidentally for machines to execute. - Structure and Interpretation of Computer Programs (The Wizard Book)
  5. Choose Meaningful Names

  6. Two hard things in computer science are cache invalidation and

    naming things. - Phil Karlton
  7. Avoid Generic Names tmp tmp2 manager data

  8. ucf = UpperCaseFormatter() ba = BankAccount() formatter = UpperCaseFormatter() account

    = BankAccount() Avoid Abbreviations
  9. Avoid using datatype as name sum(list) count_words(string) sum(numbers) count_words(sentence)

  10. Nouns & Verbs Use nouns for variables and classes. size,

    price, Task, Scheduler, Bank Account Use verbs for functions. get_file_size, make_account, deposit
  11. largest_line(lines) files = os.listdir(directory) file = os.listdir(directory) for lines in

    open(filename).readlines(): sum += int(lines) Use plural for a list
  12. Reserve Loop Indexes Use i, j only as loop indexes.

    for i in range(10): print i for i in numbers: result += i for n in numbers: result += n
  13. Can you improve this? def get_data(x, y): z = []

    for i in x: z.append(i[y]) return z Example 1
  14. def get_column(dataset, col_index): column = [] for row in dataset:

    column.append(row[col_index]) return column Example 1
  15. Never use similar names for completely different datatypes. a1 =

    [1, 2, 3] a2 = len(x) values = [1, 2, 3] n = len(x) Similar names
  16. Comments

  17. Don’t say the obvious # increments x by 2 x

    = x + 2 # compensate for border on both the sides x = x + 2
  18. # The following is an optimization to saves # lot

    of memcache calls. Handle with care! ... Explain why you made that choice
  19. # -- XXX -- Anand - Sep 2015 -- #

    UTF-conversion was failing for a chinese # user for reasons I couldn't understand. # Added "ignore" as second argument to handle # that temporarily. name = name.encode("utf-8", "ignore") Document special cases
  20. # find length of the longest line n = max([len(line)

    for line in lines]) n = len(longest(lines)) Make Comments Redundant
  21. Make Comments Redundant # process documents … # upload them

    to search engine … docs = process_documents(...) search_engine_submit(docs)
  22. Program Organization

  23. Divide & Conquer Split the program into small independent modules

    and functions.
  24. The 7 ± 2 Rule The number of objects an

    average human can hold in working memory is 7 ± 2. - Miller's Law
  25. def add(input_data): try: x = int(input_data['x']) except ValueError: raise Exception("Invalid

    int value for x") try: y = int(input_data['x']) except ValueError: raise Exception("Invalid int value for y") return x+y Avoid Duplication
  26. def get_int(dictionary, key): try: return int(dictionary[key]) except ValueError: raise Exception("Invalid

    int value for {}".format(key)) def add(input_data): x = get_int(input_data, "x") y = get_int(input_data, "y") return x+y Avoid Duplication - generalize instead
  27. Avoid too many nested levels def update_post(...): post = get_post(..)

    if action == 'update-title': if title == '': ... else: ... elif action == "add-tag": ...
  28. def update_post(...): post = get_post(..) if action == "update-title": update_post_title(...)

    elif action == "add-tag": update_post_add_tag(...) Avoid too many nested levels
  29. Handle errors separately def get_user(email): if valid_user(email): if is_user_blocked(email): return

    Exception("Account is blocked") else: query = "...." row = db.select(query).first() return User(row) else: raise Exception("Invalid email")
  30. Handle errors separately def get_user(email): if not valid_user(email): raise ValueError("Invalid

    email") if is_email_blocked(email): raise Exception("Account blocked") query = "...." row = db.select(query).first() return User(row)
  31. Suppress the implementation details def main(): filename = sys.argv[1] words

    = read_words(filename) freq = wordfreq(words) print_freq(freq)
  32. Summary • Choose meaningful variable names • Use comments when

    required • Split the program into small independent modules & functions • Avoid duplication • Suppress implementation details • Always optimize for readability
  33. - The Tao of Programming “A program should be light

    and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little nor too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity.”
  34. - The Tao of Programming “A program should be light

    and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little nor too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity.”
  35. “A program should be light and agile, its subroutines connected

    like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little nor too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity.” - The Tao of Programming
  36. Happy Coding! @anandology