Documenting JavaScript

JSDoc is a popular approach to inline documentation used in many code bases.
At its simplest, it involves creating a comment with two leading asterisks (/**), and a description of what the function does.
/** * Add two numbers together */ function add (num1, num2) { return num1 + num2; } The JSDoc becomes more meaningful when you add tags: additional details about the function, it’s parameters, and what it returns.


This content originally appeared on Go Make Things and was authored by Go Make Things

JSDoc is a popular approach to inline documentation used in many code bases.

At its simplest, it involves creating a comment with two leading asterisks (/**), and a description of what the function does.

/**
 * Add two numbers together
 */
function add (num1, num2) {
	return num1 + num2;
}

The JSDoc becomes more meaningful when you add tags: additional details about the function, it’s parameters, and what it returns.

You can find a full list on the JSDoc website, but the most common are @param, which describes a function parameter, and @return, which describes what’s returned.

/**
 * Add two numbers together
 * @param  {Number} num1 The first number to add
 * @param  {Number} num2 The second number to add
 * @return {Number}      The total
 */
function add (num1, num2) {
	return num1 + num2;
}

Why use JSDoc?

JSDoc is the JavaScript implementation of DocBlock, a documentation approach used in various programming languages, including PHP, Ruby, and Python.

It provides a consistent and recognizable approach to documentation. There are also several tools that can automatically generate documentation from JSDoc comments.

Automating JSDoc comments

Your text editor can make it easier for you to author JSDoc comments.

In VS Code, typing /** and hitting the enter or return key creates a JSDoc comment with some information pre-populated for you. This is a feature that’s baked-in out-of-the-box.

/**
 * [add description]
 * @param   {[type]}  num1  [num1 description]
 * @param   {[type]}  num2  [num2 description]
 * @return  {[type]}        [return description]
 */

In SublimeText, the DocBlockr package adds the same functionality, and also works in other languages.

How much documentation is enough?

A lot of senior developers are obsessed with the idea of “self-documenting code.” That is, code whose purpose is so obvious that it doesn’t need documentation.

This is a myth.

What’s obvious to you may not be obvious to someone else reading your code. Documenting helps them work faster and easier. The self-documenting code myth is a form of gatekeeping.

Good documentation doesn’t just describe what’s happening, but why you’re doing it. That’s the kind of stuff you’ll forget a year from now when you go to look at code you haven’t touched in a while.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-03-30T14:30:00+00:00) Documenting JavaScript. Retrieved from https://www.scien.cx/2021/03/30/documenting-javascript/

MLA
" » Documenting JavaScript." Go Make Things | Sciencx - Tuesday March 30, 2021, https://www.scien.cx/2021/03/30/documenting-javascript/
HARVARD
Go Make Things | Sciencx Tuesday March 30, 2021 » Documenting JavaScript., viewed ,<https://www.scien.cx/2021/03/30/documenting-javascript/>
VANCOUVER
Go Make Things | Sciencx - » Documenting JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/30/documenting-javascript/
CHICAGO
" » Documenting JavaScript." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/03/30/documenting-javascript/
IEEE
" » Documenting JavaScript." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/03/30/documenting-javascript/. [Accessed: ]
rf:citation
» Documenting JavaScript | Go Make Things | Sciencx | https://www.scien.cx/2021/03/30/documenting-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.