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 vs. 0.0004 ms. I can write 4x faster. And human brain haven't changed a lot, so it may be even 1 week vs. 1 month. Human Time ⋙ Computer Time
“Time is money.” - Benjamin Franklin
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.
Style Guide? # It looks okay! count += 1
You remain only 10k days in your life.
Spend time on writing exciting feature, not debugging.
Mosky Python 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
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.
We read lines randomly.
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 Exact “Apple” Which “Apple”? “Apple, the company.” apple_the_company
date_a-date_b Which dates? today-joined_date
user User’s what? user_name user_email
name Who’s name? user_name store_name
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')
Too Long? The context helps. fruit.py apple
Type Hint We operate variables. count += 1 parse(input_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
Make It Intuitive count should be numeric name string thing names many strings; may be a list
page page_no so should be numeric event event_key so should be string stuff
List or Set? requested_fields & allowed_fields ? set(requested_fileds) & allowed_field_set
List or Generator? large_rows large_rows # ? large_row_gen # generator large_row_it # iterator
Object or Dict? user What is the type of user? user['name'] # ? user.name # ? user_dict['name'] user_d['name']
Is it a bool? new_user A bool? A User instance? is_new_user A bool? A Function? new user_is_new
Is it a function? Use imperative, i.e., start with a verb.
.text .json A function? A string thing? .get_text .parse_from_json .text # string thing