Define a clear, hierarchical structure for application configurations with descriptive naming and proper organization. Avoid hardcoded values, use named constants for magic numbers, and ensure configuration is loaded only once at startup and passed through the application rather than loaded at arbitrary points.
Define a clear, hierarchical structure for application configurations with descriptive naming and proper organization. Avoid hardcoded values, use named constants for magic numbers, and ensure configuration is loaded only once at startup and passed through the application rather than loaded at arbitrary points.
Key practices:
// Avoid
const deadline = Date.now() + 1000;
// Better
const PROCESS_TERMINATION_TIMEOUT_MS = 1000;
const deadline = Date.now() + PROCESS_TERMINATION_TIMEOUT_MS;
// Avoid
output: {
maxBytes: 12410,
maxLines: 256
}
// Better
tools: {
shell: {
maxBytes: 12410,
maxLines: 256
}
}
// Avoid
client_id: "Iv1.b507a08c87ecfe98"
// Better
client_id: process.env.GITHUB_CLIENT_ID || config.github.clientId
// Avoid
function someFunction() {
const config = loadConfig(); // Loading config at arbitrary points
// ...
}
// Better
function someFunction(config) { // Config passed as parameter
// ...
}
// Avoid
const providersConfig = config.providers; // Could be undefined
// Better
const providersConfig = config.providers ?? defaultProviders;
Following these practices makes configurations more maintainable, testable, and prevents unpredictable behavior in long-running applications when configurations change during execution.
Enter the URL of a public GitHub repository