make beautiful python code

64
MAKING BEAUTIFUL PYTHON CODE Or Readability Counts JAIME BUELTA BY

Upload: jaime-buelta

Post on 29-Nov-2014

3.806 views

Category:

Technology


2 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Make beautiful Python code

MAKING BEAUTIFUL PYTHON CODE

Or Readability Counts

JAIME BUELTABY

Page 2: Make beautiful Python code

Developers like Python because code

is beautiful

Page 3: Make beautiful Python code

Almost no one will appreciate it

Page 4: Make beautiful Python code

(At least until there’s a problem)

Page 5: Make beautiful Python code

BOSS DOES NOT CAREworried about costs and stuff

Page 6: Make beautiful Python code

CUSTOMERS DON’T CAREworried about functionality and stuff

Page 7: Make beautiful Python code

but your team will care

Page 8: Make beautiful Python code

most of the time, the effort difference between good code and bad code is small

Page 9: Make beautiful Python code

MY DREAM

Page 10: Make beautiful Python code
Page 11: Make beautiful Python code
Page 12: Make beautiful Python code
Page 13: Make beautiful Python code
Page 14: Make beautiful Python code
Page 15: Make beautiful Python code

CODE IS ALWAYS CHANGING

Page 16: Make beautiful Python code

CODE NEEDS TO WORK

Page 17: Make beautiful Python code

“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)

Page 18: Make beautiful Python code

WHAT MAKES BEAUTIFUL CODE?

Page 19: Make beautiful Python code

EASY TO UNDERSTAND

Page 20: Make beautiful Python code

(THEREFORE, EASY TO CHANGE)

Page 21: Make beautiful Python code

NOT THE OTHER WAY AROUND

Page 22: Make beautiful Python code

OVERDESIGN

Page 23: Make beautiful Python code

GOOD CODE

Page 24: Make beautiful Python code

FITS IN YOUR HEAD

Page 25: Make beautiful Python code

BAD CODE...

Page 26: Make beautiful Python code
Page 27: Make beautiful Python code

Function

Module

System

Page 28: Make beautiful Python code

Show how good you are HERE

Not here

Page 29: Make beautiful Python code
Page 30: Make beautiful Python code
Page 31: Make beautiful Python code

LOCALITY

CONSISTENCY

VERBOSITY

Page 32: Make beautiful Python code

CONSISTENCY

VERBOSITY

Keep related stuff togetherLOCALITY

Page 33: Make beautiful Python code

LOCALITY

VERBOSITYUse patterns

CONSISTENCY

Page 34: Make beautiful Python code

LOCALITY

CONSISTENCY

When in doubt, explainVERBOSITY

Page 35: Make beautiful Python code

Be as OBVIOUS as possible

in other words...

Page 36: Make beautiful Python code

LOCALITY

Page 37: Make beautiful Python code

MSG_TEMPLATE = ‘Template {date}: {msg}’MORE_CONSTANTS.......

def log_msg(message): formatted_msg = MSG_TEMPLATE.format( date=utcnow(), msg=this_message ) print formatted_msg

Page 38: Make beautiful Python code

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

Page 39: Make beautiful Python code

MSG_TEMPLATE = ‘{date}: {msg}’

def log_msg(message): formatted_msg = TEMPLATE.format( date=utcnow(), msg=message ) print formatted_msg

Page 40: Make beautiful Python code

Related classes on the same module

Function used only in a module not in

independent module

Keep files short

Avoid boilerplate

Separated business logic

Page 41: Make beautiful Python code

CONSISTENCY

Page 42: Make beautiful Python code

Humans are amazing recognizing patterns

(so good we tend to see too many)

Page 43: Make beautiful Python code

PEP8 IS THE MOST IMPORTANT PATTERN

COLLECTION OUT THERE

Page 44: Make beautiful Python code

Abstract common operations

List comprehensions

Decorators and with statement

No private methods

No getters and setters

Page 45: Make beautiful Python code

I LOVE MY BRICK!

syndrome

Page 46: Make beautiful Python code

VERBOSITY

Page 47: Make beautiful Python code

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)

Page 48: Make beautiful Python code

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, )

Page 49: Make beautiful Python code

WHEN IN DOUBT, COMMENT

Page 50: Make beautiful Python code

PUTTING ALL TOGETHER

Page 51: Make beautiful Python code

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)

Page 52: Make beautiful Python code

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)]

Page 53: Make beautiful Python code

SMART IS THE ENEMY OF READABILITY

Page 54: Make beautiful Python code

HAVE A GOOD REASON TO BE SMART

And comment accordingly

Page 55: Make beautiful Python code

ONE DAY, SOMEONE WILL BE SURPRISED WITH YOUR CODE

Page 56: Make beautiful Python code

YOURSELF!!!

Page 57: Make beautiful Python code

SO, REMEMBER

Page 58: Make beautiful Python code
Page 59: Make beautiful Python code

BE OBVIOUS

Page 60: Make beautiful Python code

ASSUME YOU CAN’T FIT A LOT OF CODE ON YOUR HEAD

Page 61: Make beautiful Python code

BE SMART IN SYSTEMS, NOT IN FUNCTIONS

Page 62: Make beautiful Python code

LOCALITY

CONSISTENCY

VERBOSITY

Page 63: Make beautiful Python code

Tell your buddies how much you appreciate them writing simple code!

Page 64: Make beautiful Python code