Write code that prioritizes readability and maintainability over cleverness or excessive optimization. Avoid overly clever techniques that might confuse other developers or make the code harder to maintain.
Write code that prioritizes readability and maintainability over cleverness or excessive optimization. Avoid overly clever techniques that might confuse other developers or make the code harder to maintain.
Key principles:
Example - Instead of this clever approach:
const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
setOwnProperty(handleTypes, -1, null);
// Later using handleTypes[someIndex]
Prefer this more explicit approach:
const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
function getHandleType(type) {
return type >= 0 && type < handleTypes.length ? handleTypes[type] : null;
}
Similarly, prefer direct string creation over array joining for static content:
// Instead of:
const message = [
'Error: This file cannot be parsed as either CommonJS or ES Module.',
'- CommonJS error: await is only valid in async functions.',
'- ES Module error: require is not defined in ES module scope.'
].join('\n');
// Prefer:
const message = 'Error: This file cannot be parsed as either CommonJS or ES Module. ' +
'CommonJS error: await is only valid in async functions. ' +
'ES Module error: require is not defined in ES module scope.';
When functions are called multiple times, consider the performance implications of inlining vs. extracting. Unnecessary function extraction can hinder compiler optimizations in hot paths.
Enter the URL of a public GitHub repository