Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document a argument list with a variable length with known parameter types?

Related: Correct way to document open-ended argument functions in JSDoc

I've a function that accepts multiple arrays by accessing the arguments variable:

/**
 * @param options An object containing options
 * @param [options.bind] blablabla (optional)
 */
function modify_function (options) {
    for (var i=1; i<arguments.length; i++) {
        // ...
    }
}

Now, I know that each argument besides options is an array containing values that are worth documenting:

[search_term, replacement, options]

I'm not considering putting the (lengthy) description in the variable parameter line.

@param {...} An array with search terms, replacements and its options; index 0: the search term within the function; 1: the replacement text; 2: optional options (catch_errors: catches errors and log it, escape: escape dollars in the replacement text, pos: "L" for placing the replacement before the search term, "R" for placing it after) Not a readable solution and the type is not visible.

Is there a way to document the types and values of a variable parameter?

@param {...[]} An array with search terms, replacements and its options
@param {...[0]} The search term within the function
@param {...[1]} The replacement text 
@param {...[2]} An optional object with obtions for the replacement
@param {...[2].catch_errors} catches errors and log it
@param {...[2].escape} etc...

The above looks ugly, but it should give you an idea of what I'm trying to achieve:

  • document the type of a variable parameter (in this case an Array)
  • document the values of this array
  • document the properties of an object inside this array

For laziness, I've used an array instead of a object. Other suggestions are always welcome.

like image 491
Lekensteyn Avatar asked Jun 25 '11 09:06

Lekensteyn


People also ask

How do you declare a variable length argument?

Practical Data Science using Python As the name implies, an argument with a variable length can take on a variety of values. You define a variable argument using a '*', for example *args, to show that the function can take a variable number of arguments.

What is variable length parameter list?

Variable-length argument lists are a new feature in J2SE 5.0. Programmers can create methods that receive an unspecified number of arguments. An argument type followed by an ellipsis (...) in a method's parameter list indicates that the method receives a variable number of arguments of that particular type.

What is variable length parameter list in Java?

A method with variable length arguments(Varargs) in Java can have zero or multiple arguments. Variable length arguments are most useful when the number of arguments to be passed to the method is not known beforehand. They also reduce the code as overloaded methods are not required.


1 Answers

Your function is not truly variable arguments, you should just change its signature to what foundrama suggested. Except that JSDoc has syntax a little better than foundrama suggested

/**
 * @param {String} searchTerm
 * @param {String} replacementText
 * @param {Object} opts (optional) An object containing the replacement options
 * @param {Function} opts.catch_errors Description text
 * @param {Event} opts.catch_errors.e The name of the first parameter 
 *         passed to catch_errors
 * @param {Type} opts.escape Description of options
 */

And you'd call it like

modify_text('search', 'replacement', {
    catch_errors: function(e) {

    },
    escape: 'someEscape'

});

If you really do have a case for varargs style, it should be a variable of the same type that can be passed at the end of the parameter list, I document it like the following, though it's not a JSDoc standard, it's what Google uses with their documentation

/**
 * Sums its parameters
 * @param {...number} var_args Numbers to be added together
 * @return number
 */
function sum(/* num, num, ... */) { 
    var sum = 0;
    for (var i =0; i < arguments.length; i++) {
      sum += arguments[i];
    }
    return sum;
}
like image 66
Juan Mendes Avatar answered Oct 05 '22 05:10

Juan Mendes