Skip to main content

Comments & Documentation

Every language has its own syntax for comments and documentation. Here's how different languages handle code comments and documentation generation.

Comments​

// Single-line comment

/* Multi-line
comment */

/**
* JSDoc documentation comment
* @param {string} name - The user's name
* @param {number} age - The user's age
* @returns {string} A greeting message
* @throws {Error} If name is empty
* @example
* greet("John", 30); // "Hello, John!"
*/
function greet(name, age) {
return "Hello, " + name + "!";
}

// TODO: Implement this feature
// FIXME: This has a bug
// NOTE: Important detail

Key Takeaways​

  • Documentation generation tools -- Each ecosystem has standard tools: JSDoc for JavaScript, PHPDoc for PHP, cargo doc for Rust, go doc for Go, Sphinx or pydoc for Python, Doxygen for C/C++, Javadoc for Java, YARD for Ruby, and swift-docc for Swift. These tools scan doc comments and produce HTML or other formats. For example, cargo doc --open in Rust generates and opens API docs; javadoc -d docs src/*.java does the same for Java. When starting a project, adopt the ecosystem's default tool early so docs stay in sync with code.

  • Doc-comment conventions -- Triple-slash (///) is used in Rust, Zig, C#, and Swift for documentation that precedes functions or types. JavaScript, PHP, Java, and C/C++ use /** */ blocks with tags like @param, @returns, and @throws. C# also supports XML tags such as <summary>, <param>, and <returns>. Python uses docstrings (triple-quoted strings) as the primary doc format. Choose the convention that matches your language's tooling; mixing styles in one project causes confusion.

  • Doc-comment syntax and tags -- Common tags across languages include parameter descriptions (@param name), return values (@returns or @return), exceptions (@throws), and examples (@example or # Examples). Rust and Zig support Markdown inside doc comments; C# uses XML. For maximum clarity, document parameters and return values for every public function, and add a short example where it helps. Sparse or inconsistent docs hurt maintainability.

  • Code example testing -- Rust's cargo test can execute code blocks in doc comments, so examples stay runnable and correct. Python's doctest runs >>> snippets in docstrings as tests. Java and Javadoc support {@code} blocks for display but not automatic execution. Investing in testable examples (Rust, Python) reduces stale or broken docs. When your language supports it, add runnable examples and run them in CI.