Improve code readability by reducing nesting depth with early returns and functional approaches. Use early returns to handle edge cases first, and prefer functional operators over nested conditionals when working with streams or collections.
Improve code readability by reducing nesting depth with early returns and functional approaches. Use early returns to handle edge cases first, and prefer functional operators over nested conditionals when working with streams or collections.
Example for early return:
// Instead of this
selectType(event): void {
this.typeSelected = event.value;
if (this.typeSelected === 'New') {
this.volume.controls.name.setValue(this.currentVolName);
}
}
// Prefer this
selectType(event): void {
this.typeSelected = event.value;
if (this.typeSelected !== 'New') return;
this.volume.controls.name.setValue(this.currentVolName);
}
Example for functional approach:
// Instead of this
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
// Do something
}
});
// Prefer this
this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe(event => {
// Do something
});
Enter the URL of a public GitHub repository