Slide 1

Slide 1 text

MAKING BEAUTIFUL PYTHON CODE Or Readability Counts JAIME BUELTA BY Thursday 17 October 13

Slide 2

Slide 2 text

Developers like Python because code is beautiful Thursday 17 October 13

Slide 3

Slide 3 text

Almost no one will appreciate it Thursday 17 October 13

Slide 4

Slide 4 text

(At least until there’s a problem) Thursday 17 October 13

Slide 5

Slide 5 text

BOSS DOES NOT CARE worried about costs and stuff Thursday 17 October 13

Slide 6

Slide 6 text

CUSTOMERS DON’T CARE worried about functionality and stuff Thursday 17 October 13

Slide 7

Slide 7 text

but your team will care Thursday 17 October 13

Slide 8

Slide 8 text

most of the time, the effort difference between good code and bad code is small Thursday 17 October 13

Slide 9

Slide 9 text

MY DREAM Thursday 17 October 13

Slide 10

Slide 10 text

Thursday 17 October 13

Slide 11

Slide 11 text

Thursday 17 October 13

Slide 12

Slide 12 text

Thursday 17 October 13

Slide 13

Slide 13 text

Thursday 17 October 13

Slide 14

Slide 14 text

Thursday 17 October 13

Slide 15

Slide 15 text

CODE IS ALWAYS CHANGING Thursday 17 October 13

Slide 16

Slide 16 text

CODE NEEDS TO WORK Thursday 17 October 13

Slide 17

Slide 17 text

“There's a difference between getting your hands dirty and being dirty.” DS Justin Ripley Thursday 17 October 13

Slide 18

Slide 18 text

“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) Thursday 17 October 13

Slide 19

Slide 19 text

WHAT MAKES BEAUTIFUL CODE? Thursday 17 October 13

Slide 20

Slide 20 text

EASY TO UNDERSTAND Thursday 17 October 13

Slide 21

Slide 21 text

(THEREFORE, EASY TO CHANGE) Thursday 17 October 13

Slide 22

Slide 22 text

NOT THE OTHER WAY AROUND Thursday 17 October 13

Slide 23

Slide 23 text

OVERDESIGN Thursday 17 October 13

Slide 24

Slide 24 text

GOOD CODE Thursday 17 October 13

Slide 25

Slide 25 text

FITS IN YOUR HEAD Thursday 17 October 13

Slide 26

Slide 26 text

BAD CODE... Thursday 17 October 13

Slide 27

Slide 27 text

Thursday 17 October 13

Slide 28

Slide 28 text

Thursday 17 October 13

Slide 29

Slide 29 text

Function Thursday 17 October 13

Slide 30

Slide 30 text

Function Module Thursday 17 October 13

Slide 31

Slide 31 text

Function Module System Thursday 17 October 13

Slide 32

Slide 32 text

Show how good you are HERE Not here Thursday 17 October 13

Slide 33

Slide 33 text

Thursday 17 October 13

Slide 34

Slide 34 text

Thursday 17 October 13

Slide 35

Slide 35 text

LOCALITY CONSISTENCY VERBOSITY Thursday 17 October 13

Slide 36

Slide 36 text

CONSISTENCY VERBOSITY Keep related stuff together LOCALITY Thursday 17 October 13

Slide 37

Slide 37 text

LOCALITY VERBOSITY Use patterns CONSISTENCY Thursday 17 October 13

Slide 38

Slide 38 text

LOCALITY CONSISTENCY When in doubt, explain VERBOSITY Thursday 17 October 13

Slide 39

Slide 39 text

Be as OBVIOUS as possible in other words... Thursday 17 October 13

Slide 40

Slide 40 text

LOCALITY Thursday 17 October 13

Slide 41

Slide 41 text

MSG_TEMPLATE = ‘Template {date}: {msg}’ MORE_CONSTANTS... .... def log_msg(message): formatted_msg = MSG_TEMPLATE.format( date=utcnow(), msg=this_message ) print formatted_msg Thursday 17 October 13

Slide 42

Slide 42 text

def log_msg(message): MSG_TEMPLATE = ‘{date}: {msg}’ formatted_msg = MSG_TEMPLATE.format( date=utcnow(), msg=message ) print formatted_msg Thursday 17 October 13

Slide 43

Slide 43 text

MSG_TEMPLATE = ‘{date}: {msg}’ def log_msg(message): formatted_msg = TEMPLATE.format( date=utcnow(), msg=message ) print formatted_msg Thursday 17 October 13

Slide 44

Slide 44 text

Related classes on the same module Function used only in a module not in independent module Keep files short Avoid boilerplate Separated business logic Thursday 17 October 13

Slide 45

Slide 45 text

CONSISTENCY Thursday 17 October 13

Slide 46

Slide 46 text

Humans are amazing recognizing patterns (so good we tend to see too many) Thursday 17 October 13

Slide 47

Slide 47 text

PEP8 IS THE MOST IMPORTANT PATTERN COLLECTION OUT THERE Thursday 17 October 13

Slide 48

Slide 48 text

Abstract common operations List comprehensions Decorators and with statement No private methods No getters and setters Thursday 17 October 13

Slide 49

Slide 49 text

I LOVE MY BRICK! syndrome Thursday 17 October 13

Slide 50

Slide 50 text

VERBOSITY Thursday 17 October 13

Slide 51

Slide 51 text

data = "+RESP:GTTRI,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"\ "%s,%s,%s,%s,%s,%s,%s\0" % (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) Thursday 17 October 13

Slide 52

Slide 52 text

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, ) Thursday 17 October 13

Slide 53

Slide 53 text

WHEN IN DOUBT, COMMENT Thursday 17 October 13

Slide 54

Slide 54 text

PUTTING ALL TOGETHER Thursday 17 October 13

Slide 55

Slide 55 text

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) Thursday 17 October 13

Slide 56

Slide 56 text

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)] Thursday 17 October 13

Slide 57

Slide 57 text

SMART IS THE ENEMY OF READABILITY Thursday 17 October 13

Slide 58

Slide 58 text

HAVE A GOOD REASON TO BE SMART And comment accordingly Thursday 17 October 13

Slide 59

Slide 59 text

ONE DAY, SOMEONE WILL BE SURPRISED WITH YOUR CODE Thursday 17 October 13

Slide 60

Slide 60 text

YOURSELF!!! Thursday 17 October 13

Slide 61

Slide 61 text

SO, REMEMBER Thursday 17 October 13

Slide 62

Slide 62 text

Thursday 17 October 13

Slide 63

Slide 63 text

BE OBVIOUS Thursday 17 October 13

Slide 64

Slide 64 text

ASSUME YOU CAN’T FIT A LOT OF CODE ON YOUR HEAD Thursday 17 October 13

Slide 65

Slide 65 text

BE SMART IN SYSTEMS, NOT IN FUNCTIONS Thursday 17 October 13

Slide 66

Slide 66 text

LOCALITY CONSISTENCY VERBOSITY Thursday 17 October 13

Slide 67

Slide 67 text

Tell your buddies how much you appreciate them writing simple code! Thursday 17 October 13

Slide 68

Slide 68 text

Thursday 17 October 13