Design API parameters to be extensible and future-proof by using object arguments and explicit type definitions instead of boolean flags or simplistic checks. This approach makes APIs more maintainable and easier to evolve over time.
Design API parameters to be extensible and future-proof by using object arguments and explicit type definitions instead of boolean flags or simplistic checks. This approach makes APIs more maintainable and easier to evolve over time.
Key principles:
Example:
// ❌ Avoid rigid parameter design
function validate(condition: string): string {
// ...
}
// ✅ Use extensible object parameters
function validate({
condition,
context
}: {
condition: string,
context?: {
organization?: string,
// Easily extendable with new context properties
}
}): string {
// ...
}
// ❌ Avoid boolean flags that limit future options
interface ComponentProps {
fitMaxContent: boolean;
}
// ✅ Use string literals for extensible options
interface ComponentProps {
fit?: 'max-content' | 'min-content' | 'content'; // Extensible
}
Enter the URL of a public GitHub repository