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
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 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.
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(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
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.)
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.)
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.
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