Skip to content

Comments

Comments are used to document code and are ignored by the interpreter.

Single-Line Comments

Use // for single-line comments:

// This is a comment
print "Hello";  // Comment after code

// Comments can explain what code does
let user_age = 25;  // Store the user's age

Multi-Line Comments

CacaoLang currently only supports single-line comments. For multi-line comments, use multiple // lines:

// This is a multi-line comment
// that spans several lines
// to provide detailed explanation
let complex_calculation = 42;

Documentation Comments

Use comments to document functions:

// Calculate the factorial of a number
// Parameters:
//   n - the number to calculate factorial for
// Returns:
//   the factorial of n
fn factorial(n) {
    if n <= 1 {
        return 1;
    }
    return n * factorial(n - 1);
}

Best Practices

1. Explain Why, Not What

// Good - explains why
// Use binary search because list is sorted
let index = binary_search(items, target);

// Bad - explains what (obvious from code)
// Search for target in items
let index = binary_search(items, target);

2. Keep Comments Updated

// Update comments when code changes
// Calculate total with 10% discount  ← Update this if discount changes
let total = subtotal * 0.9;

3. Use Comments for TODOs

// TODO: Add input validation
// TODO: Handle edge case when array is empty
// FIXME: This breaks with negative numbers