Good Coding Practices#
Language-Specific Conventions#
Every language has its own idioms — learn them
Python: docstrings, PEP 8,
snake_casefor functions,CamelCasefor classesFollowing conventions makes your code readable to others (and to tools)
Bad naming forces the reader to memorize arbitrary labels.
Don’t
def proc(d, t):
return d / t
Do
def compute_speed(distance, time):
return distance / time
Rule of thumb
If you need a comment to explain what a variable is, rename it instead.
Use comments to… |
Avoid comments that… |
|---|---|
Explain why a decision was made |
Repeat what the code already says |
Summarize complex logic at a high level |
Substitute for clear naming |
Provide usage examples |
Excuse unreadable code |
Remember
Code is read far more often than it is written.
Stick to one style: indentation, naming, file structure
Consistency reduces cognitive load
Readability always beats brevity