Mastering Docblocks: The Secret to Better Code Documentation and IntelliSense

Mastering Docblocks: The Secret to Better Code Documentation and IntelliSense

12/12/2025 Software Development By Tech Writer
DocumentationTypeScriptPythonBest PracticesIntelliSense

As a programmer, you must be familiar with IntelliSense, right? It’s that helpful feature in your IDE (like VS Code) where, when you start typing a function name or hover over a variable, a popup appears showing the signature, types, and documentation of that symbol.

But have you ever wondered: Why does that appear? Where does that information come from?

The answer—often—is Docblocks.

In this article, we will dive deep into what Docblocks are, why they are crucial for a healthy codebase, and look at concrete examples in TypeScript and Python.

What is a Docblock?

A Docblock (Documentation Block) is a specially formatted comment block placed above a function, class, method, or variable. Unlike standard comments that are meant for the developer reading the source code, Docblocks are designed to be parsed by tools.

These tools can be:

  1. IDEs (like VS Code): To generate IntelliSense tooltips.
  2. Documentation Generators: To build static documentation sites (like TypeDoc for JS/TS or Sphinx for Python).
  3. Linters/Type Checkers: To validate how functions are used.

TypeScript / JavaScript (JSDoc & TSDoc)

In the JavaScript and TypeScript world, the standard format is JSDoc (or the strictly typed variant TSDoc). It starts with /** and ends with */. Each line within usually starts with an asterisk *.

Before Docblock

Let’s look at a simple function without documentation:

function calculateDiscount(price: number, type: string) {
  if (type === 'VIP') {
    return price * 0.8;
  }
  return price * 0.95;
}

When you call this function elsewhere, TypeScript will tell you that price is a number and type is a string. But it won’t tell you what valid values for type are, or what the logic behind the discount is.

After Docblock

Now, let’s add a Docblock:

/**
 * Calculates the final price after applying a discount based on customer type.
 * 
 * @remarks
 * This function assumes the price is in USD usually, but works with any unit.
 * 
 * @param price - The base price of the item. Must be non-negative.
 * @param type - The customer tier. Can be 'VIP' (20% off) or 'Regular' (5% off).
 * @returns The final discounted price.
 * 
 * @example
 * ```ts
 * const final = calculateDiscount(100, 'VIP'); 
 * console.log(final); // Output: 80
 * ```
 */
function calculateDiscount(price: number, type: 'VIP' | 'Regular'): number {
  if (type === 'VIP') {
    return price * 0.8;
  }
  return price * 0.95;
}

What changed?

  1. Description: When you hover over calculateDiscount, you now see a summary of what it does.
  2. @param: Describes each argument. In strict TypeScript, we often put the type in the code (type: 'VIP' | 'Regular'), but the comment explains the meaning (20% vs 5%).
  3. @example: This is incredibly useful. The tooltip will actually show this code snippet, helping other developers understand usage instantly.

Python (Docstrings)

In Python, these are called Docstrings. They are not comments (starting with #), but rather string literals that occur as the first statement in a module, function, class, or method definition. They are defined using triple quotes """.

Before Docstring

def connect_db(host, port, retry):
    # Implementation hidden
    pass

After Docstring

There are several styles for Python docstrings (Google, NumPy, reST). Here is the popular Google Style:

def connect_db(host: str, port: int, retry: bool = False) -> bool:
    """Establishes a connection to the database.

    Attempts to connect to the specified host and port. If the connection
    fails and retry is True, it will attempt one more time.

    Args:
        host (str): The hostname or IP address of the database server.
        port (int): The port number to listen on.
        retry (bool): If True, attempts to reconnect on failure. Defaults to False.

    Returns:
        bool: True if connection is successful, False otherwise.

    Raises:
        ConnectionError: If the network is unreachable.
        
    Example:
        >>> success = connect_db('localhost', 5432)
        >>> print(success)
        True
    """
    # Implementation...
    return True

Key benefits in Python:

  • The docstring is accessible at runtime using connect_db.__doc__.
  • Tools like Sphinx can auto-generate beautiful HTML documentation from these strings.
  • VS Code (via Pylance) renders this beautifully in the hover tooltip.

Best Practices for Docblocks

  1. Don’t State the Obvious:

    // BAD
    /**
     * @param name - The name
     */
    function printName(name: string) {}

    If the variable name is self-explanatory, focus on constraints or details that aren’t obvious (e.g., “The name must be capitalized”).

  2. Keep it Updated: The only thing worse than no documentation is wrong documentation. If you change a parameter name, you MUST update the docblock.

  3. Use Markdown: Most modern IDEs render docblocks using Markdown. You can use bold, code blocks, listings, and links to make your documentation easier to read.

  4. Document “Why”, not just “What”: The code often shows what is happening. The docblock is a great place to explain why a certain algorithm was chosen or edge cases that need attention.

Conclusion

Docblocks are not just “nice to have.” They are a communication tool. They bridge the gap between the code you wrote in the past and the developer (which might be you!) who has to use it in the future.

By adding proper JSDoc or Docstrings, you aren’t just commenting your code; you are actively improving the developer experience (DX) for everyone who interacts with your project.

So next time you write a function, take that extra minute to type /** or """. Your future self will thank you.