Effective commenting is an essential practice in software development to ensure code clarity, maintainability, and collaboration among developers. This document outlines rules and best practices for writing comments in codebases.
// Check if the user is authenticated
if (!user.isAuthenticated) redirectToLogin();
/*
* This function calculates the factorial of a number.
* It uses a recursive approach to multiply all positive integers up to the given number.
*/
function factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
@param
and @return
./**
* Calculates the sum of two numbers.
* @param {number} a - The first number.
* @param {number} b - The second number.
* @return {number} The sum of the two numbers.
*/
function add(a, b) {
return a + b;
}
# Use binary search to improve performance on sorted data
while left <= right:
mid = (left + right) // 2
if data[mid] == target:
return mid
// Using a LinkedHashMap to maintain insertion order for predictable iteration
Map<String, String> map = new LinkedHashMap<>();
// This function assumes $array is sorted; behavior is undefined otherwise.
function binarySearch($array, $key) {
// ... implementation ...
}
// BAD: Adds two numbers
int sum = a + b;
// GOOD: Add numbers to calculate the total expense
int totalExpense = foodCost + transportCost;
# BAD: Set the timeout to 5000ms
config.timeout = 5000;
# GOOD: Set the timeout for the network request
config.timeout = NETWORK_TIMEOUT;
// TODO: Implement error handling for API requests
fetchData();
// FIXME: Resolve memory leak issue in the caching mechanism
cache.clear();