SlideShare ist ein Scribd-Unternehmen logo
1 von 64
Downloaden Sie, um offline zu lesen
MAKING BEAUTIFUL
PYTHON CODE
Or Readability Counts
BY

JAIME BUELTA
Developers like
Python because code
is beautiful
Almost no one will appreciate it
(At least until there’s a problem)
BOSS DOES NOT CARE
worried about costs and stuff
CUSTOMERS DON’T CARE
worried about functionality and stuff
but your team will care
most of the time,
the effort difference
between good code
and bad code is small
MY
DREAM
CODE IS ALWAYS CHANGING
CODE NEEDS TO WORK
“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)
WHAT MAKES BEAUTIFUL
CODE?
EASY TO UNDERSTAND
(THEREFORE, EASY TO
CHANGE)
NOT THE OTHER WAY
AROUND
OVERDESIGN
GOOD CODE
FITS IN YOUR HEAD
BAD
CODE...
System

Module
Function
Show how good you are HERE

Not here
LOCALITY
CONSISTENCY
VERBOSITY
LOCALITY
Keep related stuff together

CONSISTENCY
VERBOSITY
LOCALITY

CONSISTENCY
Use patterns

VERBOSITY
LOCALITY
CONSISTENCY

VERBOSITY

When in doubt, explain
in other words...

Be as OBVIOUS as possible
LOCALITY
MSG_TEMPLATE = ‘Template {date}: {msg}’
MORE_CONSTANTS...
....

def log_msg(message):
formatted_msg = MSG_TEMPLATE.format(
date=utcnow(),
msg=this_message
)
print formatted_msg
def log_msg(message):
MSG_TEMPLATE = ‘{date}: {msg}’
formatted_msg = MSG_TEMPLATE.format(
date=utcnow(),
msg=message
)
print formatted_msg
MSG_TEMPLATE = ‘{date}: {msg}’
def log_msg(message):
formatted_msg = TEMPLATE.format(
date=utcnow(),
msg=message
)
print formatted_msg
Related classes on
the same module

Keep files short

Function used only in
a module not in
independent module
Avoid
boilerplate

Separated
business logic
CONSISTENCY
Humans are amazing
recognizing patterns

(so good we tend to see too many)
PEP8 IS THE MOST
IMPORTANT PATTERN
COLLECTION OUT THERE
Abstract common operations
No getters and setters

List comprehensions
Decorators and with
statement
No private methods
I LOVE MY BRICK!

syndrome
VERBOSITY
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)
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,
)
WHEN IN DOUBT,
COMMENT
PUTTING ALL
TOGETHER
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)
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)]
SMART IS THE ENEMY OF
READABILITY
HAVE A GOOD REASON TO
BE SMART
And comment accordingly
ONE DAY, SOMEONE WILL BE
SURPRISED WITH YOUR CODE
YOURSELF!!!
SO, REMEMBER
BE OBVIOUS
ASSUME YOU CAN’T FIT A LOT
OF CODE ON YOUR HEAD
BE SMART IN SYSTEMS, NOT
IN FUNCTIONS
LOCALITY
CONSISTENCY
VERBOSITY
Tell your buddies how much you
appreciate them writing simple code!
Make beautiful Python code

Weitere ähnliche Inhalte

Andere mochten auch (6)

Do more than one thing at the same time, the Python way
Do more than one thing at the same time, the Python wayDo more than one thing at the same time, the Python way
Do more than one thing at the same time, the Python way
 
Zen and the Art of Python
Zen and the Art of PythonZen and the Art of Python
Zen and the Art of Python
 
Mongo db
Mongo dbMongo db
Mongo db
 
Politics And Governance With Phil. Constitution boa
Politics And Governance With Phil. Constitution boaPolitics And Governance With Phil. Constitution boa
Politics And Governance With Phil. Constitution boa
 
Visual Thinking Games
Visual Thinking GamesVisual Thinking Games
Visual Thinking Games
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computer
 

Ähnlich wie Make beautiful Python code

Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review Guide
Benjamin Kissinger
 
Intelligent Ruby + Machine Learning
Intelligent Ruby + Machine LearningIntelligent Ruby + Machine Learning
Intelligent Ruby + Machine Learning
Ilya Grigorik
 
A Small Talk on Getting Big
A Small Talk on Getting BigA Small Talk on Getting Big
A Small Talk on Getting Big
britt
 
notes as .ppt
notes as .pptnotes as .ppt
notes as .ppt
butest
 
Prophet - Beijing Perl Workshop
Prophet - Beijing Perl WorkshopProphet - Beijing Perl Workshop
Prophet - Beijing Perl Workshop
Jesse Vincent
 
TOWARDS OPTIMALITY OF IMAGE SEGMENTATION PART- I
TOWARDS OPTIMALITY OF IMAGE SEGMENTATION PART- ITOWARDS OPTIMALITY OF IMAGE SEGMENTATION PART- I
TOWARDS OPTIMALITY OF IMAGE SEGMENTATION PART- I
Anish Acharya
 

Ähnlich wie Make beautiful Python code (20)

You can't spell "monitoring" without "monoid"
You can't spell "monitoring" without "monoid"You can't spell "monitoring" without "monoid"
You can't spell "monitoring" without "monoid"
 
Basics of Programming - A Review Guide
Basics of Programming - A Review GuideBasics of Programming - A Review Guide
Basics of Programming - A Review Guide
 
Tips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software EngineeringTips And Tricks For Bioinformatics Software Engineering
Tips And Tricks For Bioinformatics Software Engineering
 
Data oriented design and c++
Data oriented design and c++Data oriented design and c++
Data oriented design and c++
 
The R of War
The R of WarThe R of War
The R of War
 
Intelligent Ruby + Machine Learning
Intelligent Ruby + Machine LearningIntelligent Ruby + Machine Learning
Intelligent Ruby + Machine Learning
 
PyDX Presentation about Python, GeoData and Maps
PyDX Presentation about Python, GeoData and MapsPyDX Presentation about Python, GeoData and Maps
PyDX Presentation about Python, GeoData and Maps
 
2020 04 10 Catch IT - Getting started with ML.Net
2020 04 10 Catch IT - Getting started with ML.Net2020 04 10 Catch IT - Getting started with ML.Net
2020 04 10 Catch IT - Getting started with ML.Net
 
A Small Talk on Getting Big
A Small Talk on Getting BigA Small Talk on Getting Big
A Small Talk on Getting Big
 
notes as .ppt
notes as .pptnotes as .ppt
notes as .ppt
 
StatsD DevOps Boulder 7/20/15
StatsD DevOps Boulder 7/20/15StatsD DevOps Boulder 7/20/15
StatsD DevOps Boulder 7/20/15
 
Prophet - Beijing Perl Workshop
Prophet - Beijing Perl WorkshopProphet - Beijing Perl Workshop
Prophet - Beijing Perl Workshop
 
Allegograph
AllegographAllegograph
Allegograph
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
Screencasting and Presenting for Engineers
Screencasting and Presenting for EngineersScreencasting and Presenting for Engineers
Screencasting and Presenting for Engineers
 
Maintainability SFJS Sept 4 2014
Maintainability SFJS Sept 4 2014 Maintainability SFJS Sept 4 2014
Maintainability SFJS Sept 4 2014
 
Pragmatics of Declarative Ajax
Pragmatics of Declarative AjaxPragmatics of Declarative Ajax
Pragmatics of Declarative Ajax
 
TOWARDS OPTIMALITY OF IMAGE SEGMENTATION PART- I
TOWARDS OPTIMALITY OF IMAGE SEGMENTATION PART- ITOWARDS OPTIMALITY OF IMAGE SEGMENTATION PART- I
TOWARDS OPTIMALITY OF IMAGE SEGMENTATION PART- I
 
Chatbot ppt
Chatbot pptChatbot ppt
Chatbot ppt
 
Map Reduce
Map ReduceMap Reduce
Map Reduce
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

Make beautiful Python code