Generate comprehensive code documentation including docstrings, comments, and API documentation. Use when code lacks documentation, needs better explanations, or requires API docs. Focuses on clarity, completeness, and following language-specific documentation standards.
Input (Python function without docs):
def calculate_total(items, tax_rate=0.1):
subtotal = sum(item.price for item in items)
return subtotal * (1 + tax_rate)
Output:
def calculate_total(items: List[Item], tax_rate: float = 0.1) -> float:
"""
Calculate the total price including tax for a list of items.
Args:
items: List of Item objects with a price attribute
tax_rate: Tax rate as a decimal (default: 0.1 for 10%)
Returns:
Total price including tax as a float
Example:
>>> items = [Item(price=10.0), Item(price=20.0)]
>>> calculate_total(items, tax_rate=0.08)
32.4
"""
subtotal = sum(item.price for item in items)
return subtotal * (1 + tax_rate)
Input (JavaScript function):
function processUserData(data) {
const filtered = data.filter(u => u.active);
return filtered.map(u => ({ id: u.id, name: u.name }));
}
Output:
/**
* Process user data by filtering active users and extracting key fields.
*
* @param {Array<Object>} data - Array of user objects
* @param {boolean} data[].active - Whether the user is active
* @param {string} data[].id - User identifier
* @param {string} data[].name - User display name
* @returns {Array<Object>} Array of processed user objects with id and name
*
* @example
* const users = [
* { id: '1', name: 'Alice', active: true },
* { id: '2', name: 'Bob', active: false }
* ];
* processUserData(users); // [{ id: '1', name: 'Alice' }]
*/
function processUserData(data) {
const filtered = data.filter(u => u.active);
return filtered.map(u => ({ id: u.id, name: u.name }));
}
Python:
Raises:Example: section for complex functionsJavaScript/TypeScript:
@param, @returns, @throws@example for usage demonstrations