Skip to main content
Good in-code documentation helps teammates and tools (linters, doc generators, and code assistants) understand and use your code faster. This guide covers practical techniques for adding documentation inside your codebase: short inline comments, docstrings (manually or generated with a code assistant), and README scaffolding for repos. Examples use FastAPI and Pydantic where relevant.

Quick inline comments

Inline comments are best for short clarifications of non-obvious code paths, intent, or one-off details. Keep them concise and place them directly above or beside the line they explain. Example — a simple FastAPI route with an inline comment:
Benefits of inline comments:
  • Minimal maintenance cost for small clarifications.
  • Helpful for explaining why a choice was made (not what the code does).
  • Ideal for temporary notes during refactors.
When to avoid inline comments:
  • Don’t restate obvious code (e.g., i += 1 # increment i).
  • Avoid long prose—use docstrings for public APIs and modules.

Generate docstrings with a code assistant

For public functions, modules, or endpoints, prefer docstrings. They integrate with editors, documentation generators, and type-checking tools. You can use a code assistant (like GitHub Copilot) to bootstrap docstrings, but always review and adapt the generated text. Example of an assistant-generated docstring for a FastAPI route:
Always verify assistant-generated docstrings against your actual code (function signatures, Pydantic models, and repository functions). Generated text is a helpful starting point, not an authoritative source.
Tips for using assistants effectively:
  • Prompt clearly (e.g., “Write a concise Google-style docstring describing parameters, return value, and a usage example”).
  • Confirm argument names, types, and return shapes — assistants sometimes use stale context.
  • Choose a docstring style (PEP 257, Google, NumPy) and keep it consistent across the project.

When models and code drift — verify and correct

Assistants may generate docstrings that reference outdated fields. If your Pydantic model changed, update both the route and the docstring to accurately reflect the request schema and repository behavior. Example Pydantic request model:
If your repository API is get_fake_data(count: int) -> list, update the route to call it correctly and reflect that in the docstring:
If the repository requires locale, change the call to get_fake_data(request.count, request.locale) and update the docstring accordingly.

Generating README.md with a code assistant

Code assistants are useful to bootstrap repository-level documentation like README.md. Use a clear prompt (for example: “Generate a README for this FastAPI-based fake data generator, including setup, usage, and examples”) and then refine the output to reflect your actual repository structure and commands. Example README scaffold an assistant might create (refine to match your repo):
Always cross-check autogenerated README content for accuracy (commands, paths, environment variables, example payloads).

Quick comparison: comments, docstrings, and README

Practical tips and workflow suggestions

  • Use inline comments for small, local clarifications and docstrings for public-facing APIs.
  • Maintain a short review step when using assistants: check types, argument names, and response structure.
  • Keep docstrings consistent (pick one style: Google, NumPy, or PEP 257).
  • For API projects, document request model fields and response payload shapes; it’s helpful for consumers and for tools generating OpenAPI specs.
  • Add automated checks to catch drift: type hints, unit tests for endpoints, and CI tasks that run a subset of integration checks.
Using a mix of concise inline comments, well-formed docstrings, and a clear README will improve maintainability, developer onboarding, and the effectiveness of code-assistant outputs. Thank you for reading this lesson.

Watch Video