When writing code, communication isn’t just between you and the machine, but it’s also between you and other developers (or your future self!). Clear and effective code commenting plays a vital role in making your code understandable, maintainable, and scalable.
In this guide, we’ll explore:
- What code comments are and why they matter
- Different types of comments: inline vs. block
- The power of JSDoc for inline documentation
- How to balance commenting to avoid clutter
- Advantages of structured documentation over simple comments
What Are Code Comments?
Code comments are notes written inside your source code to explain what the code does, why certain decisions were made, or how to use a function or module. They don’t affect the program execution but are critical for clarity.
Comments help:
- Other developers quickly understand your code
- You recall your own logic months later
- Tools and IDEs provide better assistance (when using structured comments like JSDoc)
Types of Comments: Inline vs. Block
Inline Comments
- Written on a single line, usually beside the code they explain
- Start with
//
in JavaScript - Ideal for brief explanations or notes
Example:
let count = 10; // number of items to process
Block Comments
- Can span multiple lines, enclosed between
/*
and*/
- Used for longer explanations, function headers, or multi-line notes
Example:
/*
Calculates the sum of two numbers.
This function takes two numeric inputs and returns their sum.
*/
function add(a, b) {
return a + b;
}
Why Use JSDoc? The Power of Structured Inline Documentation
While simple comments are helpful, JSDoc takes documentation to the next level by providing a structured way to describe functions, parameters, return values, and more — all inline with your code.
Example JSDoc usage:
/**
* Adds two numbers.
* @param {number} a - First number
* @param {number} b - Second number
* @returns {number} Sum of a and b
*/
function add(a, b) {
return a + b;
}
Benefits of JSDoc over simple comments:
- Provides type information for parameters and return values
- Helps IDEs (like VSCode) show helpful hints and autocomplete suggestions
- Enables automatic generation of API documentation with tools like
jsdoc
CLI - Improves readability by enforcing a standard format
- Clarifies the intent and usage of functions better than generic comments
Balancing Comments: When and How Much to Comment?
While commenting is crucial, too many comments can clutter code and reduce readability. Here are some tips for a good balance:
- Write self-explanatory code first: Use meaningful variable and function names so that the code itself communicates its purpose. This reduces the need for excessive commenting.
-
Comment the “why,” not the “what”: Explain the reason behind complex logic, not obvious code. For example, avoid comments like
// adds two numbers
on a simple addition function. - Document public APIs and complex functions: Use JSDoc or block comments to describe how to use functions, especially if they have multiple parameters or tricky behavior.
- Avoid redundant comments: Comments that just repeat what the code does add no value and should be removed.
Inline Comment vs. JSDoc: A Quick Comparison
Aspect | Inline Comment (// ) |
JSDoc (/** ... */ ) |
---|---|---|
Purpose | Brief notes or reminders | Structured documentation of code |
Format | Simple, unstructured | Formal tags like @param , @returns
|
IDE Support | Basic syntax highlighting | Enhanced support: hints, autocomplete |
Documentation Generation | None | Supports automatic API docs |
Example: Comparing Inline Comment and JSDoc
Inline Comment
// Adds two numbers and returns the result
function add(a, b) {
return a + b;
}
JSDoc Comment
/**
* Adds two numbers.
* @param {number} a - First number
* @param {number} b - Second number
* @returns {number} Sum of a and b
*/
function add(a, b) {
return a + b;
}
The JSDoc comment provides richer information and better tool support.
Summary:
Good commenting is an art. Writing clear, concise, and meaningful comments, especially using tools like JSDoc — makes your code more understandable and maintainable for everyone, including your future self.
Always strive to:
- Write readable code first
- Use comments to explain “why” and document usage
- Use JSDoc for structured, standardized documentation
This will save time, reduce bugs, and make collaboration much smoother.
Top comments (0)