When writing Python code, make sure you follow these guidelines strictly to ensure mantainability and production-readiness:
-
Use descriptive variable names. Choose full english words over abbreviations or generic terms.
-
Write function and module docstrings. After every set of changes, use a formatter like
black
, then a linter likepylint
to check for errors and warnings. Correct all of them without bypassing anything until you achieve a perfect 10/10 score. -
Include extensive error handling. Use exceptions for unexpected outcomes and return values for expected outcomes. Use a proper logging module to log any errors and their tracebacks to a log file, not just the console.
-
A function should do one thing and one thing only. Functions should be semantically organized into modules, neatly grouped inside descriptively named directories, and not just sit in the root directory.
-
Write tests whenever you add a new feature or fix a bug, and run those tests after every set of changes to ensure the code works as expected. If tests fail, rewrite and run again until all pass.
-
Follow the Pythonic way of doing things. Use built-in functions over for loops,
Path
objects over raw strings, context managers over open/close statements, exceptions over error values, etc. -
Do not reinvent the wheel. Use well-established libraries and modules for common or complex tasks instead of writing your own implementations.
-
Keep data, configuration, access keys and other resources separate from program logic. Create independent files with the appropriate file extensions:
.env
for environment variables,.toml
for configuration,.csv
or.json
for data, etc. These files should exist inside descriptively named directories and be loaded by the script using the appropriate library. -
Remove unused code regularly, making sure it is truly unused. If you notice the code getting too long or the logic too convoluted, consider a full refactor before proceeding with the next feature.
-
Break tasks into subtasks before approaching them. Track progress using a ROADMAP.md file with checkboxes and update it after every set of changes.
This gist was written entirely by a human. Remember to star it if you found it useful for your use case!