Post on 28-Jul-2015
transcript
It's all about time.
I can write a 4x faster program than you!!
But the hardware is super powerful now, it may be just 0.0001 ms v.s. 0.0004 ms.!
I can write 4x faster.!
And human brain haven't changed a lot,so it may be even 1 week v.s. 1 month.!
Human Time ⋙ Computer Time
How to write faster?
Good language, e.g., Python!
Powerful libraries!
Experience!
∈ The things you can't change immediately.
Understandable codebase. i.e., codebase which has high maintainability.!
∈ You can just learn from this share.
MoskyPython Charmer at Pinkoi!
The author of the Python packages!
MoSQL, Clime, …!
The speaker of the conferences,!
PyCon TW/JP/SG, …!
Also teaching Python!
mosky.tw
Outline
How human brain read code?!
Name thing meaningful but short.!
Paragraphing & Sectioning.!
Find the hidden “lines”.!
Face bad smell healthily.
A Computer
Read the code line by line.!
Cover all of the code, usually.!
Just complain when it doesn't understand.
A Human Brain
We can't read code like a computer.!
CPU is super slow, and even can't focus.!
RAM is tiny, programmer especially.!
Disk is losing bytes all the time.!
Nobody to complain.!
So we prefer to leave code alone.
But, computer is always complaining.!
We jump to the failed line.!
Read back from the line.!
Finally, we leave immediately after we fix it.
Or, new requirement comes.!
We still jump to the related line.!
Read back from the line.!
Finally, we also leave immediately after we finish it.
Maintainability
To understand a random line, how many lines do you need to read back?!
Lesser → Higher maintainability
Nothing But
Have a good dictionary.!
Be exact to avoid ambiguity.!
Be consistent to avoid misleading.!
Hint, hint, and hint.
Be Consistent
Saw this in somewhere:!
apple = Company('apple')
Now you read:!
apple
Then you will guess it is a company. So don't:!
apple = Fruit('apple')
Type Hint
We operate variables.!
count += 1
parse(json)
If we know how to operate it at first glance,it saves time from tracing or running.!
Not the “Type Hint” in PEP 0484
Object or Dict?
user
What is the type of user?!
user['name'] # ?
user.name # ?
user_dict['name']
user_d['name']
Avoid None
d = None if failed else {} d['key'] = value value = d.get('key')
It causes TypeError or AttributeError in Python,!
and extra effort in other language.!
Be consistent in type.
json['keyword'] # ?
Actually the json is a dict-like.!
JSON: JavaScript Object Notation → a string!
arg_dict = json.loads(arg_json)
Transformation!
You can see the domain and codomain.
Structure Hint
We operate the variables.!
If we know how to operate it at first glance,it saves time from tracing or running.!
(Yeah, I copied it from the type hint.)
# ok users = { 'mosky': User('mosky'), … }
# even better uid_user_map = { 'mosky': User('mosky'), … }
key_value_pairs = sql('select key, value')
for key, value in key_value_pairs: …
key_value_map = dict(key_value_pairs)
Composite
If apply structure hint and type hint,!
It will be long.!
You may shorten.!
Keep long description in comment or doc.
Use common abbreviation!
http://abbreviations.com/!
Use acronym, e.g., rlname for really_long_name.!
Do comment if you use them.
# compromise between meaning and length # # event key: e.g., 'monthers_day' # config_d: config dict # # - start_date: … # … # event_key_config_d_map = { 'monthers_day': {…} }
# focus on reducing length # # ekey: event key # config: config dict # # - start_date: … # … # ekey_config_map = { 'monthers_day': {…} }
adj. new_user
past participle joined_date
present participle working_queue
infinitive keys_to_remove
another noun template_args
intn, m
i, j, k twd_int
int/range
page_no birth_month birth_daytids_idx
bool
new, joined user_is_new new_or_not new_bool
str name, month_str
str/key event_key
str/url next_url
str / json user_json
str/html page_html
str/sql to_update_sql
date start_date
datetime joined_dt
re email_re
decimal total_dec
currency total_currency, total_ccy total_twd
Return Type Hint
We operate the variables.!
If we know how to operate it at first glance,it saves time from tracing or running.!
(Yeah, I copied it from type hint, again.)
.allow_to_log_in
Allow something to log in?!
.do_allow_to_log_in
If return value is a bool, use yes–no question.
Performance Hint
get_name # Memory op.
parse_from_json # CPU op.
query_html request_html # IO op.
Let people know the roughy cost.
Private Hint
“Don't touch me hint”!
A simple underscore prefix (_)!
Don't use me out of the module or file.!
Non-privates are just public APIs.
Blank Line
Use blank line to separate your code into:!
Paragraph!
Section!
Like lightweight markup language,e.g., Markdown, RST.
Paragraph
Lines without any blank line.!
Group similar lines into a paragraph.!
Contains sub-paragraph.!
Use blank line to separate paragraphs.
Section
Paragraphs.!
Group similar paragraphs into a section.!
Avoid sub-section.!
Use two blank lines to separate sections.
How messy?
Are the directions are all same?!
Usually ↑ in a file and files.!
Order your code.!
Are they point to limited sections or a files?!
Lesser is better.!
Section or modularize them.
Reality
Don't have time to refactor.!
They actually work well.!
They itch you, but won't beat you.!
You may already understand.
So Just Seal It Off
Comment the pitfalls.!
Use # TODO rather than really refactor.!
Assign return value to a better named variable.!
Wrap them.!
Just write new code.
Recap
We read lines randomly.!
Type hints and other hints!
Paragraph & section by blank line.!
Line your functions.!
Bad smell won't beat you.!
http://mosky.tw
Back-End Engineer Quality Assurance Engineer Data Engineer Search Engineer Android Engineer iOS Engineer