HOWTO write a function
summary
Argues that a good Python function has three concrete phases: input transform output — validate and winnow inputs up front (Django ORM example), do the work in the middle, then format or return the result; prefers return early to avoid buried side effects; recommends naming and putting defaults next to use (avoid module-level constants), consistent return types, and using language features sparingly — replaced a reduce/groupby with collections.Counter in one-hour refactor to make the code clearer.
AI-written summary · about
chapters (12)
- 0:00Introduction to functions
- 1:21The three-phase structure
- 4:44Refining inputs
- 8:21Returning early
- 10:05Managing constants
- 12:07Exception handling
- 13:21Naming conventions
- 17:12Consistent return types
- 19:34Keeping related logic grouped
- 21:19Linter and language hazards
- 23:21Code review strategies
- 27:33Conclusion
