Implement defensive programming practices to prevent runtime errors and provide meaningful error information when failures occur. This includes validating object state before operations and ensuring error messages contain specific contextual details.
Implement defensive programming practices to prevent runtime errors and provide meaningful error information when failures occur. This includes validating object state before operations and ensuring error messages contain specific contextual details.
Key practices:
Example from window management:
// Bad - can crash if window is destroyed
showWindow() {
this.window.show(); // TypeError: Object has been destroyed
}
// Good - validate state first
showWindow() {
if (!this.window.isDestroyed()) {
this.window.show();
}
}
Example from error construction:
// Bad - vague error message
throw new Error(`Cannot update to this version`);
// Good - specific error message
throw new Error(`Cannot update to version ${actualVersion}. Minimum supported version is ${minVersion}`);
This approach prevents crashes from invalid operations and provides developers with actionable information when errors do occur.
Enter the URL of a public GitHub repository