DEV Community

Cover image for Docstrings vs Markdown Docs: What Should Developers Actually Write?
Anoop Kumar Paul for Wonderer Tech

Posted on

Docstrings vs Markdown Docs: What Should Developers Actually Write?

Every developer hits this wall eventually. You know you should document your code. You've heard the lectures. But then you're staring at your project and wondering... do I write docstrings? A README? Both? Some massive documentation site?
It's genuinely confusing. And nobody explains it well.
Here's the thing. Docstrings and markdown documentation aren't competing approaches. They do different jobs. Docstrings live inside your code and document how your API works. Markdown files sit outside your code and explain how to actually use your project.
Different purposes. Different audiences. Both necessary for anything serious.

What Are Docstrings?

Docstrings are inline documentation written directly in your source code. Triple quotes in Python. Special comment blocks in other languages.

`def calculate_total(items, tax_rate=0.08):
    """Calculate the total price including tax.

    Args:
        items: List of item prices.
        tax_rate: Tax rate as decimal. Defaults to 0.08.

    Returns:
        Total price with tax applied.
    """
    subtotal = sum(items)
    return subtotal * (1 + tax_rate)`
Enter fullscreen mode Exit fullscreen mode

That's a docstring. Lives right there with the function. Describes what the function does, what it takes, what it returns.
The purpose is API reference documentation. When someone imports your library and calls help(calculate_total), they see that docstring. When documentation tools scan your codebase, they pull these docstrings out and build API docs from them.
Python has several formatting conventions. Google style is clean and readable. NumPy style works well for scientific code with complex parameters. Pick one and stick with it.
JavaScript uses JSDoc with a different syntax but same idea. Java has Javadoc. The concept translates across languages.

What Is Markdown Documentation?

Markdown documentation means external files. Your README.md. Tutorial guides. Architecture docs. Anything written in those .md files that live alongside your code but not inside it.

# Getting Started

Install the package:

pip install mypackage

Quick example:

from mypackage import calculate_total
total = calculate_total([10.99, 24.50, 8.75])
Enter fullscreen mode Exit fullscreen mode

This is project-level documentation. It explains the big picture. How to install. How to get started. Why your project exists. What problems it solves.
Tools like MkDocs turn these markdown files into documentation websites. Docusaurus does the same thing. GitHub renders your README automatically on your repo page.
Different beast from docstrings entirely.

Key Differences Between Docstrings and Markdown Docs

Key Differences Between Docstrings and Markdown Docs

The location difference matters most practically. Docstrings travel with your code. You update a function, the docstring is right there reminding you to update it too. Markdown files are separate. Easy to forget. Easy to let drift out of sync.
But markdown gives you space. Room for tutorials. Screenshots. Architecture diagrams. Stuff that doesn't belong crammed into a function definition.

When to Use Docstrings

Write docstrings for anything other developers will call directly.
API documentation for functions, classes, and methods. If it's public, it needs a docstring. Full stop. Someone will import it and wonder what it does.

Parameter descriptions and return types. What does this function accept? What comes back? Don't make people read your implementation to figure this out.

Code examples for specific functions. Short examples showing basic usage. Not full tutorials. Just enough to understand the pattern.

Type hints and signatures. Modern Python uses type hints directly, but docstrings can elaborate when types alone don't tell the story.

Auto-generated API reference. Tools like Sphinx and mkdocstrings pull docstrings into searchable documentation. Write good docstrings once, get API docs forever.

Here's my strong opinion: docstrings are mandatory for public APIs. Not optional. Not "nice to have." Mandatory. If you publish a library without docstrings, you're making everyone's life harder including your own in six months.

When to Use Markdown Documentation

Markdown handles everything that doesn't fit in a docstring.

README files and project overviews. What is this project? Why does it exist? How do I install it? First thing anyone sees on GitHub.

Getting started guides and tutorials. Walk through a complete workflow. Multiple functions working together. Real use cases.

Architecture and design decisions. Why did you structure things this way? What are the trade-offs? Where are the extension points?

Changelog and release notes. What changed in version 2.0? What broke? What's deprecated?

User guides and how-to articles. How do I accomplish this specific task? Step-by-step instructions with context.

Contributing guidelines. How do I set up the development environment? What's the PR process? Code style requirements?

None of this belongs in docstrings. You'd end up with function documentation that's 500 lines long. Nobody wants that.

Can You Use Both Together?

Yes. You should use both together. They're complementary.
The workflow looks like this:

Write docstrings for all your public code. Every function, every class, every method that someone might import.

Use tools like mkdocstrings or Sphinx to auto-generate API reference documentation from those docstrings. Write once, publish automatically.
Create markdown docs for everything else. Tutorials that show functions working together. Guides that explain concepts. READMEs that welcome newcomers.

Link between both documentation types. Your tutorial mentions a function? Link to the API reference. Your API docs show a function? Link to the relevant tutorial.

Modern documentation setups make this seamless. MkDocs with mkdocstrings can pull your Python docstrings directly into your markdown documentation site. You get narrative docs and API reference living together.
Example structure:


  index.md           # Project overview
  getting-started.md # Installation and first steps
  tutorials/
    basic-usage.md   # Walk-through tutorial
  api/
    reference.md     # Auto-generated from docstrings
Enter fullscreen mode Exit fullscreen mode

The API reference page literally pulls from your docstrings. Update your code, rebuild the docs, everything stays in sync.

Tools That Bridge Docstrings and Markdown

Several tools exist specifically to connect docstrings with markdown documentation.

MkDocs + mkdocstrings is my current favorite for Python projects. MkDocs builds the documentation site from markdown. mkdocstrings adds the ability to pull docstrings directly into those markdown pages. Clean, modern, works well.

Sphinx is the traditional Python documentation tool. More powerful. Steeper learning curve. Originally used reStructuredText but handles Markdown now with extensions. Powers the documentation for most major Python projects.

pdoc takes a lightweight approach. Point it at your Python package, get HTML documentation from your docstrings. Minimal configuration. Good for smaller projects.

JSDoc does similar work for JavaScript. Parses specially formatted comments and generates HTML documentation.

Javadoc is the Java ecosystem standard. Same concept. Comments in specific format, tool extracts and builds documentation.

All these tools extract docstrings and generate markdown or HTML documentation. Write your docstrings well and the tooling handles the publishing.

Best Practices for Developer Documentation

Write docstrings for all public APIs. Every function, class, and method that's part of your public interface. No exceptions.
Keep docstrings concise and technical. Parameters, return values, brief description of behavior. This isn't the place for lengthy explanations or background context.

Use markdown for narrative and tutorials. Anything that needs more than a paragraph of explanation belongs in external documentation.
Maintain consistency in formatting. Pick a docstring style. Stick with it across the project. Pick a markdown structure. Stick with that too.
Auto-generate API docs from docstrings. Don't manually duplicate information. Let tools do that work. You'll forget to update manual docs. Tools don't forget.

Version control both types together. Documentation lives in the same repo as code. Same commits. Same branches. Same review process.

Common Mistakes to Avoid

Duplicating information across docstrings and markdown. If you describe a function in detail in both places, one will become outdated. Use docstrings for API reference, link to them from markdown.

Writing lengthy tutorials in docstrings. I've seen docstrings with 200 lines of examples and explanations. That's not a docstring anymore. That's a tutorial crammed into the wrong place.

Neglecting to update docstrings when code changes. Function signature changed but docstring still describes the old parameters. Classic. Review docstrings during code review.

Creating markdown docs without linking to API reference. Your tutorial mentions five functions but doesn't link to their documentation. Users have to hunt around. Make it easy.

Using inconsistent documentation styles. One function uses Google style docstrings. Another uses NumPy style. A third uses some homebrew format. Pick a standard, enforce it.

Should I write docstrings or markdown documentation first?

Write docstrings first. They're coupled to your code. When you write a function, write the docstring immediately. It takes thirty seconds and you understand the function right now.

Markdown docs come later. Once you have working code with docstrings, you can write tutorials and guides that reference that documented API. The docstrings provide the foundation.

Do I need both docstrings and README files?

Yes. They serve completely different purposes.
Docstrings document your code's API. What does this function take? What does it return? How do I call it correctly?
README provides project overview. What is this project? How do I install it? Show me a quick example. Point me to more documentation.
Different audiences. Different needs. Both required for any serious project.

Can docstrings contain markdown formatting?

Yes. Most modern documentation tools support markdown syntax within docstrings.

Sphinx handles it with extensions. MkDocs and mkdocstrings process markdown in docstrings by default. pdoc supports it too.

You can use code blocks, bold text, lists, links. The documentation generator renders it properly. Just don't go overboard. Docstrings should stay concise.

What's the difference between docstrings and comments?

Docstrings are user-facing API documentation. They describe what code does from an external perspective. Call help() on a function and you see its docstring.

Comments are developer notes explaining implementation. Why this algorithm? Why this workaround? Internal context for future maintainers.

def process_data(items):
    """Process items and return cleaned results."""  # Docstring
    # Using set() here to remove duplicates efficiently  # Comment
    unique = set(items)
    return list(unique)
Different purposes. Different audiences. Both valuable.
Enter fullscreen mode Exit fullscreen mode

How do I convert docstrings to markdown documentation?

Use documentation generators designed for this.

For Python: mkdocstrings, Sphinx, or pdoc. Point the tool at your package, configure output format, run the generator. It extracts docstrings and produces markdown or HTML.

For JavaScript: JSDoc parses your comments and generates documentation.
The process is automated. Write docstrings following the expected format, run the tool, get documentation.

Which docstring format should I use?

For Python, use Google style or NumPy style. Both are widely supported by documentation tools. Both are readable.

Google style is more compact:

def example(param1, param2):
    """Brief description.

    Args:
        param1: Description of param1.
        param2: Description of param2.

    Returns:
        Description of return value.
    """
Enter fullscreen mode Exit fullscreen mode

NumPy style is more verbose but clearer for complex scientific parameters:

def example(param1, param2):
    """Brief description.

    Parameters
    ----------
    param1 : type
        Description of param1.
    param2 : type
        Description of param2.

    Returns
    -------
    type
        Description of return value.
    """
Enter fullscreen mode Exit fullscreen mode

Pick one. Use it everywhere. Consistency matters more than which specific style you choose.

Top comments (0)