Always validate configuration structure before accessing properties and provide sensible defaults while preserving explicit user overrides. Configuration code should defensively check for the existence of nested properties and gracefully handle missing or malformed configuration.
Always validate configuration structure before accessing properties and provide sensible defaults while preserving explicit user overrides. Configuration code should defensively check for the existence of nested properties and gracefully handle missing or malformed configuration.
When implementing configuration logic:
Example of problematic code:
// Crashes when dependency.platforms is undefined
Object.keys(dependency.platforms).forEach(platform => {
if (dependency.platforms[platform] == null) {
notLinkedPlatforms.push(platform);
}
});
Example of improved code:
// Defensive validation before access
if (dependency.platforms) {
Object.keys(dependency.platforms).forEach(platform => {
if (dependency.platforms[platform] == null) {
notLinkedPlatforms.push(platform);
}
});
}
For default vs explicit configuration:
// Provide defaults but respect explicit user configuration
if (!config.watchFolders.some(folder => folder !== ctx.root)) {
// Apply auto-detection only when user hasn't specified custom folders
overrides.watchFolders = getWatchFolders(ctx.root);
}
This approach prevents runtime crashes from malformed configuration while maintaining flexibility for users to customize behavior when needed.
Enter the URL of a public GitHub repository