Pattern 9: Documentation Generation

Creating Comprehensive Docs from Code

Overview

Documentation generation uses AI to create clear docs from existing code.

Why do this?

  • Document legacy code no one understands
  • Create instant onboarding materials
  • Keep documentation synchronized with code
  • AI infers intent from patterns

Critical for inherited codebases, open source projects, and code others will maintain.

Like code, AI-written docs need human review and editing.

Key Principles

  1. Code as truth - Derive from implementation
  2. Multiple audiences - Users, developers, maintainers
  3. Living documentation - Update when code changes
  4. Examples everywhere - Show usage for every function

Exercise: Document a Utility Library

Pick an undocumented module with 10-15 functions.

Steps:

  1. Analyze: Have AI examine functions, parameters, returns, side effects. Identify purpose and use cases. Write to markdown.

  2. Generate layers: Create API reference, usage guide, architecture overview. Write to markdown.

  3. Add inline docs: Generate docstrings with parameters, returns, examples.

  4. Review: Check accuracy. As you find patterns, have AI apply them to remaining work.

Your Turn

Pick your own project!

Examples:

  • Document personal utility scripts you’ve never explained
  • Generate CONTRIBUTING.md for your open-source project
  • Create user docs for your web app’s API