Design APIs with intuitive defaults for common use cases while providing specialized options for edge cases. This approach makes your API approachable for most users while maintaining flexibility for advanced scenarios.
Design APIs with intuitive defaults for common use cases while providing specialized options for edge cases. This approach makes your API approachable for most users while maintaining flexibility for advanced scenarios.
Key principles:
For example, instead of having separate configuration parameters that are usually set to identical values:
# Problematic design - requires duplicate settings for common case
config = ConfigDict(
val_date_or_time_unit='milliseconds',
ser_date_or_time_unit='milliseconds',
)
# Better design
config = ConfigDict(
date_or_time_unit='milliseconds', # Applies to both validation and serialization
# Add specific overrides only when needed:
val_date_or_time_unit_override=None, # Only set when different from common setting
)
Similarly, when designing validators or constraint systems, provide high-level simple APIs for common cases, with options to bypass default behavior when needed (like the check_unsupported_field_info_attributes=False
parameter that allows silencing warnings in specific contexts).
For URL schemes and other extensible systems, include the most commonly used variants by default while allowing extension for specialized cases.
Enter the URL of a public GitHub repository