Chapter 6
Lengthy Method Signatures
Identification
Imagine a method that fetches all active users from a database:
public getUsers() : User[] {
// Does stuff.
}
A business came to you with some new features they need. You'll need to reuse the same logic from the previous method. But now it needs to include all inactive users.
What's the easy fix? Something like this?
public getUsers(includeInactive: boolean = false) : User[] {
}
The Slippery Slope of Optional Parameters
That's a seemingly crafty fix. It ensures you won't duplicate that logic somewhere else by copying and pasting it.
Over time, however, this can slowly grow into a grossly unmaintainable method.
Imagine that, over the next few weeks, several junior developers add some new ...
Get Refactoring TypeScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.