SlideShare a Scribd company logo
Boost
Maintainability
File "api.py", line 101, in generate_response

next_page = page + 1
TypeError: must be str, not int
page + 1
?
page = make_html(...)
next_page = page+1

!
We read lines randomly.
”
“
page_html = make_html(...)
next_page_no = page_no+1

✓
page_html + 1
!
— Joel on Software
Making wrong code look wrong
”
“
—Joel on Software
Making Wrong Code Look Wrong
• “Making Wrong Code Look Wrong”
• http://bit.ly/joel-wrong
• This talk will be more systemic than it.
• but exception is good, IMO.
Maintainability
To understand a random line,
the lines you need to read back.
”
“
💰💰💰
Time is just
❤ Python & Open Source
Mosky
• Python Charmer at Pinkoi.
• Has spoken at
• PyCons in TW, KR, JP, SG, HK,

COSCUPs, and TEDx, etc.
• Countless hours 

on teaching Python.
• Own Python packages:
• ZIPCodeTW, 

MoSQL, Clime, etc.
• http://mosky.tw/
Android &
Backend

Engineers
We're looking for
Boost Maintainability
• “Maintainability”
• Lines need to read back.
• Making It Zero
• Progressive From Zero
Making It Zero
A 

Dominant Rule
Be exact & consistent.
”
“
result = ...
result = ...
𝖷
resp = ...
parsed_dict = ...
✓
user = User(user_id)
buyer = user_id
𝖷
buyer = User(user_id)
buyer_id = user_id
✓
Get a dictionary, please.
Ops Hint
page = ...
△
page_no = ...
page_html = ...
✓
requested_fields

&

allowed_fields
?
set(requested_fields)

&

allowed_field_set
✓
user = User(...)
user = {}
?
user = User(...)
user_d = {}
✓
resp = requests.get(...)
resp.text
resp.json
?
resp.text
resp.json()
!
resp.text
resp.parse_as_json()
✓
Req.is_secure()
is_secure = True
𝖷
secure = True
with_tls = True
✓
using_tls = True

req_is_secure = True
✓
The General Rules
Ops Hint
• Hints which ops you should use.
• _no: numeric.
• <plural>: sequence, usually is mutable sequence (list).
• _<type>: if no intuitive way.
• _<abstract type>:
• _seq: for sequence.
• _gen: for generator.
For Callable
Ops Hint
• <verb>_ = imperative sentence.
• <yes-no question> → boolean.
• to_<thing> → thing.
For Boolean
Ops Hint
• Normal objects take <noun>.
• Callables take <verb>_ and <yes-no question>.
• So use:
• <adj>
• <prep>_
• <participle>_
• <simple sentence>
• <to-infinitive>
For Return Value
Ops Hint
• Use on the return value:
• get_page_no → numeric >= 1.
• query_user → user object.
• parse_to_tree → tree object.
Explicit Unknown
Ops Hint
• _x: anything.
• Rather than an ambiguous name.
• You won't forget to determine the ops it supports.
• Use hasattr or isinstance later.
Avoid None and Null
Ops Hint
• Consider:
• user = query_user(uid)

user.is_valid()
• Then query_user returns None.
• Boom! An AttributeError! ∵ None supports almost no op.
• Accept an exception?
• Y: just raise when you wanna return None.
• N: use a dummy object like a dummy user or an empty str, etc.
tmpl = '<p>{}</p>'

tmpl.format(name)
name = '<script>...</script>'
!
tmpl_html = ...

tmpl_html.format(

escape_to_html(name)

)
✓
arg = parse(arg)
?
arg_d = parse(arg_json)
✓
str/x
Ops Hint
• _key: key (of a dict).
• 'lowercase_n_underscore'
• Or just k.
• _url: URL.
• Percent-encode.
• _json: JSON.
• JSON is a string, actually.
• _html: HTML.
• Avoid XSS.
• _sql: SQL.
• Avoid SQL injection.
numeric/x
Ops Hint
• _no: number, #.
• ≥ 1.
• _idx: index.
• ≥ 0.
• Or just i, j, k.
• _secs
• It's seconds!
• _pct
• n = 10%

n_pct = 10
• _month, _day, ...
• Use month_str if in string.
Structure Hint
users
?
users = {

'mosky': 'mosky.tw@gmail.com',
...

}
uid_email_map = {

'mosky': 'mosky.tw@gmail.com',
...

}
uid_email_map
✓
uid_email_pairs = ...
uid_email_map = dict(↖)
✓
# select uid, email
for uid, email in uid_email_pairs:

...
✓
For Dict & Tuple
Structure Hint
• <key>_<value>_map
• For tuples:
• _pair: 2-tuple.
• _pairs: 2-tuples.
• <1st>_<2nd>_<3rd>_tuple: n-tuple.
Others
“Don't use me!”
• _<name>
• Don't use when out of
• a module.
• a class.
• Don't guarantee the behavior.
• Make it easier to trace and refactor.
Private Hint
“Should I cache it?”
Performance Hint
• get_: memory op.
• parse_ / calc_ : CPU-bound op.
• query_: IO-bound op.
• query_or_get_: IO-bound op with cache.
Progressive 

From Zero
Too long? Use proper abbreviation.
Define in
Comment
receiver_address_dict
# rad: receiver addr dict
rad = ...
Paragraph 

& Section
def request_or_get(url):
if has_cache(url):

return get_cache(url)
content = request(url)

set_cache(url, content)
return content
# Check Arguments
...

...
# Query from Tables
...

...
...

...
# Transform
...
With Blank Line and Comment
Paragraph & Section
• Like writing reStructuredText or Markdown.
• Paragraph:
• A paragraph contains no blank line.
• A blank line separates paragraphs.
• Section:
• A section contains paragraphs.
• A “title comment” separates sections.
• A semi-function.
Line Functions Up
Func A
Func U
Func B
Line No
Func A
Func U
Func B
Line No
Func A
Func U
Func B
Line No
Func A
Func U
Func B
Module 1
Module 2
By Calls
Line Functions Up
• Measure the “disorderedness”.
• In a file,
• Keep the lines has same direction.
• Layer functions, and may become modules.
• In a project,
• Apply it to modules.
Face 

Bad Smell
Don't lost!
Face Bad Smell
• Comment:
• Pitfalls, the actual return type, side effects, etc.
• # TODO:
• Seal it with better name or stabler wrapper.
• good = poor()
• def good(): poor()
• Stay focused!
Use hints to 

boost maintainability!
🚀
Photo Credits
• “The Money“

https://commons.wikimedia.org/wiki/
File:Forex_Money_for_Exchange_in_Currency_Bank.jpg
• “The Bricks”

https://pixabay.com/zh/%E5%BB%BA%E8%AE%BE-
%E6%92%AD%E6%94%BE-%E7%8E%A9%E8%80%8D-
%E4%B9%90%E8%B6%A3-%E5%88%9B%E6%84%8F-1159776/
• Thanks!

More Related Content

What's hot (20)

PDF
Python: an introduction for PHP webdevelopers
Glenn De Backer
 
PDF
Understanding PHP objects
julien pauli
 
PDF
Power of Puppet 4
Martin Alfke
 
PDF
Bringing modern PHP development to IBM i (ZendCon 2016)
James Titcumb
 
PDF
Spl in the wild
Elizabeth Smith
 
ODP
using python module: doctest
mitnk
 
PPTX
Streams, sockets and filters oh my!
Elizabeth Smith
 
PDF
Doing It Wrong with Puppet -
Puppet
 
PPTX
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Ilya Grigorik
 
PPT
PHP - Introduction to PHP Functions
Vibrant Technologies & Computers
 
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
James Titcumb
 
ODP
How Xslate Works
Goro Fuji
 
PDF
Building Custom PHP Extensions
AzRy LLC, Caucasus School of Technology
 
PPT
Hacking with hhvm
Elizabeth Smith
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
Php Extensions for Dummies
Elizabeth Smith
 
PDF
Damien seguy php 5.6
Damien Seguy
 
PDF
PHP7 is coming
julien pauli
 
PDF
Cross platform php
Elizabeth Smith
 
Python: an introduction for PHP webdevelopers
Glenn De Backer
 
Understanding PHP objects
julien pauli
 
Power of Puppet 4
Martin Alfke
 
Bringing modern PHP development to IBM i (ZendCon 2016)
James Titcumb
 
Spl in the wild
Elizabeth Smith
 
using python module: doctest
mitnk
 
Streams, sockets and filters oh my!
Elizabeth Smith
 
Doing It Wrong with Puppet -
Puppet
 
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
Ilya Grigorik
 
PHP - Introduction to PHP Functions
Vibrant Technologies & Computers
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
James Titcumb
 
How Xslate Works
Goro Fuji
 
Building Custom PHP Extensions
AzRy LLC, Caucasus School of Technology
 
Hacking with hhvm
Elizabeth Smith
 
Php extensions
Elizabeth Smith
 
Php extensions
Elizabeth Smith
 
Php Extensions for Dummies
Elizabeth Smith
 
Damien seguy php 5.6
Damien Seguy
 
PHP7 is coming
julien pauli
 
Cross platform php
Elizabeth Smith
 

Viewers also liked (16)

PDF
Graph-Tool in Practice
Mosky Liu
 
PDF
Concurrency in Python
Mosky Liu
 
PDF
ZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
Mosky Liu
 
PDF
Programming with Python - Adv.
Mosky Liu
 
PDF
Learning Git with Workflows
Mosky Liu
 
PDF
Learning Python from Data
Mosky Liu
 
PDF
Simple Belief - Mosky @ TEDxNTUST 2015
Mosky Liu
 
PDF
Programming with Python - Basic
Mosky Liu
 
PDF
Twitterology - The Science of Twitter
Bruno Gonçalves
 
PDF
Minimal MVC in JavaScript
Mosky Liu
 
PDF
Introduction to Clime
Mosky Liu
 
PPTX
Angular 2 表單的處理與驗證
Jeff Wu
 
PDF
恰如其分的 MySQL 設計技巧 [Modern Web 2016]
Yi-Feng Tzeng
 
PDF
Object-oriented Programming in Python
Juan-Manuel Gimeno
 
PDF
Mining Georeferenced Data
Bruno Gonçalves
 
PPT
Social Network Analysis
Giorgos Cheliotis
 
Graph-Tool in Practice
Mosky Liu
 
Concurrency in Python
Mosky Liu
 
ZIPCodeTW: Find Taiwan ZIP Code by Address Fuzzily
Mosky Liu
 
Programming with Python - Adv.
Mosky Liu
 
Learning Git with Workflows
Mosky Liu
 
Learning Python from Data
Mosky Liu
 
Simple Belief - Mosky @ TEDxNTUST 2015
Mosky Liu
 
Programming with Python - Basic
Mosky Liu
 
Twitterology - The Science of Twitter
Bruno Gonçalves
 
Minimal MVC in JavaScript
Mosky Liu
 
Introduction to Clime
Mosky Liu
 
Angular 2 表單的處理與驗證
Jeff Wu
 
恰如其分的 MySQL 設計技巧 [Modern Web 2016]
Yi-Feng Tzeng
 
Object-oriented Programming in Python
Juan-Manuel Gimeno
 
Mining Georeferenced Data
Bruno Gonçalves
 
Social Network Analysis
Giorgos Cheliotis
 
Ad

Similar to Boost Maintainability (20)

PPTX
Code Like Pythonista
Chiyoung Song
 
KEY
Test First Teaching
Sarah Allen
 
PDF
Python in 90 minutes
Bardia Heydari
 
KEY
Test First Teaching and the path to TDD
Sarah Allen
 
PPTX
Python assignment help
www.myassignmenthelp.net
 
PDF
Conf orm - explain
Louise Grandjonc
 
PDF
TDD with phpspec2
Anton Serdyuk
 
PPTX
Functions, List and String methods
PranavSB
 
KEY
Developer testing 201: When to Mock and When to Integrate
LB Denker
 
PDF
Django at Scale
bretthoerner
 
PDF
Introduction to Python for Plone developers
Jim Roepcke
 
PPTX
Python Details Functions Description.pptx
2442230910
 
PDF
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth
 
KEY
Php Code Audits (PHP UK 2010)
Damien Seguy
 
PDF
singh singhsinghsinghsinghsinghsinghsinghsinghsingh.pdf
horiamommand
 
PDF
Hacker 101/102 - Introduction to Programming w/Processing
Dan Chudnov
 
PPTX
CPP02 - The Structure of a Program
Michael Heron
 
PDF
Intro to Python
Daniel Greenfeld
 
PDF
Reflex - How Does It Work? (extended dance remix)
Rocco Caputo
 
PDF
Lua pitfalls
Dmitriy Kotelnikov
 
Code Like Pythonista
Chiyoung Song
 
Test First Teaching
Sarah Allen
 
Python in 90 minutes
Bardia Heydari
 
Test First Teaching and the path to TDD
Sarah Allen
 
Python assignment help
www.myassignmenthelp.net
 
Conf orm - explain
Louise Grandjonc
 
TDD with phpspec2
Anton Serdyuk
 
Functions, List and String methods
PranavSB
 
Developer testing 201: When to Mock and When to Integrate
LB Denker
 
Django at Scale
bretthoerner
 
Introduction to Python for Plone developers
Jim Roepcke
 
Python Details Functions Description.pptx
2442230910
 
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth
 
Php Code Audits (PHP UK 2010)
Damien Seguy
 
singh singhsinghsinghsinghsinghsinghsinghsinghsingh.pdf
horiamommand
 
Hacker 101/102 - Introduction to Programming w/Processing
Dan Chudnov
 
CPP02 - The Structure of a Program
Michael Heron
 
Intro to Python
Daniel Greenfeld
 
Reflex - How Does It Work? (extended dance remix)
Rocco Caputo
 
Lua pitfalls
Dmitriy Kotelnikov
 
Ad

More from Mosky Liu (7)

PDF
Statistical Regression With Python
Mosky Liu
 
PDF
Practicing Python 3
Mosky Liu
 
PDF
Data Science With Python
Mosky Liu
 
PDF
Hypothesis Testing With Python
Mosky Liu
 
PDF
Dive into Pinkoi 2013
Mosky Liu
 
PDF
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
Mosky Liu
 
PDF
MoSQL: More than SQL, but less than ORM
Mosky Liu
 
Statistical Regression With Python
Mosky Liu
 
Practicing Python 3
Mosky Liu
 
Data Science With Python
Mosky Liu
 
Hypothesis Testing With Python
Mosky Liu
 
Dive into Pinkoi 2013
Mosky Liu
 
MoSQL: More than SQL, but Less than ORM @ PyCon APAC 2013
Mosky Liu
 
MoSQL: More than SQL, but less than ORM
Mosky Liu
 

Recently uploaded (20)

PDF
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
PPTX
ChessBase 18.02 Crack + Serial Key Free Download
cracked shares
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
SAP GUI Installation Guide for Windows | Step-by-Step Setup for SAP Access
SAP Vista, an A L T Z E N Company
 
PPTX
SAP Public Cloud PPT , SAP PPT, Public Cloud PPT
sonawanekundan2024
 
PDF
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
PDF
Enhancing Security in VAST: Towards Static Vulnerability Scanning
ESUG
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
How to Download and Install ADT (ABAP Development Tools) for Eclipse IDE | SA...
SAP Vista, an A L T Z E N Company
 
PDF
Virtual Threads in Java: A New Dimension of Scalability and Performance
Tier1 app
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PDF
Malaysia’s e-Invoice System: A Complete Guide for Businesses
Matiyas Solutions
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PDF
AI Software Engineering based on Multi-view Modeling and Engineering Patterns
Hironori Washizaki
 
PDF
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PDF
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 
Balancing Resource Capacity and Workloads with OnePlan – Avoid Overloading Te...
OnePlan Solutions
 
ChessBase 18.02 Crack + Serial Key Free Download
cracked shares
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
SAP GUI Installation Guide for Windows | Step-by-Step Setup for SAP Access
SAP Vista, an A L T Z E N Company
 
SAP Public Cloud PPT , SAP PPT, Public Cloud PPT
sonawanekundan2024
 
AI Image Enhancer: Revolutionizing Visual Quality”
docmasoom
 
Enhancing Security in VAST: Towards Static Vulnerability Scanning
ESUG
 
Presentation about variables and constant.pptx
kr2589474
 
How to Download and Install ADT (ABAP Development Tools) for Eclipse IDE | SA...
SAP Vista, an A L T Z E N Company
 
Virtual Threads in Java: A New Dimension of Scalability and Performance
Tier1 app
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Malaysia’s e-Invoice System: A Complete Guide for Businesses
Matiyas Solutions
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
AI Software Engineering based on Multi-view Modeling and Engineering Patterns
Hironori Washizaki
 
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
 

Boost Maintainability