Make beautiful Python code

4,758 views

Published on

Sure, Python can produce great readable code. But what are the best techniques for making your code shine? Some discussion about the values and principles behind great code.

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,758
On SlideShare
0
From Embeds
0
Number of Embeds
2,949
Actions
Shares
0
Downloads
22
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Make beautiful Python code

  1. 1. MAKING BEAUTIFUL PYTHON CODE Or Readability Counts BY JAIME BUELTA
  2. 2. Developers like Python because code is beautiful
  3. 3. Almost no one will appreciate it
  4. 4. (At least until there’s a problem)
  5. 5. BOSS DOES NOT CARE worried about costs and stuff
  6. 6. CUSTOMERS DON’T CARE worried about functionality and stuff
  7. 7. but your team will care
  8. 8. most of the time, the effort difference between good code and bad code is small
  9. 9. MY DREAM
  10. 10. CODE IS ALWAYS CHANGING
  11. 11. CODE NEEDS TO WORK
  12. 12. “There's a difference between getting your hands dirty and being dirty.” DS Justin Ripley (yes, the picture is not from Luther, but from The Good Cop)
  13. 13. WHAT MAKES BEAUTIFUL CODE?
  14. 14. EASY TO UNDERSTAND
  15. 15. (THEREFORE, EASY TO CHANGE)
  16. 16. NOT THE OTHER WAY AROUND
  17. 17. OVERDESIGN
  18. 18. GOOD CODE
  19. 19. FITS IN YOUR HEAD
  20. 20. BAD CODE...
  21. 21. System Module Function
  22. 22. Show how good you are HERE Not here
  23. 23. LOCALITY CONSISTENCY VERBOSITY
  24. 24. LOCALITY Keep related stuff together CONSISTENCY VERBOSITY
  25. 25. LOCALITY CONSISTENCY Use patterns VERBOSITY
  26. 26. LOCALITY CONSISTENCY VERBOSITY When in doubt, explain
  27. 27. in other words... Be as OBVIOUS as possible
  28. 28. LOCALITY
  29. 29. MSG_TEMPLATE = ‘Template {date}: {msg}’ MORE_CONSTANTS... .... def log_msg(message): formatted_msg = MSG_TEMPLATE.format( date=utcnow(), msg=this_message ) print formatted_msg
  30. 30. def log_msg(message): MSG_TEMPLATE = ‘{date}: {msg}’ formatted_msg = MSG_TEMPLATE.format( date=utcnow(), msg=message ) print formatted_msg
  31. 31. MSG_TEMPLATE = ‘{date}: {msg}’ def log_msg(message): formatted_msg = TEMPLATE.format( date=utcnow(), msg=message ) print formatted_msg
  32. 32. Related classes on the same module Keep files short Function used only in a module not in independent module Avoid boilerplate Separated business logic
  33. 33. CONSISTENCY
  34. 34. Humans are amazing recognizing patterns (so good we tend to see too many)
  35. 35. PEP8 IS THE MOST IMPORTANT PATTERN COLLECTION OUT THERE
  36. 36. Abstract common operations No getters and setters List comprehensions Decorators and with statement No private methods
  37. 37. I LOVE MY BRICK! syndrome
  38. 38. VERBOSITY
  39. 39. data = "+RESP:GTTRI,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s," "%s,%s,%s,%s,%s,%s,%s0" % (imei, number, reserved_1, reserved_2, gps_fix, speed, heading, altitude, gps_accuracy, longitude, latitude, send_time, mcc, mnc, lac, cellid, ta, count_num, ver)
  40. 40. data = (‘+RESP:GTTRI,{imei},{number},{reserved_1},’ ‘{reserved_2},{gps_fix},{speed},{heading},’ ‘{altitude},{gps_accuracy},{longitude},’ ‘{latitude},{send_time}, {mmc}, {lac}, {cellid}’ ‘{ta},{count_num}, {version}’).format( imei=imei, number=number, reserved_1=reserved_1, reserved_2=reserved_2, gps_fix=gps_fix, speed=speed, heading=heading, altitude=altitude, gps_accuracy=gps_accuracy, longitude=longitude, latitude=latitude, send_time=send_time, mcc=mcc, mnc=mnc, lac=lac, cellid=cellid, ta=ta, count_num=count_num, version=ver, )
  41. 41. WHEN IN DOUBT, COMMENT
  42. 42. PUTTING ALL TOGETHER
  43. 43. results = [] for row in query_results: tag, value, updated = row if value and updated > last_time: TEMP = ‘tag: {0}, value: {1} result = TEMP.format(tag, value) results.append(result)
  44. 44. def format_result(row): tag, value, _ = row TEMP = ‘tag: {0}, value: {1}’ return TEMP.format(tag, value) def interesting(row): _, value, updated = row return value and updated > last_time results = [format_result(row) for row in query_results if interesting(row)]
  45. 45. SMART IS THE ENEMY OF READABILITY
  46. 46. HAVE A GOOD REASON TO BE SMART And comment accordingly
  47. 47. ONE DAY, SOMEONE WILL BE SURPRISED WITH YOUR CODE
  48. 48. YOURSELF!!!
  49. 49. SO, REMEMBER
  50. 50. BE OBVIOUS
  51. 51. ASSUME YOU CAN’T FIT A LOT OF CODE ON YOUR HEAD
  52. 52. BE SMART IN SYSTEMS, NOT IN FUNCTIONS
  53. 53. LOCALITY CONSISTENCY VERBOSITY
  54. 54. Tell your buddies how much you appreciate them writing simple code!

×